"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { Button } from "@open-mercato/ui/primitives/button";
import { Checkbox } from "@open-mercato/ui/primitives/checkbox";
import { Input } from "@open-mercato/ui/primitives/input";
import { Label } from "@open-mercato/ui/primitives/label";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "@open-mercato/ui/primitives/select";
import { Spinner } from "@open-mercato/ui/primitives/spinner";
import { ArrowDown, ArrowUp, Plus, Trash2 } from "lucide-react";
import { createProductUnitConversionDraft } from "./productForm.js";
import { REFERENCE_UNIT_CODES } from "@open-mercato/shared/lib/units/unitCodes";
import { toTrimmedOrNull } from "./productFormUtils.js";
import { useUnitPriceDisplayEnabled } from "./hooks/useUnitPriceDisplayEnabled.js";
const REFERENCE_UNIT_DISPLAY = {
  kg: "1 kg",
  l: "1 l",
  m2: "1 m\xB2",
  m3: "1 m\xB3",
  pc: "1 pc"
};
const REFERENCE_UNIT_OPTIONS = REFERENCE_UNIT_CODES.map((code) => ({
  value: code,
  i18nKey: `catalog.products.unitPrice.options.${code}`,
  fallback: REFERENCE_UNIT_DISPLAY[code] ?? code
}));
function normalizeDecimalInput(value) {
  return value.replace(/,/g, ".");
}
function toPositiveNumber(value) {
  if (typeof value === "number") {
    return Number.isFinite(value) && value > 0 ? value : null;
  }
  if (typeof value !== "string") return null;
  const normalized = toTrimmedOrNull(value);
  if (!normalized) return null;
  const numeric = Number(normalized.replace(",", "."));
  return Number.isFinite(numeric) && numeric > 0 ? numeric : null;
}
function toSortValue(value) {
  const numeric = Number(value);
  return Number.isFinite(numeric) ? numeric : Number.MAX_SAFE_INTEGER;
}
function formatPreviewNumber(value) {
  if (!Number.isFinite(value)) return "0";
  const rounded = Math.round(value * 1e6) / 1e6;
  return Number.isInteger(rounded) ? String(rounded) : rounded.toString();
}
function normalizeConversions(value) {
  if (!Array.isArray(value)) return [];
  const normalized = value.map((entry) => {
    if (!entry || typeof entry !== "object") return null;
    const row = entry;
    return {
      id: toTrimmedOrNull(row.id) ?? null,
      unitCode: toTrimmedOrNull(row.unitCode) ?? "",
      toBaseFactor: toTrimmedOrNull(row.toBaseFactor) ? normalizeDecimalInput(toTrimmedOrNull(row.toBaseFactor)) : "",
      sortOrder: toTrimmedOrNull(row.sortOrder) ?? "",
      isActive: row.isActive !== false
    };
  }).filter((entry) => Boolean(entry));
  normalized.sort((left, right) => {
    const leftOrder = toSortValue(left.sortOrder);
    const rightOrder = toSortValue(right.sortOrder);
    if (leftOrder === rightOrder) return left.unitCode.localeCompare(right.unitCode);
    return leftOrder - rightOrder;
  });
  return normalized.map((entry, index) => ({
    ...entry,
    sortOrder: String((index + 1) * 10)
  }));
}
function buildUnitOptions(entries) {
  const list = Array.isArray(entries) ? entries : [];
  const options = list.map((entry) => {
    const value = toTrimmedOrNull(entry.value);
    if (!value) return null;
    return {
      value,
      label: toTrimmedOrNull(entry.label) ?? value
    };
  }).filter((entry) => Boolean(entry));
  return options.sort((left, right) => left.label.localeCompare(right.label));
}
function ProductUomSection({
  values,
  errors,
  setValue,
  embedded = false
}) {
  const t = useT();
  const { enabled: unitPriceDisplayEnabled } = useUnitPriceDisplayEnabled();
  const [unitOptions, setUnitOptions] = React.useState([]);
  const [loadingUnits, setLoadingUnits] = React.useState(false);
  const [errorLoadingUnits, setErrorLoadingUnits] = React.useState(false);
  const conversions = React.useMemo(
    () => normalizeConversions(values.unitConversions),
    [values.unitConversions]
  );
  React.useEffect(() => {
    let cancelled = false;
    async function loadUnits() {
      setLoadingUnits(true);
      setErrorLoadingUnits(false);
      try {
        const response = await apiCall(
          "/api/catalog/dictionaries/unit",
          void 0,
          { fallback: { entries: [] } }
        );
        if (cancelled) return;
        setUnitOptions(buildUnitOptions(response.result?.entries));
      } catch {
        if (!cancelled) {
          setUnitOptions([]);
          setErrorLoadingUnits(true);
        }
      } finally {
        if (!cancelled) setLoadingUnits(false);
      }
    }
    void loadUnits();
    return () => {
      cancelled = true;
    };
  }, []);
  const findUnitLabel = React.useCallback(
    (value) => {
      const code = toTrimmedOrNull(value);
      if (!code) return null;
      const option = unitOptions.find((entry) => entry.value === code);
      return option?.label ?? code;
    },
    [unitOptions]
  );
  const setConversions = React.useCallback(
    (next) => {
      const normalized = next.map((entry, index) => ({
        ...entry,
        sortOrder: String((index + 1) * 10)
      }));
      setValue("unitConversions", normalized);
    },
    [setValue]
  );
  const addConversion = React.useCallback(() => {
    const next = [
      ...conversions,
      createProductUnitConversionDraft({
        sortOrder: String((conversions.length + 1) * 10)
      })
    ];
    setConversions(next);
  }, [conversions, setConversions]);
  const updateConversion = React.useCallback(
    (index, patch) => {
      const next = conversions.map(
        (entry, rowIndex) => rowIndex === index ? { ...entry, ...patch } : entry
      );
      setConversions(next);
    },
    [conversions, setConversions]
  );
  const removeConversion = React.useCallback(
    (index) => {
      const next = conversions.filter((_entry, rowIndex) => rowIndex !== index);
      setConversions(next);
    },
    [conversions, setConversions]
  );
  const moveConversion = React.useCallback(
    (index, direction) => {
      const targetIndex = direction === "up" ? index - 1 : index + 1;
      if (targetIndex < 0 || targetIndex >= conversions.length) return;
      const next = [...conversions];
      const source = next[index];
      next[index] = next[targetIndex];
      next[targetIndex] = source;
      setConversions(next);
    },
    [conversions, setConversions]
  );
  const defaultUnit = toTrimmedOrNull(values.defaultUnit) ?? "";
  const defaultSalesUnit = toTrimmedOrNull(values.defaultSalesUnit) ?? "";
  const defaultSalesQuantityRaw = toTrimmedOrNull(values.defaultSalesUnitQuantity) ?? "1";
  const defaultSalesQuantity = normalizeDecimalInput(defaultSalesQuantityRaw);
  const unitPriceEnabled = Boolean(values.unitPriceEnabled);
  const unitPriceReferenceUnit = toTrimmedOrNull(values.unitPriceReferenceUnit) ?? "";
  const unitPriceBaseQuantityRaw = toTrimmedOrNull(values.unitPriceBaseQuantity) ?? "";
  const unitPriceBaseQuantity = normalizeDecimalInput(unitPriceBaseQuantityRaw);
  const baseUnitLabel = findUnitLabel(defaultUnit) ?? defaultUnit;
  const salesUnitLabel = findUnitLabel(defaultSalesUnit || defaultUnit) ?? defaultSalesUnit ?? defaultUnit;
  const defaultSalesFactor = React.useMemo(() => {
    const defaultUnitKey = defaultUnit.toLowerCase();
    const defaultSalesKey = (defaultSalesUnit || defaultUnit).toLowerCase();
    if (!defaultUnitKey || !defaultSalesKey) return null;
    if (defaultSalesKey === defaultUnitKey) return 1;
    const row = conversions.find(
      (entry) => entry.isActive && entry.unitCode.toLowerCase() === defaultSalesKey && toPositiveNumber(entry.toBaseFactor) !== null
    );
    return row ? toPositiveNumber(row.toBaseFactor) : null;
  }, [conversions, defaultSalesUnit, defaultUnit]);
  const defaultSalesQuantityNumber = toPositiveNumber(defaultSalesQuantity);
  const defaultSalesQuantityNormalized = defaultSalesQuantityNumber && defaultSalesFactor ? defaultSalesQuantityNumber * defaultSalesFactor : null;
  const unitPriceBaseQuantityNumber = toPositiveNumber(unitPriceBaseQuantity);
  const validConversions = conversions.filter(
    (entry) => toTrimmedOrNull(entry.unitCode) && toTrimmedOrNull(entry.toBaseFactor)
  );
  const conversionPreviewItems = validConversions.slice(0, 3).map((entry) => {
    const label = findUnitLabel(entry.unitCode) ?? entry.unitCode;
    const baseLabel = findUnitLabel(defaultUnit) ?? defaultUnit;
    const factor = toTrimmedOrNull(entry.toBaseFactor) ?? "1";
    return `1 ${label} = ${factor} ${baseLabel || t("catalog.products.uom.baseUnit", "base unit")}`;
  });
  const conversionPreview = validConversions.length > 3 ? `${conversionPreviewItems.join(" \u2022 ")} (+${validConversions.length - 3})` : conversionPreviewItems.join(" \u2022 ");
  return /* @__PURE__ */ jsxs(
    "div",
    {
      className: embedded ? "space-y-5" : "space-y-5 rounded-lg border bg-card p-4",
      children: [
        /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
          /* @__PURE__ */ jsx("h3", { className: "text-sm font-semibold", children: t("catalog.products.uom.title", "Units of measure") }),
          /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
            "catalog.products.uom.description",
            "Set base unit, sales unit, and packaging conversions."
          ) })
        ] }),
        loadingUnits ? /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 text-xs text-muted-foreground", children: [
          /* @__PURE__ */ jsx(Spinner, { className: "h-3 w-3" }),
          t("catalog.products.uom.loadingUnits", "Loading units...")
        ] }) : null,
        errorLoadingUnits ? /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: t(
          "catalog.products.uom.errors.loadUnits",
          "Failed to load units. Please try refreshing the page."
        ) }) : null,
        /* @__PURE__ */ jsxs("div", { className: "grid gap-4 md:grid-cols-2", children: [
          /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
            /* @__PURE__ */ jsx(Label, { htmlFor: "catalog-product-uom-base-unit", children: t("catalog.products.uom.baseUnit", "Base unit") }),
            /* @__PURE__ */ jsxs(
              Select,
              {
                value: defaultUnit || void 0,
                onValueChange: (value) => setValue("defaultUnit", value || null),
                disabled: loadingUnits,
                children: [
                  /* @__PURE__ */ jsx(SelectTrigger, { id: "catalog-product-uom-base-unit", children: /* @__PURE__ */ jsx(SelectValue, { placeholder: t("catalog.products.uom.selectUnit", "Select unit") }) }),
                  /* @__PURE__ */ jsx(SelectContent, { children: unitOptions.map((option) => /* @__PURE__ */ jsx(SelectItem, { value: option.value, children: option.label }, option.value)) })
                ]
              }
            ),
            errors.defaultUnit ? /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: errors.defaultUnit }) : null
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
            /* @__PURE__ */ jsx(Label, { htmlFor: "catalog-product-uom-sales-unit", children: t("catalog.products.uom.defaultSalesUnit", "Default sales unit") }),
            /* @__PURE__ */ jsxs(
              Select,
              {
                value: defaultSalesUnit || void 0,
                onValueChange: (value) => setValue("defaultSalesUnit", value || null),
                disabled: loadingUnits,
                children: [
                  /* @__PURE__ */ jsx(SelectTrigger, { id: "catalog-product-uom-sales-unit", children: /* @__PURE__ */ jsx(SelectValue, { placeholder: t("catalog.products.uom.selectUnit", "Select unit") }) }),
                  /* @__PURE__ */ jsx(SelectContent, { children: unitOptions.map((option) => /* @__PURE__ */ jsx(SelectItem, { value: option.value, children: option.label }, option.value)) })
                ]
              }
            ),
            errors.defaultSalesUnit ? /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: errors.defaultSalesUnit }) : null
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "space-y-2 md:col-span-2", children: [
            /* @__PURE__ */ jsx(Label, { htmlFor: "catalog-product-uom-default-sales-quantity", children: t(
              "catalog.products.uom.defaultSalesQuantityLabel",
              "Default line quantity (in sales unit)"
            ) }),
            /* @__PURE__ */ jsx(
              Input,
              {
                id: "catalog-product-uom-default-sales-quantity",
                type: "text",
                inputMode: "decimal",
                value: defaultSalesQuantity,
                onChange: (event) => setValue(
                  "defaultSalesUnitQuantity",
                  normalizeDecimalInput(event.target.value)
                ),
                placeholder: "1"
              }
            ),
            /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
              "catalog.products.uom.defaultSalesQuantityHint",
              "Used to prefill quantity in quote/order lines. Value is interpreted in Default sales unit."
            ) }),
            defaultSalesQuantityNumber && salesUnitLabel ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: defaultSalesQuantityNormalized && baseUnitLabel ? t(
              "catalog.products.uom.defaultSalesQuantityPreviewWithNormalization",
              "Default line: {{quantity}} {{salesUnit}} (= {{normalized}} {{baseUnit}}).",
              {
                quantity: formatPreviewNumber(defaultSalesQuantityNumber),
                salesUnit: salesUnitLabel,
                normalized: formatPreviewNumber(
                  defaultSalesQuantityNormalized
                ),
                baseUnit: baseUnitLabel
              }
            ) : t(
              "catalog.products.uom.defaultSalesQuantityPreview",
              "Default line: {{quantity}} {{salesUnit}}.",
              {
                quantity: formatPreviewNumber(defaultSalesQuantityNumber),
                salesUnit: salesUnitLabel
              }
            ) }) : null,
            errors.defaultSalesUnitQuantity ? /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: errors.defaultSalesUnitQuantity }) : null
          ] })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "grid gap-4 md:grid-cols-2", children: [
          /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
            /* @__PURE__ */ jsx(Label, { htmlFor: "catalog-product-uom-rounding-mode", children: t("catalog.products.uom.roundingMode", "Rounding mode") }),
            /* @__PURE__ */ jsxs(
              Select,
              {
                value: values.uomRoundingMode ?? "half_up",
                onValueChange: (value) => setValue("uomRoundingMode", value),
                children: [
                  /* @__PURE__ */ jsx(SelectTrigger, { id: "catalog-product-uom-rounding-mode", children: /* @__PURE__ */ jsx(SelectValue, {}) }),
                  /* @__PURE__ */ jsxs(SelectContent, { children: [
                    /* @__PURE__ */ jsx(SelectItem, { value: "half_up", children: t("catalog.products.uom.roundingModeHalfUp", "Half up (default)") }),
                    /* @__PURE__ */ jsx(SelectItem, { value: "down", children: t("catalog.products.uom.roundingModeDown", "Round down") }),
                    /* @__PURE__ */ jsx(SelectItem, { value: "up", children: t("catalog.products.uom.roundingModeUp", "Round up") })
                  ] })
                ]
              }
            )
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
            /* @__PURE__ */ jsx(Label, { htmlFor: "catalog-product-uom-rounding-scale", children: t("catalog.products.uom.roundingScale", "Rounding scale (decimal places)") }),
            /* @__PURE__ */ jsx(
              Input,
              {
                id: "catalog-product-uom-rounding-scale",
                type: "number",
                min: 0,
                max: 6,
                value: values.uomRoundingScale ?? 4,
                onChange: (event) => {
                  const parsed = Number.parseInt(event.target.value, 10);
                  setValue("uomRoundingScale", Number.isFinite(parsed) ? Math.max(0, Math.min(6, parsed)) : 4);
                }
              }
            )
          ] })
        ] }),
        unitPriceDisplayEnabled ? /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
            /* @__PURE__ */ jsx(
              Checkbox,
              {
                id: "catalog-product-unit-price-enabled",
                checked: unitPriceEnabled,
                onCheckedChange: (checked) => setValue("unitPriceEnabled", checked === true)
              }
            ),
            /* @__PURE__ */ jsx(
              Label,
              {
                htmlFor: "catalog-product-unit-price-enabled",
                className: "text-sm",
                children: t(
                  "catalog.products.unitPrice.enable",
                  "Enable EU unit price display"
                )
              }
            )
          ] }),
          unitPriceEnabled ? /* @__PURE__ */ jsxs("div", { className: "grid gap-3 md:grid-cols-2", children: [
            /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
              /* @__PURE__ */ jsx(Label, { htmlFor: "catalog-product-uom-reference-unit", children: t(
                "catalog.products.unitPrice.referenceUnit",
                "Reference unit"
              ) }),
              /* @__PURE__ */ jsxs(
                Select,
                {
                  value: unitPriceReferenceUnit || void 0,
                  onValueChange: (value) => setValue("unitPriceReferenceUnit", value || null),
                  children: [
                    /* @__PURE__ */ jsx(SelectTrigger, { id: "catalog-product-uom-reference-unit", children: /* @__PURE__ */ jsx(
                      SelectValue,
                      {
                        placeholder: t(
                          "catalog.products.unitPrice.selectReferenceUnit",
                          "Select reference unit"
                        )
                      }
                    ) }),
                    /* @__PURE__ */ jsx(SelectContent, { children: REFERENCE_UNIT_OPTIONS.map((option) => /* @__PURE__ */ jsx(SelectItem, { value: option.value, children: t(option.i18nKey, option.fallback) }, option.value)) })
                  ]
                }
              )
            ] }),
            /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
              /* @__PURE__ */ jsx(Label, { htmlFor: "catalog-product-uom-unit-price-base-quantity", children: t(
                "catalog.products.unitPrice.baseQuantity",
                "Reference quantity (in base unit)"
              ) }),
              /* @__PURE__ */ jsx(
                Input,
                {
                  id: "catalog-product-uom-unit-price-base-quantity",
                  type: "text",
                  inputMode: "decimal",
                  value: unitPriceBaseQuantity,
                  onChange: (event) => setValue(
                    "unitPriceBaseQuantity",
                    normalizeDecimalInput(event.target.value)
                  ),
                  placeholder: "1"
                }
              )
            ] })
          ] }) : null,
          unitPriceEnabled ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: unitPriceReferenceUnit && unitPriceBaseQuantityNumber ? t(
            "catalog.products.unitPrice.hintWithPreview",
            "Show calculated price per {{quantity}} {{unit}}. For most products use 1 (for example: 1 kg, 1 l, 1 m\xB2).",
            {
              quantity: formatPreviewNumber(unitPriceBaseQuantityNumber),
              unit: unitPriceReferenceUnit
            }
          ) : t(
            "catalog.products.unitPrice.hint",
            "Show calculated price per selected reference unit. In most cases set quantity to 1."
          ) }) : null
        ] }) : null,
        /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
          /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
            /* @__PURE__ */ jsx(Label, { className: "text-sm", children: t("catalog.products.uom.conversions", "Product conversions") }),
            /* @__PURE__ */ jsxs(
              Button,
              {
                type: "button",
                variant: "outline",
                size: "sm",
                onClick: addConversion,
                children: [
                  /* @__PURE__ */ jsx(Plus, { className: "mr-1.5 h-4 w-4" }),
                  t("catalog.products.uom.addConversion", "Add conversion")
                ]
              }
            )
          ] }),
          conversions.length === 0 ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
            "catalog.products.uom.emptyConversions",
            "No conversions configured yet."
          ) }) : /* @__PURE__ */ jsx("div", { className: "space-y-2", children: conversions.map((entry, index) => {
            const conversionFactor = toPositiveNumber(entry.toBaseFactor);
            const conversionPreviewText = entry.unitCode && conversionFactor !== null ? t(
              "catalog.products.uom.conversionPreview",
              "1 {{fromUnit}} = {{factor}} {{baseUnit}}",
              {
                fromUnit: findUnitLabel(entry.unitCode) ?? entry.unitCode,
                factor: formatPreviewNumber(conversionFactor),
                baseUnit: findUnitLabel(defaultUnit) ?? defaultUnit ?? t("catalog.products.uom.baseUnit", "base unit")
              }
            ) : null;
            return /* @__PURE__ */ jsxs(
              "div",
              {
                className: "grid gap-3 rounded-md border p-3 md:grid-cols-12",
                children: [
                  /* @__PURE__ */ jsxs("div", { className: "space-y-1 md:col-span-4", children: [
                    /* @__PURE__ */ jsx(Label, { htmlFor: `catalog-product-uom-conversion-unit-${index}`, className: "text-xs text-muted-foreground", children: t("catalog.products.uom.conversionUnit", "Sales unit") }),
                    /* @__PURE__ */ jsxs(
                      Select,
                      {
                        value: entry.unitCode || void 0,
                        onValueChange: (value) => updateConversion(index, { unitCode: value ?? "" }),
                        disabled: loadingUnits,
                        children: [
                          /* @__PURE__ */ jsx(SelectTrigger, { id: `catalog-product-uom-conversion-unit-${index}`, children: /* @__PURE__ */ jsx(SelectValue, { placeholder: t("catalog.products.uom.selectUnit", "Select unit") }) }),
                          /* @__PURE__ */ jsx(SelectContent, { children: unitOptions.map((option) => /* @__PURE__ */ jsx(SelectItem, { value: option.value, children: option.label }, option.value)) })
                        ]
                      }
                    )
                  ] }),
                  /* @__PURE__ */ jsxs("div", { className: "space-y-1 md:col-span-3", children: [
                    /* @__PURE__ */ jsx(Label, { htmlFor: `catalog-product-uom-conversion-factor-${index}`, className: "text-xs text-muted-foreground", children: t(
                      "catalog.products.uom.toBaseFactor",
                      "Base units per 1 sales unit"
                    ) }),
                    /* @__PURE__ */ jsx(
                      Input,
                      {
                        id: `catalog-product-uom-conversion-factor-${index}`,
                        type: "text",
                        inputMode: "decimal",
                        value: entry.toBaseFactor,
                        onChange: (event) => updateConversion(index, {
                          toBaseFactor: normalizeDecimalInput(event.target.value)
                        }),
                        placeholder: "1"
                      }
                    )
                  ] }),
                  /* @__PURE__ */ jsxs("div", { className: "flex items-end gap-2 md:col-span-3", children: [
                    /* @__PURE__ */ jsxs("div", { className: "inline-flex h-9 rounded-md border", children: [
                      /* @__PURE__ */ jsx(
                        Button,
                        {
                          type: "button",
                          variant: "ghost",
                          size: "icon",
                          className: "h-9 w-9 rounded-none border-r",
                          onClick: () => moveConversion(index, "up"),
                          disabled: index === 0,
                          "aria-label": t(
                            "catalog.products.uom.moveUp",
                            "Move conversion up"
                          ),
                          children: /* @__PURE__ */ jsx(ArrowUp, { className: "h-4 w-4" })
                        }
                      ),
                      /* @__PURE__ */ jsx(
                        Button,
                        {
                          type: "button",
                          variant: "ghost",
                          size: "icon",
                          className: "h-9 w-9 rounded-none",
                          onClick: () => moveConversion(index, "down"),
                          disabled: index === conversions.length - 1,
                          "aria-label": t(
                            "catalog.products.uom.moveDown",
                            "Move conversion down"
                          ),
                          children: /* @__PURE__ */ jsx(ArrowDown, { className: "h-4 w-4" })
                        }
                      )
                    ] }),
                    /* @__PURE__ */ jsxs(
                      "label",
                      {
                        htmlFor: `catalog-product-uom-conversion-active-${index}`,
                        className: "flex items-center gap-2 px-1 text-xs text-muted-foreground",
                        children: [
                          /* @__PURE__ */ jsx(
                            Checkbox,
                            {
                              id: `catalog-product-uom-conversion-active-${index}`,
                              checked: entry.isActive,
                              onCheckedChange: (checked) => updateConversion(index, { isActive: checked === true })
                            }
                          ),
                          t("catalog.products.uom.active", "Active")
                        ]
                      }
                    )
                  ] }),
                  /* @__PURE__ */ jsx("div", { className: "flex items-end justify-end md:col-span-2", children: /* @__PURE__ */ jsx(
                    Button,
                    {
                      type: "button",
                      variant: "ghost",
                      size: "icon",
                      className: "text-destructive",
                      onClick: () => removeConversion(index),
                      "aria-label": t(
                        "catalog.products.uom.removeConversion",
                        "Remove conversion"
                      ),
                      children: /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" })
                    }
                  ) }),
                  conversionPreviewText && /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground md:col-span-12", children: conversionPreviewText })
                ]
              },
              entry.id ?? `uom-conversion-${index}`
            );
          }) }),
          conversions.length > 1 ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
            "catalog.products.uom.conversionOrderHint",
            "Use arrows to reorder conversion priority."
          ) }) : null,
          conversionPreview ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: conversionPreview }) : null
        ] })
      ]
    }
  );
}
export {
  ProductUomSection
};
//# sourceMappingURL=ProductUomSection.js.map
