import { apiPost, ApiClientError } from "@/lib/api/client";

export type PosPaymentRequestResult = {
  sent: boolean;
  skipped: boolean;
  message?: string | null;
};

export async function requestPosPayment(
  cafeId: string,
  branchId: string,
  orderId: string,
  amount: number
): Promise<PosPaymentRequestResult> {
  return apiPost<PosPaymentRequestResult>(
    `/api/cafes/${cafeId}/branches/${branchId}/pos-device/payment-request`,
    { orderId, amount }
  );
}

export function posDeviceErrorMessage(
  err: unknown,
  t: (key: string) => string
): string {
  if (err instanceof ApiClientError) {
    if (err.code === "POS_DEVICE_NOT_CONFIGURED") return t("posDeviceNotConfigured");
    if (err.code === "POS_DEVICE_CONNECTION_FAILED") return t("posDeviceConnectionFailed");
    if (err.code === "POS_DEVICE_TIMEOUT") return t("posDeviceTimeout");
    if (err.code === "POS_DEVICE_REJECTED") return t("posDeviceRejected");
    if (err.code.startsWith("POS_DEVICE")) return t("posDeviceError");
  }
  return t("posDeviceError");
}
