import { createContext, useContext } from 'react' import type { AssemblyOptionInput, ItemAdditionalInfo, MarketingData, SKUSpecification, } from '../typings' export interface Context { /** * @deprecated Use `addItems` instead */ addItem: AddItemFn addItems: AddItemsFn updateQuantity: UpdateQuantityFn removeItem: RemoveItemFn setManualPrice: SetManualPriceFn } export type AddItemItemParam = { id: string seller: string quantity: number options?: AssemblyOptionInput[] index?: number } & OptimisticItemInfo type OptimisticItemInfo = { productId?: string name?: string skuName?: string skuSpecifications?: SKUSpecification[] imageUrl?: string price?: number listPrice?: number sellingPrice?: number measurementUnit?: string unitMultiplier?: number detailUrl?: string availability?: string additionalInfo?: ItemAdditionalInfo } export type AddItemsOptions = { marketingData?: Partial salesChannel?: string allowedOutdatedData?: string[] splitItem?: boolean } export type AddItemsFn = ( items: AddItemItemParam[], options?: AddItemsOptions ) => PromiseLike export type AddItemFn = ( items: AddItemItemParam[], marketingData?: Partial, salesChannel?: string ) => void interface ItemIdentificationById { id: string uniqueId?: string index?: number } interface ItemIdentificationByUniqueId { id?: string uniqueId: string index?: number } interface ItemIdentificationByIndex { id?: string uniqueId?: string index: number } type ItemIdentification = | ItemIdentificationById | ItemIdentificationByUniqueId | ItemIdentificationByIndex type UpdateQuantityItemParam = { seller: string quantity: number } & ItemIdentification export type UpdateQuantityOptions = { allowedOutdatedData?: string[] splitItem?: boolean } export type UpdateQuantityFn = ( item: UpdateQuantityItemParam, options?: UpdateQuantityOptions ) => PromiseLike type RemoveItemParam = { seller: string } & ItemIdentification export type RemoveItemFn = ( input: RemoveItemParam, options?: UpdateQuantityOptions ) => void export type SetManualPriceFn = (price: number, itemIndex: number) => void const noop = async () => {} export const OrderItemsContext = createContext({ addItem: noop, addItems: noop, updateQuantity: noop, removeItem: noop, setManualPrice: noop, }) export const useOrderItems = () => { return useContext(OrderItemsContext) }