import { apiGet, apiPost } from "@/app/lib/clientApi"; import type { PosCounterProduct, PosTender, CounterPlaceSalePayload, PosPlaceSaleResult, PosWalletBalanceView, CounterWalletPreviewPayload, CounterWalletSalePayload, PosWalletSaleResult, MemberCheckoutPreviewResult, CounterSplitSalePayload, SplitSaleResult, SplitSettlementStatus, PosQuotePayload, } from "@/app/lib/types"; // Client-side wrapper for the /pos/counter page (add-branch-counter-sale). Calls only the // portal's own /api/counter/* route handlers — never solvway-mobile-api directly. Ids stay as // strings end-to-end (repo convention snowflake-ids-json-string). The mutation goes through // apiPost so a Next.js GET prefetch can never trigger a sale (CLAUDE.md GET-prefetch rule). /** List the MEMBER-tier products sellable at the operator's branch counter. Pure read. */ export function listCounterProducts(): Promise { return apiGet("/api/counter/products"); } /** List the POS-enabled, active tenders for the operator's branch. Pure read. */ export function listCounterTenders(): Promise { return apiGet("/api/counter/payment-methods"); } /** Place a recorded-tender branch counter sale. Mutation path — POST. */ export function placeCounterSale(payload: CounterPlaceSalePayload): Promise { return apiPost("/api/counter/sale", payload); } // --- Member-wallet tender (add-branch-counter-wallet-tender) --- // The PIN (candidatePin) is held only for the duration of the placeCounterWalletSale call and is // never stored, logged, or persisted client-side. /** Read member wallet balances + PIN status for the counter wallet tender UI. Pure read — GET. */ export function readCounterMemberWallet(memberCode: string): Promise { return apiGet( `/api/counter/wallet/${encodeURIComponent(memberCode)}`, ); } /** Preview the settlement split before placing a counter wallet sale. Mutation path — POST. */ export function previewCounterWallet( payload: CounterWalletPreviewPayload, ): Promise { return apiPost("/api/counter/wallet/preview", payload); } /** Price the Branch Counter cart net of promotions. Nothing is paid or written — POST so a prefetch never fires it. */ export function quoteCounterSale(payload: PosQuotePayload): Promise { return apiPost("/api/counter/quote", payload); } /** * Place a branch-counter member-wallet sale authorized by the member's PIN. * The candidatePin value must be discarded immediately after this call returns. */ export function placeCounterWalletSale( payload: CounterWalletSalePayload, ): Promise { return apiPost("/api/counter/wallet/sale", payload); } // --- Split-tender (wallet + cash + gateway) sale + poll/cancel (split-payment gateway) --- // The candidatePin (present only when a wallet amount is above 0) is held only for the duration of the // placeCounterSplitSale call and is never stored, logged, or persisted client-side. /** Place a branch-counter split-tender sale. Mutation path — POST. */ export function placeCounterSplitSale( payload: CounterSplitSalePayload, ): Promise { return apiPost("/api/counter/split/sale", payload); } /** Poll a split gateway sale's settlement state. Pure read — GET, prefetch-safe. */ export function pollCounterSplitStatus(uuid: string): Promise { return apiGet( `/api/counter/split/status/${encodeURIComponent(uuid)}`, ); } /** Cancel a still-pending split gateway sale (releases reserved stock). Mutation path — POST. */ export function cancelCounterSplit(uuid: string): Promise { return apiPost(`/api/counter/split/cancel/${encodeURIComponent(uuid)}`); }