"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { z } from "zod";
import { DataTable } from "@open-mercato/ui/backend/DataTable";
import { RowActions } from "@open-mercato/ui/backend/RowActions";
import { Button } from "@open-mercato/ui/primitives/button";
import { Badge } from "@open-mercato/ui/primitives/badge";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@open-mercato/ui/primitives/dialog";
import { CrudForm } from "@open-mercato/ui/backend/CrudForm";
import { flash } from "@open-mercato/ui/backend/FlashMessages";
import { readApiResultOrThrow, apiCall, withScopedApiRequestHeaders } from "@open-mercato/ui/backend/utils/apiCall";
import { buildOptimisticLockHeader } from "@open-mercato/ui/backend/utils/optimisticLock";
import { surfaceRecordConflict } from "@open-mercato/ui/backend/conflicts";
import { raiseCrudError } from "@open-mercato/ui/backend/utils/serverErrors";
import { useOrganizationScopeVersion } from "@open-mercato/shared/lib/frontend/useOrganizationScope";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { useConfirmDialog } from "@open-mercato/ui/backend/confirm-dialog";
import { formatDateTime } from "@open-mercato/shared/lib/time";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("sales");
const taxRateFormSchema = z.object({
  name: z.string().trim().min(1),
  code: z.string().trim().min(1),
  rate: z.coerce.number().min(0).max(100),
  countryCode: z.string().trim().optional(),
  regionCode: z.string().trim().optional(),
  postalCode: z.string().trim().optional(),
  city: z.string().trim().optional(),
  isDefault: z.boolean().optional()
});
const DEFAULT_FORM_VALUES = {
  name: "",
  code: "",
  rate: 0,
  countryCode: "",
  regionCode: "",
  postalCode: "",
  city: "",
  isDefault: false
};
function TaxRatesSettings() {
  const t = useT();
  const scopeVersion = useOrganizationScopeVersion();
  const { confirm, ConfirmDialogElement } = useConfirmDialog();
  const [entries, setEntries] = React.useState([]);
  const [loading, setLoading] = React.useState(false);
  const [dialog, setDialog] = React.useState(null);
  const [search, setSearch] = React.useState("");
  const translations = React.useMemo(() => ({
    title: t("sales.config.taxRates.title", "Tax rates"),
    description: t("sales.config.taxRates.description", "Maintain VAT classes applied to catalog pricing."),
    actions: {
      add: t("sales.config.taxRates.actions.add", "Add tax rate"),
      edit: t("sales.config.taxRates.actions.edit", "Edit"),
      delete: t("sales.config.taxRates.actions.delete", "Delete"),
      deleteConfirm: t("sales.config.taxRates.actions.deleteConfirm", 'Delete tax rate "{{name}}"?'),
      refresh: t("sales.config.taxRates.actions.refresh", "Refresh")
    },
    table: {
      name: t("sales.config.taxRates.table.name", "Name"),
      code: t("sales.config.taxRates.table.code", "Code"),
      rate: t("sales.config.taxRates.table.rate", "Rate"),
      location: t("sales.config.taxRates.table.location", "Location"),
      updatedAt: t("sales.config.taxRates.table.updatedAt", "Updated"),
      empty: t("sales.config.taxRates.table.empty", "No tax rates yet."),
      search: t("sales.config.taxRates.table.search", "Search tax rates\u2026"),
      defaultBadge: t("sales.config.taxRates.table.defaultBadge", "Default")
    },
    form: {
      createTitle: t("sales.config.taxRates.form.createTitle", "Add tax rate"),
      editTitle: t("sales.config.taxRates.form.editTitle", "Edit tax rate"),
      name: t("sales.config.taxRates.form.name", "Name"),
      code: t("sales.config.taxRates.form.code", "Code"),
      rate: t("sales.config.taxRates.form.rate", "Rate (%)"),
      countryCode: t("sales.config.taxRates.form.countryCode", "Country code"),
      regionCode: t("sales.config.taxRates.form.regionCode", "Region code"),
      postalCode: t("sales.config.taxRates.form.postalCode", "Postal code"),
      city: t("sales.config.taxRates.form.city", "City"),
      isDefault: t("sales.config.taxRates.form.isDefault", "Default tax class"),
      isDefaultHelp: t(
        "sales.config.taxRates.form.isDefaultHelp",
        "Preselect this class for new catalog products."
      ),
      save: t("sales.config.taxRates.form.save", "Save"),
      cancel: t("sales.config.taxRates.form.cancel", "Cancel"),
      codeHelp: t("sales.config.taxRates.form.codeHelp", "Lowercase letters, numbers, and dashes.")
    },
    messages: {
      saved: t("sales.config.taxRates.messages.saved", "Tax rate saved."),
      deleted: t("sales.config.taxRates.messages.deleted", "Tax rate deleted.")
    },
    errors: {
      load: t("sales.config.taxRates.errors.load", "Failed to load tax rates."),
      save: t("sales.config.taxRates.errors.save", "Failed to save tax rate."),
      delete: t("sales.config.taxRates.errors.delete", "Failed to delete tax rate.")
    }
  }), [t]);
  const fields = React.useMemo(() => [
    { id: "name", label: translations.form.name, type: "text", required: true },
    { id: "code", label: translations.form.code, type: "text", required: true, description: translations.form.codeHelp },
    { id: "rate", label: translations.form.rate, type: "number", required: true },
    { id: "countryCode", label: translations.form.countryCode, type: "text", layout: "half" },
    { id: "regionCode", label: translations.form.regionCode, type: "text", layout: "half" },
    { id: "postalCode", label: translations.form.postalCode, type: "text", layout: "half" },
    { id: "city", label: translations.form.city, type: "text", layout: "half" },
    {
      id: "isDefault",
      label: translations.form.isDefault,
      type: "checkbox",
      description: translations.form.isDefaultHelp
    }
  ], [translations.form]);
  const loadEntries = React.useCallback(async () => {
    setLoading(true);
    try {
      const payload = await readApiResultOrThrow(
        "/api/sales/tax-rates?pageSize=200",
        void 0,
        { errorMessage: translations.errors.load, fallback: { items: [] } }
      );
      const items = Array.isArray(payload.items) ? payload.items : [];
      setEntries(
        items.map((item) => {
          const rawRate = typeof item.rate === "number" ? item.rate : Number(item.rate ?? Number.NaN);
          return {
            id: String(item.id),
            name: typeof item.name === "string" && item.name.length ? item.name : "\u2014",
            code: typeof item.code === "string" && item.code.length ? item.code : null,
            rate: Number.isFinite(rawRate) ? rawRate : null,
            isDefault: Boolean(
              typeof item.isDefault === "boolean" ? item.isDefault : typeof item.is_default === "boolean" ? item.is_default : false
            ),
            countryCode: typeof item.countryCode === "string" && item.countryCode.length ? item.countryCode : null,
            regionCode: typeof item.regionCode === "string" && item.regionCode.length ? item.regionCode : null,
            postalCode: typeof item.postalCode === "string" && item.postalCode.length ? item.postalCode : null,
            city: typeof item.city === "string" && item.city.length ? item.city : null,
            updatedAt: typeof item.updatedAt === "string" ? item.updatedAt : null
          };
        })
      );
    } catch (err) {
      logger.error("sales.tax-rates.list failed", { err });
      flash(translations.errors.load, "error");
    } finally {
      setLoading(false);
    }
  }, [translations.errors.load]);
  React.useEffect(() => {
    void loadEntries();
  }, [loadEntries, scopeVersion]);
  const openCreate = React.useCallback(() => {
    setDialog({ mode: "create" });
  }, []);
  const openEdit = React.useCallback((entry) => {
    setDialog({ mode: "edit", entry });
  }, []);
  const closeDialog = React.useCallback(() => {
    setDialog(null);
  }, []);
  const handleSubmit = React.useCallback(async (values) => {
    if (!dialog) return;
    const payload = {
      name: values.name.trim(),
      code: values.code.trim().toLowerCase(),
      rate: Number(values.rate),
      countryCode: values.countryCode?.trim() ? values.countryCode.trim().toUpperCase() : void 0,
      regionCode: values.regionCode?.trim() || void 0,
      postalCode: values.postalCode?.trim() || void 0,
      city: values.city?.trim() || void 0,
      isDefault: Boolean(values.isDefault)
    };
    const method = dialog.mode === "create" ? "POST" : "PUT";
    if (dialog.mode === "edit") {
      payload.id = dialog.entry.id;
    }
    try {
      const saveTaxRate = () => apiCall("/api/sales/tax-rates", {
        method,
        headers: { "content-type": "application/json" },
        body: JSON.stringify(payload)
      });
      const headers = buildOptimisticLockHeader(dialog.mode === "edit" ? dialog.entry.updatedAt : null);
      const call = await withScopedApiRequestHeaders(headers, saveTaxRate);
      if (!call.ok) {
        if (surfaceRecordConflict({ status: call.status, body: call.result }, t)) return;
        await raiseCrudError(call.response, translations.errors.save);
        return;
      }
      flash(translations.messages.saved, "success");
      closeDialog();
      await loadEntries();
    } catch (err) {
      logger.error("sales.tax-rates.save failed", { err });
      flash(translations.errors.save, "error");
    }
  }, [dialog, translations.errors.save, translations.messages.saved, closeDialog, loadEntries]);
  const deleteEntry = React.useCallback(async (entry) => {
    const message = translations.actions.deleteConfirm.replace("{{name}}", entry.name || entry.code || entry.id);
    const confirmed = await confirm({
      title: message,
      variant: "destructive"
    });
    if (!confirmed) return;
    try {
      const headers = buildOptimisticLockHeader(entry.updatedAt);
      const call = await withScopedApiRequestHeaders(headers, () => apiCall("/api/sales/tax-rates", {
        method: "DELETE",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ id: entry.id })
      }));
      if (!call.ok) {
        if (surfaceRecordConflict({ status: call.status, body: call.result }, t)) return;
        await raiseCrudError(call.response, translations.errors.delete);
        return;
      }
      flash(translations.messages.deleted, "success");
      await loadEntries();
    } catch (err) {
      logger.error("sales.tax-rates.delete failed", { err });
      flash(translations.errors.delete, "error");
    }
  }, [confirm, translations.actions.deleteConfirm, translations.errors.delete, translations.messages.deleted, loadEntries]);
  const columns = React.useMemo(() => [
    {
      header: translations.table.name,
      accessorKey: "name",
      cell: ({ row }) => /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
        /* @__PURE__ */ jsx("span", { className: "font-medium", children: row.original.name }),
        row.original.isDefault ? /* @__PURE__ */ jsx(Badge, { variant: "outline", className: "text-xs uppercase tracking-wide", children: translations.table.defaultBadge }) : null
      ] })
    },
    {
      header: translations.table.code,
      accessorKey: "code",
      cell: ({ row }) => row.original.code ?? "\u2014"
    },
    {
      header: translations.table.rate,
      accessorKey: "rate",
      cell: ({ row }) => typeof row.original.rate === "number" ? `${row.original.rate}%` : "\u2014"
    },
    {
      header: translations.table.location,
      cell: ({ row }) => formatLocation(row.original)
    },
    {
      header: translations.table.updatedAt,
      accessorKey: "updatedAt",
      cell: ({ row }) => row.original.updatedAt ? formatDateTime(row.original.updatedAt) : "\u2014"
    }
  ], [translations.table]);
  const dialogValues = React.useMemo(() => {
    if (!dialog || dialog.mode === "create") return { ...DEFAULT_FORM_VALUES };
    return {
      name: dialog.entry.name ?? "",
      code: dialog.entry.code ?? "",
      rate: dialog.entry.rate ?? 0,
      countryCode: dialog.entry.countryCode ?? "",
      regionCode: dialog.entry.regionCode ?? "",
      postalCode: dialog.entry.postalCode ?? "",
      city: dialog.entry.city ?? "",
      isDefault: Boolean(dialog.entry.isDefault)
    };
  }, [dialog]);
  const filteredEntries = React.useMemo(() => {
    const term = search.trim().toLowerCase();
    if (!term) return entries;
    return entries.filter((entry) => {
      const location = formatLocation(entry).toLowerCase();
      return entry.name.toLowerCase().includes(term) || (entry.code ?? "").toLowerCase().includes(term) || location.includes(term);
    });
  }, [entries, search]);
  return /* @__PURE__ */ jsxs("section", { className: "rounded border bg-card text-card-foreground shadow-sm", children: [
    /* @__PURE__ */ jsxs("div", { className: "border-b px-6 py-4 space-y-1", children: [
      /* @__PURE__ */ jsx("h2", { className: "text-lg font-medium", children: translations.title }),
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: translations.description })
    ] }),
    /* @__PURE__ */ jsx("div", { className: "px-2 py-4 sm:px-4", children: /* @__PURE__ */ jsx(
      DataTable,
      {
        data: filteredEntries,
        columns,
        isLoading: loading,
        embedded: true,
        searchValue: search,
        onSearchChange: setSearch,
        searchPlaceholder: translations.table.search,
        emptyState: /* @__PURE__ */ jsx("p", { className: "py-8 text-center text-sm text-muted-foreground", children: translations.table.empty }),
        actions: /* @__PURE__ */ jsx(Button, { onClick: openCreate, size: "sm", children: translations.actions.add }),
        refreshButton: {
          label: translations.actions.refresh,
          onRefresh: () => {
            void loadEntries();
          },
          isRefreshing: loading
        },
        rowActions: (row) => /* @__PURE__ */ jsx(
          RowActions,
          {
            items: [
              { id: "edit", label: translations.actions.edit, onSelect: () => openEdit(row) },
              { id: "delete", label: translations.actions.delete, destructive: true, onSelect: () => deleteEntry(row) }
            ]
          }
        )
      }
    ) }),
    /* @__PURE__ */ jsx(Dialog, { open: Boolean(dialog), onOpenChange: (open) => {
      if (!open) closeDialog();
    }, children: /* @__PURE__ */ jsxs(DialogContent, { className: "max-h-[90vh] overflow-y-auto sm:max-w-md", children: [
      /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsx(DialogTitle, { children: dialog?.mode === "edit" ? translations.form.editTitle : translations.form.createTitle }) }),
      /* @__PURE__ */ jsx(
        CrudForm,
        {
          schema: taxRateFormSchema,
          fields,
          initialValues: dialogValues,
          optimisticLockUpdatedAt: dialog?.mode === "edit" ? dialog.entry.updatedAt : null,
          submitLabel: translations.form.save,
          cancelHref: void 0,
          embedded: true,
          onSubmit: handleSubmit,
          extraActions: /* @__PURE__ */ jsx(Button, { type: "button", variant: "ghost", onClick: closeDialog, children: translations.form.cancel })
        }
      )
    ] }) }),
    ConfirmDialogElement
  ] });
}
function formatLocation(entry) {
  const parts = [];
  if (entry.city) parts.push(entry.city);
  if (entry.regionCode) parts.push(entry.regionCode);
  if (entry.postalCode) parts.push(entry.postalCode);
  if (entry.countryCode) parts.push(entry.countryCode.toUpperCase());
  return parts.length ? parts.join(", ") : "\u2014";
}
export {
  TaxRatesSettings
};
//# sourceMappingURL=TaxRatesSettings.js.map
