import { KeyValueDTOOfStringAndString } from '@generated/swagger/oms-api';
import { ReceiptItem } from '../../models';

/**
 * Retrieves a specific action object from a receipt item's actions array by key.
 *
 * @param receiptItem - The receipt item containing the actions array.
 * @param actionKey - The key of the action to retrieve.
 * @returns The action object with the specified key, or undefined if not found or if actions are missing.
 *
 * @remarks
 * - Returns undefined if the 'actions' property is missing or not an array.
 * - Returns undefined if no action with the given key exists.
 */
export function getReceiptItemAction(
  receiptItem: ReceiptItem,
  actionKey: string,
): KeyValueDTOOfStringAndString | undefined {
  if ('actions' in receiptItem === false) {
    return undefined;
  }

  return receiptItem.actions?.find((a) => a.key === actionKey);
}
