"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { cn } from "@open-mercato/shared/lib/utils";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { DictionaryEntrySelect } from "@open-mercato/core/modules/dictionaries/components/DictionaryEntrySelect";
import { useCurrencyDictionary } from "../hooks/useCurrencyDictionary.js";
const CURRENCY_PRIORITY = ["EUR", "USD", "GBP", "PLN"];
function DealCurrencyField({ id, value, onChange, disabled = false }) {
  const t = useT();
  const { data, error: rawError, isLoading, refetch } = useCurrencyDictionary();
  const dictError = rawError ? rawError instanceof Error ? rawError.message : String(rawError) : null;
  const resolvedError = React.useMemo(() => {
    if (dictError) return dictError;
    if (!isLoading && !data) {
      return t("customers.deals.form.currency.missing", "Currency dictionary is not configured yet.");
    }
    return null;
  }, [data, dictError, isLoading, t]);
  const fetchOptions = React.useCallback(async () => {
    let payload = data ?? null;
    if (!payload) {
      try {
        payload = await refetch();
      } catch (err) {
        const message = err instanceof Error ? err.message : String(err ?? "");
        throw new Error(message || t("customers.deals.form.currency.error", "Failed to load currency dictionary."));
      }
    }
    if (!payload) {
      throw new Error(t("customers.deals.form.currency.missing", "Currency dictionary is not configured yet."));
    }
    const priorityOrder = /* @__PURE__ */ new Map();
    CURRENCY_PRIORITY.forEach((code, index) => priorityOrder.set(code, index));
    const prioritized = [];
    const remainder = [];
    payload.entries.forEach((entry) => {
      const code = entry.value.toUpperCase();
      const label = entry.label && entry.label.length ? `${code} \u2013 ${entry.label}` : code;
      const option = { value: code, label, color: null, icon: null };
      if (priorityOrder.has(code)) prioritized.push(option);
      else remainder.push(option);
    });
    prioritized.sort((a, b) => priorityOrder.get(a.value) - priorityOrder.get(b.value));
    remainder.sort((a, b) => a.label.localeCompare(b.label, void 0, { sensitivity: "base" }));
    return [...prioritized, ...remainder];
  }, [data, refetch, t]);
  const labels = React.useMemo(
    () => ({
      placeholder: t("customers.deals.form.currency.placeholder", "Select currency\u2026"),
      addLabel: t("customers.deals.form.currency.add", "Add currency"),
      dialogTitle: t("customers.deals.form.currency.dialogTitle", "Add currency"),
      valueLabel: t("customers.deals.form.currency.valueLabel", "Currency code"),
      valuePlaceholder: t("customers.deals.form.currency.valuePlaceholder", "e.g. USD"),
      labelLabel: t("customers.deals.form.currency.labelLabel", "Label"),
      labelPlaceholder: t("customers.deals.form.currency.labelPlaceholder", "Display name shown in UI"),
      emptyError: t("customers.deals.form.currency.error.required", "Currency code is required."),
      cancelLabel: t("customers.deals.form.currency.cancel", "Cancel"),
      saveLabel: t("customers.deals.form.currency.save", "Save"),
      errorLoad: t("customers.deals.form.currency.error", "Failed to load currency dictionary."),
      errorSave: t("customers.deals.form.currency.error", "Failed to load currency dictionary."),
      loadingLabel: t("customers.deals.form.currency.loading", "Loading currencies\u2026"),
      manageTitle: t("customers.deals.form.currency.manage", "Manage currency dictionary")
    }),
    [t]
  );
  return /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
    /* @__PURE__ */ jsx(
      DictionaryEntrySelect,
      {
        id,
        value: value || void 0,
        onChange: (next) => onChange(next ?? ""),
        fetchOptions,
        labels,
        manageHref: "/backend/config/dictionaries?key=currency",
        allowInlineCreate: false,
        allowAppearance: false,
        sortOptions: "none",
        selectClassName: "w-full",
        disabled,
        showLabelInput: false
      }
    ),
    resolvedError ? /* @__PURE__ */ jsx("p", { className: cn("text-xs", dictError ? "text-status-error-text" : "text-muted-foreground"), children: resolvedError }) : null
  ] });
}
var DealCurrencyField_default = DealCurrencyField;
export {
  DealCurrencyField,
  DealCurrencyField_default as default
};
//# sourceMappingURL=DealCurrencyField.js.map
