"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { apiCallOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
import { Button } from "@open-mercato/ui/primitives/button";
import { Spinner } from "@open-mercato/ui/primitives/spinner";
import { EmptyState } from "@open-mercato/ui/primitives/empty-state";
import { SearchX } from "lucide-react";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("sales");
function QuotePublicPage({ params }) {
  const t = useT();
  const token = params?.token;
  const [loading, setLoading] = React.useState(true);
  const [accepting, setAccepting] = React.useState(false);
  const [data, setData] = React.useState(null);
  const [error, setError] = React.useState(null);
  const [acceptedOrder, setAcceptedOrder] = React.useState(null);
  React.useEffect(() => {
    let mounted = true;
    async function run() {
      if (!token) return;
      setLoading(true);
      setError(null);
      try {
        const call = await apiCallOrThrow(`/api/sales/quotes/public/${token}`, {
          method: "GET"
        });
        if (!mounted) return;
        setData(call.result ?? null);
      } catch (err) {
        logger.error("sales.quotes.public.load", { err });
        if (!mounted) return;
        setError(t("sales.quotes.public.failed"));
      } finally {
        if (mounted) setLoading(false);
      }
    }
    void run();
    return () => {
      mounted = false;
    };
  }, [token, t]);
  const handleAccept = React.useCallback(async () => {
    if (!token) return;
    setAccepting(true);
    setError(null);
    try {
      const call = await apiCallOrThrow(`/api/sales/quotes/accept`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ token })
      });
      setAcceptedOrder(call.result ?? null);
    } catch (err) {
      logger.error("sales.quotes.accept", { err });
      setError(t("sales.quotes.public.acceptFailed"));
    } finally {
      setAccepting(false);
    }
  }, [token, t]);
  if (loading) {
    return /* @__PURE__ */ jsx("main", { className: "mx-auto max-w-3xl p-6", children: /* @__PURE__ */ jsxs("p", { className: "flex items-center gap-2 text-sm text-muted-foreground", children: [
      /* @__PURE__ */ jsx(Spinner, { className: "h-4 w-4 animate-spin" }),
      t("sales.quotes.public.loading")
    ] }) });
  }
  if (error || !data) {
    return /* @__PURE__ */ jsxs("main", { className: "mx-auto max-w-3xl p-6 space-y-2", children: [
      /* @__PURE__ */ jsx("h1", { className: "text-2xl font-semibold", children: t("sales.quotes.public.pageTitle") }),
      /* @__PURE__ */ jsx(
        EmptyState,
        {
          variant: "subtle",
          size: "lg",
          icon: /* @__PURE__ */ jsx(SearchX, { className: "h-6 w-6", "aria-hidden": true }),
          title: error ?? t("sales.quotes.public.notFound")
        }
      )
    ] });
  }
  if (acceptedOrder) {
    return /* @__PURE__ */ jsxs("main", { className: "mx-auto max-w-3xl p-6 space-y-2", children: [
      /* @__PURE__ */ jsx("h1", { className: "text-2xl font-semibold", children: t("sales.quotes.public.acceptedTitle") }),
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t("sales.quotes.public.acceptedMessage", { orderNumber: acceptedOrder.orderNumber }) })
    ] });
  }
  return /* @__PURE__ */ jsxs("main", { className: "mx-auto max-w-3xl p-6 space-y-6", children: [
    /* @__PURE__ */ jsxs("header", { className: "space-y-1", children: [
      /* @__PURE__ */ jsxs("h1", { className: "text-2xl font-semibold", children: [
        t("sales.quotes.public.pageTitle"),
        " ",
        data.quote.quoteNumber
      ] }),
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: data.quote.validUntil ? t("sales.quotes.public.validUntil", { date: new Date(data.quote.validUntil).toLocaleDateString() }) : t("sales.quotes.public.noValidityDate") })
    ] }),
    data.isExpired ? /* @__PURE__ */ jsxs("section", { className: "rounded-lg border p-4", children: [
      /* @__PURE__ */ jsx("p", { className: "font-medium", children: t("sales.quotes.public.expired") }),
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t("sales.quotes.public.expiredMessage") })
    ] }) : null,
    /* @__PURE__ */ jsxs("section", { className: "rounded-lg border p-4 space-y-3", children: [
      /* @__PURE__ */ jsx("h2", { className: "font-medium", children: t("sales.quotes.public.items") }),
      /* @__PURE__ */ jsx("div", { className: "space-y-2", children: data.lines.map((line) => /* @__PURE__ */ jsxs("div", { className: "flex items-start justify-between gap-4", children: [
        /* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
          /* @__PURE__ */ jsx("p", { className: "font-medium truncate", children: line.name ?? t("sales.quotes.public.item") }),
          line.description ? /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: line.description }) : null,
          /* @__PURE__ */ jsxs("p", { className: "text-sm text-muted-foreground", children: [
            t("sales.quotes.public.qty", { quantity: line.quantity }),
            line.quantityUnit ? ` ${line.quantityUnit}` : ""
          ] }),
          line.normalizedUnit && (line.normalizedUnit !== line.quantityUnit || line.normalizedQuantity !== line.quantity) ? /* @__PURE__ */ jsxs("p", { className: "text-xs text-muted-foreground", children: [
            line.normalizedQuantity,
            " ",
            line.normalizedUnit
          ] }) : null,
          line.unitPriceReference?.grossPerReference || line.unitPriceReference?.netPerReference ? /* @__PURE__ */ jsxs("p", { className: "text-xs text-muted-foreground", children: [
            line.unitPriceReference?.grossPerReference ?? line.unitPriceReference?.netPerReference ?? "",
            " ",
            line.currencyCode,
            " ",
            t("sales.quotes.public.perReferenceUnit", "per 1 {{unit}}", {
              unit: line.unitPriceReference?.referenceUnitCode ?? line.unitPriceReference?.referenceUnit ?? t("sales.quotes.public.defaultUnit", "unit")
            })
          ] }) : null
        ] }),
        /* @__PURE__ */ jsx("div", { className: "text-right text-sm", children: /* @__PURE__ */ jsxs("p", { children: [
          line.totalGrossAmount,
          " ",
          line.currencyCode
        ] }) })
      ] }, `${line.lineNumber ?? "x"}-${line.name ?? ""}`)) })
    ] }),
    /* @__PURE__ */ jsxs("section", { className: "rounded-lg border p-4 space-y-2", children: [
      /* @__PURE__ */ jsx("h2", { className: "font-medium", children: t("sales.quotes.public.totals") }),
      /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between text-sm", children: [
        /* @__PURE__ */ jsx("span", { children: t("sales.quotes.public.subtotalGross") }),
        /* @__PURE__ */ jsxs("span", { children: [
          data.quote.subtotalGrossAmount,
          " ",
          data.quote.currencyCode
        ] })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between text-sm", children: [
        /* @__PURE__ */ jsx("span", { children: t("sales.quotes.public.discount") }),
        /* @__PURE__ */ jsxs("span", { children: [
          data.quote.discountTotalAmount,
          " ",
          data.quote.currencyCode
        ] })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between text-sm", children: [
        /* @__PURE__ */ jsx("span", { children: t("sales.quotes.public.tax") }),
        /* @__PURE__ */ jsxs("span", { children: [
          data.quote.taxTotalAmount,
          " ",
          data.quote.currencyCode
        ] })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between font-medium", children: [
        /* @__PURE__ */ jsx("span", { children: t("sales.quotes.public.total") }),
        /* @__PURE__ */ jsxs("span", { children: [
          data.quote.grandTotalGrossAmount,
          " ",
          data.quote.currencyCode
        ] })
      ] })
    ] }),
    !data.isExpired ? /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
      error ? /* @__PURE__ */ jsx("p", { className: "text-sm text-destructive", children: error }) : null,
      /* @__PURE__ */ jsxs(Button, { onClick: () => void handleAccept(), disabled: accepting, children: [
        accepting ? /* @__PURE__ */ jsx(Spinner, { className: "mr-2 h-4 w-4 animate-spin" }) : null,
        t("sales.quotes.public.acceptButton")
      ] })
    ] }) : null
  ] });
}
export {
  QuotePublicPage as default
};
//# sourceMappingURL=page.js.map
