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

export async function printReceipt(cafeId: string, orderId: string): Promise<void> {
  await apiPost(`/api/cafes/${cafeId}/print/receipt/${orderId}`, {});
}

export async function printKitchen(cafeId: string, orderId: string): Promise<void> {
  await apiPost(`/api/cafes/${cafeId}/print/kitchen/${orderId}`, {});
}

export async function testPrinter(
  cafeId: string,
  printerIp: string,
  port: number
): Promise<void> {
  await apiPost(`/api/cafes/${cafeId}/print/test`, { printerIp, port });
}

export function printErrorMessage(err: unknown, t: (key: string) => string): string {
  if (err instanceof ApiClientError) {
    if (err.code === "PRINTER_NOT_CONFIGURED" || err.code === "KITCHEN_PRINTER_NOT_CONFIGURED")
      return t("notConfigured");
    if (err.code === "PRINTER_CONNECTION_FAILED") return t("connectionFailed");
  }
  return t("connectionFailed");
}
