"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import {
  LookupSelect
} from "@open-mercato/ui/backend/inputs";
import {
  CrudForm
} from "@open-mercato/ui/backend/CrudForm";
import { collectCustomFieldValues } from "@open-mercato/ui/backend/utils/customFieldValues";
import { apiCall, withScopedApiRequestHeaders } from "@open-mercato/ui/backend/utils/apiCall";
import { buildOptimisticLockHeader } from "@open-mercato/ui/backend/utils/optimisticLock";
import { createCrud, updateCrud } from "@open-mercato/ui/backend/utils/crud";
import { createCrudFormError } from "@open-mercato/ui/backend/utils/serverErrors";
import { handleSectionMutationError } from "./optimisticLock.js";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle
} from "@open-mercato/ui/primitives/dialog";
import { useDialogKeyHandler } from "@open-mercato/ui/hooks/useDialogKeyHandler";
import { Button } from "@open-mercato/ui/primitives/button";
import { Input } from "@open-mercato/ui/primitives/input";
import {
  Alert,
  AlertDescription,
  AlertTitle
} from "@open-mercato/ui/primitives/alert";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "@open-mercato/ui/primitives/select";
import { DollarSign, Settings } from "lucide-react";
import { normalizeCustomFieldValues } from "@open-mercato/shared/lib/custom-fields/normalize";
import {
  renderDictionaryIcon,
  renderDictionaryColor
} from "@open-mercato/core/modules/dictionaries/components/dictionaryAppearance";
import { E } from "../../../../generated/entities.ids.generated.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { useOrganizationScopeDetail } from "@open-mercato/shared/lib/frontend/useOrganizationScope";
import { formatMoney, normalizeNumber } from "./lineItemUtils.js";
import {
  normalizeCustomFieldSubmitValue,
  extractCustomFieldValues
} from "./customFieldHelpers.js";
import { canonicalizeUnitCode } from "@open-mercato/shared/lib/units/unitCodes";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("sales");
function mapTaxRateOption(item) {
  const id = typeof item.id === "string" ? item.id : null;
  const name = typeof item.name === "string" && item.name.trim().length ? item.name.trim() : typeof item.code === "string" ? item.code : null;
  if (!id || !name) return null;
  const rate = normalizeNumber(item.rate);
  const code = typeof item.code === "string" && item.code?.trim().length ? item.code?.trim() ?? null : null;
  const isDefault = Boolean(
    item.isDefault ?? item.is_default
  );
  return {
    id,
    name,
    code,
    rate: Number.isFinite(rate) ? rate : null,
    isDefault
  };
}
function mergeTaxRateOptions(options, selected) {
  if (!selected) return options;
  if (options.some((option) => option.id === selected.id)) return options;
  return [selected, ...options];
}
const defaultForm = (currencyCode) => ({
  lineMode: "catalog",
  productId: null,
  variantId: null,
  quantity: "1",
  quantityUnit: null,
  priceId: null,
  priceMode: "gross",
  unitPrice: "",
  taxRate: null,
  taxRateId: null,
  name: "",
  currencyCode: currencyCode ?? null,
  catalogSnapshot: null,
  customFieldSetId: null,
  statusEntryId: null
});
const UNIT_PRICE_INPUT_SCALE = 4;
function buildPriceScopeReason(item, t) {
  const tags = [];
  const add = (key) => tags.push(key);
  if (item.channel_id || item.channelId)
    add(t("sales.documents.items.priceScope.channel", "Channel"));
  if (item.offer_id || item.offerId)
    add(t("sales.documents.items.priceScope.offer", "Offer"));
  if (item.variant_id || item.variantId)
    add(t("sales.documents.items.priceScope.variant", "Variant"));
  if (item.customer_group_id || item.customerGroupId)
    add(t("sales.documents.items.priceScope.customerGroup", "Customer group"));
  if (item.customer_id || item.customerId)
    add(t("sales.documents.items.priceScope.customer", "Customer"));
  if (item.user_group_id || item.userGroupId)
    add(t("sales.documents.items.priceScope.userGroup", "User group"));
  if (item.user_id || item.userId)
    add(t("sales.documents.items.priceScope.user", "User"));
  const minQty = normalizeNumber(item.min_quantity, Number.NaN);
  const maxQty = normalizeNumber(item.max_quantity, Number.NaN);
  if (Number.isFinite(minQty) || Number.isFinite(maxQty)) {
    add(t("sales.documents.items.priceScope.quantity", "Quantity"));
  }
  if (item.starts_at || item.ends_at) {
    add(t("sales.documents.items.priceScope.schedule", "Scheduled"));
  }
  if (tags.length === 0) return { reason: null, tags };
  return { reason: tags.join(" \u2022 "), tags };
}
function buildPlaceholder(label) {
  return /* @__PURE__ */ jsx("div", { className: "flex h-8 w-8 items-center justify-center rounded border bg-muted text-xs uppercase text-muted-foreground", children: (label ?? "").slice(0, 2) || "\u2022" });
}
function normalizeUnitCode(value) {
  return canonicalizeUnitCode(value);
}
function getRecordBoolean(record, fallback, ...keys) {
  for (const key of keys) {
    const val = record[key];
    if (typeof val === "boolean") return val;
  }
  return fallback;
}
function getUomProductFields(item) {
  return {
    defaultUnit: normalizeUnitCode(item.default_unit ?? item.defaultUnit),
    defaultSalesUnit: normalizeUnitCode(
      item.default_sales_unit ?? item.defaultSalesUnit
    ),
    defaultSalesUnitQuantity: normalizeNumber(
      item.default_sales_unit_quantity ?? item.defaultSalesUnitQuantity,
      Number.NaN
    )
  };
}
function getUomConversionFields(row) {
  return {
    unitCode: normalizeUnitCode(row.unit_code ?? row.unitCode),
    isActive: getRecordBoolean(row, true, "is_active", "isActive"),
    toBaseFactor: normalizeNumber(
      row.to_base_factor ?? row.toBaseFactor,
      Number.NaN
    )
  };
}
function normalizeQuantityPreview(value) {
  if (!Number.isFinite(value)) return 0;
  return Math.round(value * 1e6) / 1e6;
}
function normalizeUnitPriceInputValue(value) {
  if (!Number.isFinite(value)) return "";
  const factor = 10 ** UNIT_PRICE_INPUT_SCALE;
  const rounded = Math.round((value + Number.EPSILON) * factor) / factor;
  if (!Number.isFinite(rounded)) return "";
  return rounded.toString();
}
function mapProductOption(item) {
  const id = typeof item.id === "string" ? item.id : null;
  if (!id) return null;
  const productItem = item;
  const title = typeof item.title === "string" ? item.title : typeof productItem.name === "string" ? productItem.name : id;
  const sku = typeof productItem.sku === "string" ? productItem.sku : null;
  const thumbnail = typeof productItem.default_media_url === "string" ? productItem.default_media_url : typeof productItem.defaultMediaUrl === "string" ? productItem.defaultMediaUrl : null;
  const pricing = typeof productItem.pricing === "object" && productItem.pricing ? productItem.pricing : null;
  const metadata = typeof productItem.metadata === "object" && productItem.metadata ? productItem.metadata : null;
  const pricingMeta = pricing;
  const metaMeta = metadata;
  const pricingTaxRateId = typeof pricingMeta?.tax_rate_id === "string" && pricingMeta.tax_rate_id.trim().length ? pricingMeta.tax_rate_id.trim() : typeof pricingMeta?.taxRateId === "string" && pricingMeta.taxRateId.trim().length ? pricingMeta.taxRateId.trim() : null;
  const metaTaxRateId = typeof metaMeta?.taxRateId === "string" && metaMeta.taxRateId.trim().length ? metaMeta.taxRateId.trim() : typeof metaMeta?.tax_rate_id === "string" && metaMeta.tax_rate_id.trim().length ? metaMeta.tax_rate_id.trim() : null;
  const taxRateValue = normalizeNumber(
    pricingMeta?.tax_rate ?? pricingMeta?.taxRate ?? productItem.tax_rate ?? productItem.taxRate,
    Number.NaN
  );
  const uomFields = getUomProductFields(item);
  return {
    id,
    title,
    sku,
    thumbnailUrl: thumbnail,
    taxRateId: pricingTaxRateId ?? metaTaxRateId ?? null,
    taxRate: Number.isFinite(taxRateValue) ? taxRateValue : null,
    defaultUnit: uomFields.defaultUnit,
    defaultSalesUnit: uomFields.defaultSalesUnit,
    defaultSalesUnitQuantity: Number.isFinite(uomFields.defaultSalesUnitQuantity) ? uomFields.defaultSalesUnitQuantity : null
  };
}
function LineItemDialog({
  open,
  kind,
  documentId,
  currencyCode,
  documentUpdatedAt,
  organizationId,
  tenantId,
  initialLine,
  onOpenChange,
  onSaved
}) {
  const t = useT();
  const scope = useOrganizationScopeDetail();
  const resolvedOrganizationId = organizationId ?? scope.organizationId ?? null;
  const resolvedTenantId = tenantId ?? scope.tenantId ?? null;
  const [initialValues, setInitialValues] = React.useState(
    () => defaultForm(currencyCode)
  );
  const [lineMode, setLineMode] = React.useState(
    defaultForm(currencyCode).lineMode
  );
  const [productOption, setProductOption] = React.useState(null);
  const [variantOption, setVariantOption] = React.useState(null);
  const [priceOptions, setPriceOptions] = React.useState([]);
  const [priceLoading, setPriceLoading] = React.useState(false);
  const [formResetKey, setFormResetKey] = React.useState(0);
  const [editingId, setEditingId] = React.useState(null);
  const [taxRates, setTaxRates] = React.useState([]);
  const [lineStatuses, setLineStatuses] = React.useState([]);
  const [unitOptions, setUnitOptions] = React.useState([]);
  const [deletedCatalogReference, setDeletedCatalogReference] = React.useState(false);
  const [, setLineStatusLoading] = React.useState(false);
  const productOptionsRef = React.useRef(/* @__PURE__ */ new Map());
  const variantOptionsRef = React.useRef(/* @__PURE__ */ new Map());
  const taxRatesRef = React.useRef([]);
  const defaultTaxRateRef = React.useRef(null);
  const dialogContentRef = React.useRef(null);
  const resourcePath = React.useMemo(
    () => kind === "order" ? "sales/order-lines" : "sales/quote-lines",
    [kind]
  );
  const documentKey = kind === "order" ? "orderId" : "quoteId";
  const customFieldEntityId = kind === "order" ? E.sales.sales_order_line : E.sales.sales_quote_line;
  const taxRateMap = React.useMemo(
    () => taxRates.reduce((acc, rate) => {
      acc.set(rate.id, rate);
      return acc;
    }, /* @__PURE__ */ new Map()),
    [taxRates]
  );
  const findTaxRateIdByValue = React.useCallback(
    (value) => {
      const numeric = normalizeNumber(value, Number.NaN);
      if (!Number.isFinite(numeric)) return null;
      const match = taxRatesRef.current.find(
        (rate) => Number.isFinite(rate.rate) && Math.abs(rate.rate - numeric) < 1e-4
      );
      return match?.id ?? null;
    },
    []
  );
  const resolveTaxSelection = React.useCallback(
    (source) => {
      const taxRateId = typeof source?.taxRateId === "string" && source.taxRateId.trim().length ? source.taxRateId.trim() : null;
      const rateFromId = taxRateId ? normalizeNumber(taxRateMap.get(taxRateId)?.rate, Number.NaN) : Number.NaN;
      const numericRate = normalizeNumber(source?.taxRate, Number.NaN);
      const resolvedRateId = taxRateId ?? (Number.isFinite(numericRate) ? findTaxRateIdByValue(numericRate) : null);
      const resolvedRate = Number.isFinite(rateFromId) ? rateFromId : Number.isFinite(numericRate) ? numericRate : null;
      return { taxRateId: resolvedRateId, taxRate: resolvedRate };
    },
    [findTaxRateIdByValue, taxRateMap]
  );
  const hasTaxMetadata = React.useCallback(
    (source) => {
      if (!source) return false;
      const id = typeof source.taxRateId === "string" ? source.taxRateId.trim() : "";
      if (id.length) return true;
      const numericRate = normalizeNumber(source.taxRate, Number.NaN);
      return Number.isFinite(numericRate);
    },
    []
  );
  const resetForm = React.useCallback(
    (next) => {
      const base = { ...defaultForm(currencyCode), ...next };
      const defaultRate = defaultTaxRateRef.current;
      if (!base.taxRateId && defaultRate) {
        base.taxRateId = defaultRate.id;
        base.taxRate = Number.isFinite(defaultRate.rate ?? null) ? defaultRate.rate : base.taxRate;
      }
      setInitialValues(base);
      setLineMode(base.lineMode);
      setProductOption(null);
      setVariantOption(null);
      setPriceOptions([]);
      setUnitOptions([]);
      setDeletedCatalogReference(false);
      setEditingId(null);
      setFormResetKey((prev) => prev + 1);
    },
    [currencyCode]
  );
  const closeDialog = React.useCallback(() => {
    onOpenChange(false);
    resetForm();
  }, [onOpenChange, resetForm]);
  const loadTaxRates = React.useCallback(async () => {
    try {
      const response = await apiCall("/api/sales/tax-rates?pageSize=100", void 0, {
        fallback: { items: [] }
      });
      const items = Array.isArray(response.result?.items) ? response.result.items : [];
      const parsed = items.map((item) => mapTaxRateOption(item)).filter((entry) => Boolean(entry));
      taxRatesRef.current = parsed;
      defaultTaxRateRef.current = parsed.find((rate) => rate.isDefault) ?? null;
      setTaxRates(parsed);
      return parsed;
    } catch (err) {
      logger.error("sales.tax-rates.fetch", { err });
      taxRatesRef.current = [];
      defaultTaxRateRef.current = null;
      setTaxRates([]);
      return [];
    }
  }, []);
  const loadTaxRateById = React.useCallback(
    async (taxRateId) => {
      const response = await apiCall(
        `/api/sales/tax-rates?id=${encodeURIComponent(taxRateId)}&pageSize=1`,
        void 0,
        { fallback: { items: [] } }
      );
      const items = Array.isArray(response.result?.items) ? response.result.items : [];
      return items.map((item) => mapTaxRateOption(item)).find((entry) => entry?.id === taxRateId) ?? null;
    },
    []
  );
  React.useEffect(() => {
    const selectedId = typeof initialValues.taxRateId === "string" && initialValues.taxRateId.trim().length ? initialValues.taxRateId.trim() : null;
    if (!selectedId || taxRates.some((rate) => rate.id === selectedId)) return;
    loadTaxRateById(selectedId).then((selected) => {
      taxRatesRef.current = mergeTaxRateOptions(taxRatesRef.current, selected);
      setTaxRates((current) => mergeTaxRateOptions(current, selected));
    }).catch(() => {
    });
  }, [initialValues.taxRateId, loadTaxRateById, taxRates]);
  const loadProductOptionById = React.useCallback(
    async (productId) => {
      if (!productId) return null;
      const response = await apiCall(
        `/api/catalog/products?id=${encodeURIComponent(productId)}&pageSize=1`,
        void 0,
        { fallback: { items: [] } }
      );
      const items = Array.isArray(response.result?.items) ? response.result.items : [];
      const matched = items.find((entry) => entry.id === productId) ?? items[0] ?? null;
      if (!matched) return null;
      const option = mapProductOption(matched);
      if (option) productOptionsRef.current.set(option.id, option);
      return option;
    },
    []
  );
  const loadProductOptions = React.useCallback(
    async (query) => {
      const params = new URLSearchParams({ pageSize: "8" });
      if (query && query.trim().length) {
        params.set("search", query.trim());
      } else {
        params.set("sortField", "title");
      }
      const response = await apiCall(`/api/catalog/products?${params.toString()}`, void 0, {
        fallback: { items: [] }
      });
      const items = Array.isArray(response.result?.items) ? response.result?.items ?? [] : [];
      const mapped = items.map((item) => {
        const option = mapProductOption(item);
        if (!option) return null;
        return {
          id: option.id,
          title: option.title,
          subtitle: option.sku ?? void 0,
          icon: option.thumbnailUrl ? /* @__PURE__ */ jsx(
            "img",
            {
              src: option.thumbnailUrl,
              alt: option.title,
              className: "h-8 w-8 rounded object-cover"
            }
          ) : buildPlaceholder(option.title),
          option
        };
      }).filter(
        (entry) => Boolean(entry)
      );
      return mapped.map((entry) => {
        productOptionsRef.current.set(entry.option.id, entry.option);
        return entry;
      });
    },
    []
  );
  const loadVariantOptions = React.useCallback(
    async (productId, fallbackThumbnail) => {
      if (!productId) return [];
      const response = await apiCall(
        `/api/catalog/variants?productId=${encodeURIComponent(productId)}&pageSize=50`,
        void 0,
        { fallback: { items: [] } }
      );
      const items = Array.isArray(response.result?.items) ? response.result.items : [];
      return items.map((item) => {
        const id = typeof item.id === "string" ? item.id : null;
        if (!id) return null;
        const title = typeof item.name === "string" ? item.name : id;
        const variantItem = item;
        const sku = typeof variantItem.sku === "string" ? variantItem.sku : null;
        const metadata = typeof variantItem.metadata === "object" && variantItem.metadata ? variantItem.metadata : null;
        const variantMeta = metadata;
        const variantTaxRateId = typeof variantMeta?.taxRateId === "string" && variantMeta.taxRateId.trim().length ? variantMeta.taxRateId.trim() : typeof variantMeta?.tax_rate_id === "string" && variantMeta.tax_rate_id.trim().length ? variantMeta.tax_rate_id.trim() : null;
        const variantTaxRate = normalizeNumber(
          variantItem.tax_rate ?? variantItem.taxRate ?? variantMeta?.tax_rate ?? variantMeta?.taxRate,
          Number.NaN
        );
        const thumbnail = typeof variantItem.default_media_url === "string" ? variantItem.default_media_url : typeof variantItem.thumbnailUrl === "string" ? variantItem.thumbnailUrl : fallbackThumbnail ?? null;
        return {
          id,
          title,
          subtitle: sku ?? void 0,
          icon: thumbnail ? /* @__PURE__ */ jsx(
            "img",
            {
              src: thumbnail,
              alt: title,
              className: "h-8 w-8 rounded object-cover"
            }
          ) : buildPlaceholder(title),
          option: {
            id,
            title,
            sku,
            thumbnailUrl: thumbnail,
            taxRateId: variantTaxRateId,
            taxRate: Number.isFinite(variantTaxRate) ? variantTaxRate : null
          }
        };
      }).filter(
        (entry) => Boolean(entry)
      ).map((entry) => {
        variantOptionsRef.current.set(entry.option.id, entry.option);
        return entry;
      });
    },
    []
  );
  const loadProductUnits = React.useCallback(
    async (productId, option) => {
      if (!productId) {
        setUnitOptions([]);
        return [];
      }
      const map = /* @__PURE__ */ new Map();
      let baseUnit = normalizeUnitCode(option?.defaultUnit);
      let defaultSalesUnit = normalizeUnitCode(option?.defaultSalesUnit);
      if (!baseUnit || !defaultSalesUnit) {
        try {
          const response = await apiCall(
            `/api/catalog/products?id=${encodeURIComponent(productId)}&pageSize=1`,
            void 0,
            { fallback: { items: [] } }
          );
          const records = Array.isArray(response.result?.items) ? response.result.items : [];
          const matched = records.find((entry) => entry.id === productId) ?? records[0] ?? null;
          if (matched) {
            const matchedUom = getUomProductFields(matched);
            baseUnit = baseUnit ?? matchedUom.defaultUnit;
            defaultSalesUnit = defaultSalesUnit ?? matchedUom.defaultSalesUnit;
          }
        } catch (err) {
          logger.error("sales.document.items.loadProductUnits.hydration", { err });
        }
      }
      if (baseUnit) {
        map.set(baseUnit, { code: baseUnit, toBaseFactor: 1, isBase: true });
      }
      try {
        const response = await apiCall(
          `/api/catalog/product-unit-conversions?productId=${encodeURIComponent(productId)}&pageSize=100`,
          void 0,
          { fallback: { items: [] } }
        );
        const rows = Array.isArray(response.result?.items) ? response.result.items : [];
        for (const row of rows) {
          const conv = getUomConversionFields(row);
          if (!conv.unitCode) continue;
          if (!conv.isActive) continue;
          map.set(conv.unitCode, {
            code: conv.unitCode,
            toBaseFactor: Number.isFinite(conv.toBaseFactor) && conv.toBaseFactor > 0 ? conv.toBaseFactor : null,
            isBase: baseUnit ? conv.unitCode === baseUnit : false
          });
        }
      } catch (err) {
        logger.error("sales.document.items.loadUnits", { err });
      }
      if (defaultSalesUnit && !map.has(defaultSalesUnit)) {
        map.set(defaultSalesUnit, {
          code: defaultSalesUnit,
          toBaseFactor: baseUnit && defaultSalesUnit === baseUnit ? 1 : null,
          isBase: baseUnit ? defaultSalesUnit === baseUnit : false
        });
      }
      const nextOptions = Array.from(map.values()).sort(
        (left, right) => left.code.localeCompare(right.code)
      );
      setUnitOptions(nextOptions);
      return nextOptions;
    },
    []
  );
  const loadPrices = React.useCallback(
    async (productId, variantId, quantity, quantityUnit) => {
      if (!productId) {
        setPriceOptions([]);
        return [];
      }
      setPriceLoading(true);
      try {
        const params = new URLSearchParams({ productId, pageSize: "20" });
        if (variantId) params.set("variantId", variantId);
        const quantityValue = normalizeNumber(quantity, Number.NaN);
        if (Number.isFinite(quantityValue) && quantityValue > 0) {
          params.set("quantity", String(quantityValue));
        }
        const quantityUnitCode = normalizeUnitCode(quantityUnit);
        if (quantityUnitCode) {
          params.set("quantityUnit", quantityUnitCode);
        }
        const response = await apiCall(`/api/catalog/prices?${params.toString()}`, void 0, {
          fallback: { items: [] }
        });
        const items = Array.isArray(response.result?.items) ? response.result.items : [];
        const mapped = items.map((item) => {
          const id = typeof item.id === "string" ? item.id : null;
          if (!id) return null;
          const amountNetRaw = normalizeNumber(
            item.unit_price_net,
            Number.NaN
          );
          const amountGrossRaw = normalizeNumber(
            item.unit_price_gross,
            Number.NaN
          );
          const amountNet = Number.isFinite(amountNetRaw) ? amountNetRaw : null;
          const amountGross = Number.isFinite(amountGrossRaw) ? amountGrossRaw : null;
          const currency = typeof item.currency_code === "string" ? item.currency_code : typeof item.currencyCode === "string" ? item.currencyCode : null;
          const displayMode = item.display_mode === "including-tax" || item.display_mode === "excluding-tax" ? item.display_mode : item.displayMode === "including-tax" || item.displayMode === "excluding-tax" ? item.displayMode : null;
          const taxRateRaw = normalizeNumber(
            item.tax_rate,
            Number.NaN
          );
          const taxRate = Number.isFinite(taxRateRaw) ? taxRateRaw : null;
          const priceKindId = typeof item.price_kind_id === "string" ? item.price_kind_id : typeof item.priceKindId === "string" ? item.priceKindId : null;
          const priceKindTitle = typeof item.price_kind_title === "string" ? item.price_kind_title : typeof item.priceKindTitle === "string" ? item.priceKindTitle : typeof item.price_kind === "object" && item && typeof item.price_kind?.title === "string" ? item.price_kind.title : typeof item.price_kind === "object" && item && typeof item.price_kind?.name === "string" ? item.price_kind.name : null;
          const priceKindCode = typeof item.price_kind_code === "string" ? item.price_kind_code : typeof item.priceKindCode === "string" ? item.priceKindCode : typeof item.price_kind === "object" && item && typeof item.price_kind?.code === "string" ? item.price_kind.code : null;
          const resolvedPriceKindTitle = priceKindTitle ?? priceKindCode ?? (typeof item.kind === "string" ? item.kind : null);
          const labelParts = [
            displayMode === "including-tax" && amountGross !== null && currency ? formatMoney(amountGross, currency) : null,
            displayMode === "excluding-tax" && amountNet !== null && currency ? formatMoney(amountNet, currency) : null,
            displayMode ? displayMode === "including-tax" ? t("sales.documents.items.priceGross", "Gross") : t("sales.documents.items.priceNet", "Net") : null
          ].filter(Boolean);
          const { reason, tags } = buildPriceScopeReason(
            item,
            (key, fallback) => t(key, fallback)
          );
          const label = labelParts.length > 0 ? labelParts.join(" \u2022 ") : amountGross !== null && currency ? formatMoney(amountGross, currency) : amountNet !== null && currency ? formatMoney(amountNet, currency) : id;
          return {
            id,
            amountNet: amountNet ?? null,
            amountGross: amountGross ?? null,
            currencyCode: currency,
            displayMode,
            taxRate: Number.isFinite(taxRate) ? taxRate : null,
            label,
            priceKindId,
            priceKindTitle: resolvedPriceKindTitle ?? null,
            priceKindCode: priceKindCode ?? null,
            scopeReason: reason,
            scopeTags: tags
          };
        }).filter((entry) => Boolean(entry));
        setPriceOptions(mapped);
        return mapped;
      } catch (err) {
        logger.error("sales.document.items.loadPrices", { err });
        return [];
      } finally {
        setPriceLoading(false);
      }
    },
    [t]
  );
  const selectPriceAfterRefresh = React.useCallback(
    (prices, currentPriceId, currentPriceKindId) => {
      if (!prices.length) return null;
      if (currentPriceId) {
        const sameId = prices.find((entry) => entry.id === currentPriceId);
        if (sameId) return sameId;
      }
      if (currentPriceKindId) {
        const sameKind = prices.find(
          (entry) => entry.priceKindId === currentPriceKindId
        );
        if (sameKind) return sameKind;
      }
      return prices[0] ?? null;
    },
    []
  );
  const resolveUnitPriceFactor = React.useCallback(
    (quantityUnit) => {
      const normalized = normalizeUnitCode(quantityUnit);
      if (!normalized) return 1;
      const unit = unitOptions.find((entry) => entry.code === normalized) ?? null;
      const factor = normalizeNumber(unit?.toBaseFactor, Number.NaN);
      if (!Number.isFinite(factor) || factor <= 0) return 1;
      return factor;
    },
    [unitOptions]
  );
  const convertUnitPriceForUnitChange = React.useCallback(
    (rawUnitPrice, fromUnit, toUnit) => {
      const amount = normalizeNumber(rawUnitPrice, Number.NaN);
      if (!Number.isFinite(amount) || amount <= 0) return null;
      const fromCode = normalizeUnitCode(fromUnit);
      const toCode = normalizeUnitCode(toUnit);
      if (!fromCode || !toCode || fromCode === toCode) return null;
      const fromFactor = resolveUnitPriceFactor(fromCode);
      const toFactor = resolveUnitPriceFactor(toCode);
      if (!Number.isFinite(fromFactor) || !Number.isFinite(toFactor) || fromFactor <= 0 || toFactor <= 0) {
        return null;
      }
      const baseAmount = amount / fromFactor;
      const convertedAmount = baseAmount * toFactor;
      if (!Number.isFinite(convertedAmount) || convertedAmount <= 0) return null;
      return normalizeUnitPriceInputValue(convertedAmount);
    },
    [resolveUnitPriceFactor]
  );
  const applyPriceSelection = React.useCallback(
    (selected, setFormValue, options) => {
      if (!setFormValue) return;
      if (selected) {
        const mode = selected.displayMode === "excluding-tax" ? "net" : "gross";
        const amountPerBaseUnit = mode === "net" ? selected.amountNet ?? selected.amountGross ?? 0 : selected.amountGross ?? selected.amountNet ?? 0;
        const factor = resolveUnitPriceFactor(options?.quantityUnit ?? null);
        const amount = Number.isFinite(amountPerBaseUnit * factor) ? amountPerBaseUnit * factor : amountPerBaseUnit;
        setFormValue("priceId", selected.id);
        setFormValue("priceMode", mode);
        setFormValue("unitPrice", normalizeUnitPriceInputValue(amount));
        setFormValue("taxRate", selected.taxRate ?? null);
        setFormValue("taxRateId", findTaxRateIdByValue(selected.taxRate));
        setFormValue(
          "currencyCode",
          selected.currencyCode ?? currencyCode ?? null
        );
        return;
      }
      const fallbackTax = resolveTaxSelection(options?.fallbackTaxSource ?? null);
      setFormValue("taxRate", fallbackTax.taxRate ?? null);
      setFormValue("taxRateId", fallbackTax.taxRateId ?? null);
    },
    [currencyCode, findTaxRateIdByValue, resolveTaxSelection, resolveUnitPriceFactor]
  );
  const loadLineStatuses = React.useCallback(async () => {
    setLineStatusLoading(true);
    try {
      const params = new URLSearchParams({ page: "1", pageSize: "100" });
      const response = await apiCall(`/api/sales/order-line-statuses?${params.toString()}`, void 0, {
        fallback: { items: [] }
      });
      const items = Array.isArray(response.result?.items) ? response.result.items : [];
      const mapped = items.map((entry) => {
        const id = typeof entry.id === "string" ? entry.id : null;
        const value = typeof entry.value === "string" ? entry.value : null;
        if (!id || !value) return null;
        const label = typeof entry.label === "string" && entry.label.trim().length ? entry.label : value;
        const color = typeof entry.color === "string" && entry.color.trim().length ? entry.color : null;
        const icon = typeof entry.icon === "string" && entry.icon.trim().length ? entry.icon : null;
        return { id, value, label, color, icon };
      }).filter((entry) => Boolean(entry));
      setLineStatuses(mapped);
      return mapped;
    } catch (err) {
      logger.error("sales.lines.statuses.load", { err });
      setLineStatuses([]);
      return [];
    } finally {
      setLineStatusLoading(false);
    }
  }, []);
  const fetchLineStatusItems = React.useCallback(
    async (query) => {
      const options = lineStatuses.length && !query ? lineStatuses : await loadLineStatuses();
      const term = query?.trim().toLowerCase() ?? "";
      const currentMap = options.reduce((acc, entry) => {
        acc[entry.value] = {
          value: entry.value,
          label: entry.label,
          color: entry.color,
          icon: entry.icon ?? null
        };
        return acc;
      }, {});
      return options.filter(
        (option) => !term.length || option.label.toLowerCase().includes(term) || option.value.toLowerCase().includes(term)
      ).map((option) => ({
        id: option.id,
        title: option.label,
        subtitle: option.label !== option.value ? option.value : void 0,
        icon: renderDictionaryIcon(option.icon, "h-4 w-4") ?? renderDictionaryColor(option.color, "h-4 w-4 rounded-full")
      }));
    },
    [lineStatuses, loadLineStatuses]
  );
  React.useEffect(() => {
    if (!open) return;
    loadTaxRates().catch(() => {
    });
    loadLineStatuses().catch(() => {
    });
  }, [loadLineStatuses, loadTaxRates, open]);
  const handleFormSubmit = React.useCallback(
    async (values) => {
      const resolvedDocumentId = typeof documentId === "string" && documentId.trim().length ? documentId : null;
      const resolvedOrg = resolvedOrganizationId;
      const resolvedTenant = resolvedTenantId;
      if (!resolvedOrg || !resolvedTenant || !resolvedDocumentId) {
        throw createCrudFormError(
          t(
            "sales.documents.items.errorScope",
            "Organization and tenant are required."
          )
        );
      }
      const lineMode2 = values.lineMode === "custom" ? "custom" : "catalog";
      const isCustomLine = lineMode2 === "custom";
      if (!isCustomLine && !values.productId) {
        throw createCrudFormError(
          t(
            "sales.documents.items.errorProductRequired",
            "Select a product to continue."
          ),
          {
            productId: t(
              "sales.documents.items.errorProductRequired",
              "Select a product to continue."
            )
          }
        );
      }
      if (!isCustomLine && !values.variantId) {
        throw createCrudFormError(
          t(
            "sales.documents.items.errorVariantRequired",
            "Select a variant to continue."
          ),
          {
            variantId: t(
              "sales.documents.items.errorVariantRequired",
              "Select a variant to continue."
            )
          }
        );
      }
      const qtyNumber = Number(values.quantity ?? 0);
      if (!Number.isFinite(qtyNumber) || qtyNumber <= 0) {
        throw createCrudFormError(
          t(
            "sales.documents.items.errorQuantity",
            "Quantity must be greater than 0."
          ),
          {
            quantity: t(
              "sales.documents.items.errorQuantity",
              "Quantity must be greater than 0."
            )
          }
        );
      }
      const resolvedQuantityUnit = (() => {
        const entered = normalizeUnitCode(values.quantityUnit);
        if (isCustomLine) return entered;
        return entered ?? normalizeUnitCode(productOption?.defaultSalesUnit) ?? normalizeUnitCode(productOption?.defaultUnit);
      })();
      const unitPriceNumber = Number(values.unitPrice ?? 0);
      if (!Number.isFinite(unitPriceNumber) || unitPriceNumber <= 0) {
        throw createCrudFormError(
          t(
            "sales.documents.items.errorUnitPrice",
            "Unit price must be greater than 0."
          ),
          {
            unitPrice: t(
              "sales.documents.items.errorUnitPrice",
              "Unit price must be greater than 0."
            )
          }
        );
      }
      const selectedPrice = !isCustomLine && values.priceId ? priceOptions.find((price) => price.id === values.priceId) ?? null : null;
      const resolvedCurrency = values.currencyCode ?? selectedPrice?.currencyCode ?? currencyCode ?? null;
      if (!resolvedCurrency) {
        throw createCrudFormError(
          t("sales.documents.items.errorCurrency", "Currency is required."),
          {
            priceId: t(
              "sales.documents.items.errorCurrency",
              "Currency is required."
            )
          }
        );
      }
      const resolvedNameRaw = (values.name ?? "").toString().trim();
      const resolvedName = isCustomLine ? resolvedNameRaw : resolvedNameRaw || variantOption?.title || productOption?.title || void 0;
      if (isCustomLine && !resolvedName) {
        throw createCrudFormError(
          t(
            "sales.documents.items.errorNameRequired",
            "Name is required for custom lines."
          ),
          {
            name: t(
              "sales.documents.items.errorNameRequired",
              "Name is required for custom lines."
            )
          }
        );
      }
      const resolvedPriceMode = values.priceMode === "net" ? "net" : "gross";
      const catalogSnapshot = !isCustomLine && typeof values.catalogSnapshot === "object" && values.catalogSnapshot ? values.catalogSnapshot : null;
      const selectedTaxRateId = typeof values.taxRateId === "string" && values.taxRateId.trim().length ? values.taxRateId : null;
      const resolvedTaxRate = Number.isFinite(values.taxRate) ? values.taxRate : normalizeNumber(values.taxRate);
      const normalizedTaxRate = Number.isFinite(resolvedTaxRate) ? resolvedTaxRate : 0;
      const unitPriceNetValue = resolvedPriceMode === "net" ? unitPriceNumber : unitPriceNumber / (1 + normalizedTaxRate / 100);
      const unitPriceGrossValue = resolvedPriceMode === "gross" ? unitPriceNumber : unitPriceNumber * (1 + normalizedTaxRate / 100);
      const safeUnitPriceNet = Number.isFinite(unitPriceNetValue) ? unitPriceNetValue : unitPriceNumber;
      const safeUnitPriceGross = Number.isFinite(unitPriceGrossValue) ? unitPriceGrossValue : unitPriceNumber;
      const totalNetAmount = safeUnitPriceNet * qtyNumber;
      const totalGrossAmount = safeUnitPriceGross * qtyNumber;
      const metadata = {
        ...catalogSnapshot ?? {},
        ...!isCustomLine && values.priceId ? { priceId: values.priceId } : {},
        priceMode: resolvedPriceMode,
        ...selectedTaxRateId ? { taxRateId: selectedTaxRateId } : {},
        ...!isCustomLine && productOption ? {
          productTitle: productOption.title,
          productSku: productOption.sku ?? null,
          productThumbnail: productOption.thumbnailUrl ?? null
        } : {},
        ...!isCustomLine && variantOption ? {
          variantTitle: variantOption.title,
          variantSku: variantOption.sku ?? null,
          variantThumbnail: variantOption.thumbnailUrl ?? productOption?.thumbnailUrl ?? null
        } : {},
        ...isCustomLine ? { customLine: true } : {},
        lineMode: lineMode2,
        ...resolvedQuantityUnit ? { quantityUnit: resolvedQuantityUnit } : {}
      };
      const payload = {
        [documentKey]: String(resolvedDocumentId),
        organizationId: String(resolvedOrg),
        tenantId: String(resolvedTenant),
        productId: isCustomLine ? void 0 : values.productId ? String(values.productId) : void 0,
        productVariantId: isCustomLine ? void 0 : values.variantId ? String(values.variantId) : void 0,
        quantity: qtyNumber,
        quantityUnit: resolvedQuantityUnit ?? void 0,
        currencyCode: String(resolvedCurrency),
        priceId: !isCustomLine && values.priceId ? String(values.priceId) : void 0,
        priceMode: resolvedPriceMode,
        taxRate: Number.isFinite(resolvedTaxRate) ? resolvedTaxRate : void 0,
        unitPriceNet: safeUnitPriceNet,
        unitPriceGross: safeUnitPriceGross,
        totalNetAmount,
        totalGrossAmount,
        ...catalogSnapshot ? { catalogSnapshot } : {},
        metadata,
        customFieldSetId: values.customFieldSetId ?? void 0,
        ...typeof values.statusEntryId === "string" && values.statusEntryId.trim().length ? { statusEntryId: values.statusEntryId.trim() } : {}
      };
      const customFields = collectCustomFieldValues(values, {
        transform: (value) => normalizeCustomFieldSubmitValue(value)
      });
      if (Object.keys(customFields).length) {
        payload.customFields = normalizeCustomFieldValues(customFields);
      }
      if (resolvedName) payload.name = resolvedName;
      try {
        const action = editingId ? updateCrud : createCrud;
        const result = await withScopedApiRequestHeaders(
          buildOptimisticLockHeader(documentUpdatedAt),
          () => action(
            resourcePath,
            editingId ? { id: editingId, ...payload } : payload,
            {
              errorMessage: t(
                "sales.documents.items.errorSave",
                "Failed to save line."
              )
            }
          )
        );
        if (result.ok) {
          if (onSaved) await onSaved();
          closeDialog();
        }
      } catch (err) {
        if (handleSectionMutationError(err, t, () => {
          if (onSaved) void onSaved();
        })) {
          closeDialog();
          return;
        }
        throw err;
      }
    },
    [
      currencyCode,
      documentId,
      documentKey,
      documentUpdatedAt,
      editingId,
      priceOptions,
      productOption,
      resourcePath,
      t,
      variantOption,
      onSaved,
      closeDialog,
      resolvedOrganizationId,
      resolvedTenantId
    ]
  );
  const fields = React.useMemo(() => {
    const isCustomLine = lineMode === "custom";
    return [
      {
        id: "lineMode",
        label: t("sales.documents.items.lineMode.label", "Line type"),
        type: "custom",
        layout: "full",
        component: ({ value, setValue, setFormValue }) => {
          const mode = value === "custom" ? "custom" : "catalog";
          const switchMode = (next) => {
            if (next === mode) return;
            setValue(next);
            setLineMode(next);
            if (next === "custom") {
              setProductOption(null);
              setVariantOption(null);
              setPriceOptions([]);
              setUnitOptions([]);
              setFormValue?.("productId", null);
              setFormValue?.("variantId", null);
              setFormValue?.("priceId", null);
              setFormValue?.("catalogSnapshot", null);
              setFormValue?.("quantityUnit", null);
            } else {
              if (deletedCatalogReference) {
                setDeletedCatalogReference(false);
                setProductOption(null);
                setVariantOption(null);
                setPriceOptions([]);
                setUnitOptions([]);
                setFormValue?.("productId", null);
                setFormValue?.("variantId", null);
                setFormValue?.("priceId", null);
                setFormValue?.("catalogSnapshot", null);
                setFormValue?.("quantityUnit", null);
              }
              setFormValue?.("unitPrice", "");
              setFormValue?.("priceMode", "gross");
            }
          };
          return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-2", children: [
            /* @__PURE__ */ jsxs("div", { className: "inline-flex w-fit gap-1 rounded-md border bg-muted/50 p-1", children: [
              /* @__PURE__ */ jsx(
                Button,
                {
                  type: "button",
                  size: "sm",
                  variant: mode === "catalog" ? "default" : "ghost",
                  onClick: () => switchMode("catalog"),
                  children: t("sales.documents.items.lineMode.catalog", "Catalog item")
                }
              ),
              /* @__PURE__ */ jsx(
                Button,
                {
                  type: "button",
                  size: "sm",
                  variant: mode === "custom" ? "default" : "ghost",
                  onClick: () => switchMode("custom"),
                  children: t("sales.documents.items.lineMode.custom", "Custom line")
                }
              )
            ] }),
            /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
              "sales.documents.items.lineMode.helper",
              "Use catalog products or create a freeform line with your own price."
            ) })
          ] });
        }
      },
      ...deletedCatalogReference ? [
        {
          id: "deletedCatalogReference",
          label: t(
            "sales.documents.items.deletedCatalogReference.label",
            "Deleted catalog item"
          ),
          type: "custom",
          layout: "full",
          component: () => /* @__PURE__ */ jsxs(Alert, { status: "information", style: "lighter", children: [
            /* @__PURE__ */ jsx(AlertTitle, { children: t(
              "sales.documents.items.deletedCatalogReference.title",
              "This catalog item was deleted"
            ) }),
            /* @__PURE__ */ jsx(AlertDescription, { children: t(
              "sales.documents.items.deletedCatalogReference.description",
              "The saved line data is kept and the line is shown as a custom line. Switch to Catalog item to replace it with an existing product."
            ) })
          ] })
        }
      ] : [],
      ...!isCustomLine ? [
        {
          id: "productId",
          label: t("sales.documents.items.product", "Product"),
          type: "custom",
          required: true,
          layout: "half",
          component: ({
            value,
            setValue,
            setFormValue,
            values
          }) => /* @__PURE__ */ jsx(
            LookupSelect,
            {
              value: typeof value === "string" ? value : null,
              onChange: (next) => {
                const selectedOption = next ? productOptionsRef.current.get(next) ?? null : null;
                if (next) setDeletedCatalogReference(false);
                setProductOption(selectedOption);
                setVariantOption(null);
                setPriceOptions([]);
                setUnitOptions([]);
                setValue(next ?? null);
                setFormValue?.("variantId", null);
                setFormValue?.("priceId", null);
                setFormValue?.("unitPrice", "");
                setFormValue?.("priceMode", "gross");
                const defaultQuantityUnit = selectedOption?.defaultSalesUnit ?? selectedOption?.defaultUnit ?? null;
                setFormValue?.("quantityUnit", defaultQuantityUnit);
                if (typeof selectedOption?.defaultSalesUnitQuantity === "number" && Number.isFinite(
                  selectedOption.defaultSalesUnitQuantity
                ) && selectedOption.defaultSalesUnitQuantity > 0) {
                  setFormValue?.(
                    "quantity",
                    String(selectedOption.defaultSalesUnitQuantity)
                  );
                }
                const taxSelection = selectedOption ? resolveTaxSelection(selectedOption) : { taxRate: null, taxRateId: null };
                setFormValue?.("taxRate", taxSelection.taxRate ?? null);
                setFormValue?.("taxRateId", taxSelection.taxRateId ?? null);
                const existingName = typeof values?.name === "string" ? values.name : "";
                if (!existingName.trim() && selectedOption?.title) {
                  setFormValue?.("name", selectedOption.title);
                }
                setFormValue?.(
                  "catalogSnapshot",
                  next ? {
                    product: {
                      id: next,
                      title: selectedOption?.title ?? null,
                      sku: selectedOption?.sku ?? null,
                      thumbnailUrl: selectedOption?.thumbnailUrl ?? null,
                      defaultUnit: selectedOption?.defaultUnit ?? null,
                      defaultSalesUnit: selectedOption?.defaultSalesUnit ?? null
                    }
                  } : null
                );
                if (next) {
                  void loadProductUnits(next, selectedOption);
                  void loadPrices(
                    next,
                    null,
                    typeof values?.quantity === "string" ? values.quantity : 1,
                    defaultQuantityUnit
                  );
                }
              },
              fetchItems: loadProductOptions,
              options: productOption ? [
                {
                  id: productOption.id,
                  title: productOption.title || productOption.id,
                  subtitle: productOption.sku ?? void 0,
                  icon: productOption.thumbnailUrl ? /* @__PURE__ */ jsx(
                    "img",
                    {
                      src: productOption.thumbnailUrl,
                      alt: productOption.title ?? productOption.id,
                      className: "h-8 w-8 rounded object-cover"
                    }
                  ) : buildPlaceholder(
                    productOption.title || productOption.id
                  )
                }
              ] : void 0,
              minQuery: 1,
              searchPlaceholder: t(
                "sales.documents.items.productSearch",
                "Search product"
              ),
              selectLabel: t("ui.lookupSelect.select", "Select"),
              selectedLabel: t("ui.lookupSelect.selected", "Selected"),
              clearLabel: t(
                "ui.lookupSelect.clearSelection",
                "Clear selection"
              ),
              emptyLabel: t("ui.lookupSelect.noResults", "No results"),
              loadingLabel: t("ui.lookupSelect.searching", "Searching\u2026"),
              startTypingLabel: t(
                "ui.lookupSelect.startTyping",
                "Start typing to search."
              ),
              selectedHintLabel: (id) => t(
                "sales.documents.items.selectedProduct",
                "Selected {{id}}",
                {
                  id: productOption?.title ?? id
                }
              )
            }
          )
        },
        {
          id: "variantId",
          label: t("sales.documents.items.variant", "Variant"),
          type: "custom",
          required: true,
          layout: "half",
          component: ({
            value,
            setValue,
            setFormValue,
            values
          }) => {
            const productId = typeof values?.productId === "string" ? values.productId : null;
            return /* @__PURE__ */ jsx(
              LookupSelect,
              {
                value: typeof value === "string" ? value : null,
                onChange: (next) => {
                  const selectedOption = next ? variantOptionsRef.current.get(next) ?? null : null;
                  setVariantOption(selectedOption);
                  setValue(next ?? null);
                  const existingName = typeof values?.name === "string" ? values.name : "";
                  if (!existingName.trim()) {
                    setFormValue?.(
                      "name",
                      selectedOption?.title ?? productOption?.title ?? existingName
                    );
                  }
                  const taxSource = hasTaxMetadata(selectedOption) ? selectedOption : hasTaxMetadata(productOption) ? productOption : null;
                  if (taxSource) {
                    const taxSelection = resolveTaxSelection(taxSource);
                    setFormValue?.("taxRate", taxSelection.taxRate ?? null);
                    setFormValue?.(
                      "taxRateId",
                      taxSelection.taxRateId ?? null
                    );
                  }
                  const prevSnapshot = typeof values?.catalogSnapshot === "object" && values.catalogSnapshot ? values.catalogSnapshot : null;
                  if (next) {
                    setFormValue?.("catalogSnapshot", {
                      ...prevSnapshot ?? {},
                      variant: {
                        id: next,
                        title: selectedOption?.title ?? null,
                        sku: selectedOption?.sku ?? null,
                        thumbnailUrl: selectedOption?.thumbnailUrl ?? null
                      }
                    });
                  } else if (prevSnapshot) {
                    const snapshot = { ...prevSnapshot };
                    if ("variant" in snapshot)
                      delete snapshot.variant;
                    setFormValue?.(
                      "catalogSnapshot",
                      Object.keys(snapshot).length ? snapshot : null
                    );
                  } else {
                    setFormValue?.("catalogSnapshot", null);
                  }
                  if (productId) {
                    const currentQuantity = typeof values?.quantity === "string" ? values.quantity : 1;
                    const currentQuantityUnit = typeof values?.quantityUnit === "string" ? values.quantityUnit : null;
                    void loadPrices(
                      productId,
                      next,
                      currentQuantity,
                      currentQuantityUnit
                    );
                  }
                },
                fetchItems: async (query) => {
                  if (!productId) return [];
                  const productThumb = productId ? productOptionsRef.current.get(productId)?.thumbnailUrl : null;
                  const options = await loadVariantOptions(
                    productId,
                    productThumb
                  );
                  const needle = query?.trim().toLowerCase() ?? "";
                  return needle.length ? options.filter(
                    (option) => option.title.toLowerCase().includes(needle)
                  ) : options;
                },
                searchPlaceholder: t(
                  "sales.documents.items.variantSearch",
                  "Search variant"
                ),
                selectLabel: t("ui.lookupSelect.select", "Select"),
                selectedLabel: t("ui.lookupSelect.selected", "Selected"),
                clearLabel: t(
                  "ui.lookupSelect.clearSelection",
                  "Clear selection"
                ),
                emptyLabel: t("ui.lookupSelect.noResults", "No results"),
                loadingLabel: t("ui.lookupSelect.searching", "Searching\u2026"),
                startTypingLabel: t(
                  "ui.lookupSelect.startTyping",
                  "Start typing to search."
                ),
                minQuery: 0,
                options: variantOption ? [
                  {
                    id: variantOption.id,
                    title: variantOption.title || variantOption.id,
                    subtitle: variantOption.sku ?? void 0,
                    icon: variantOption.thumbnailUrl ? /* @__PURE__ */ jsx(
                      "img",
                      {
                        src: variantOption.thumbnailUrl,
                        alt: variantOption.title ?? variantOption.id,
                        className: "h-8 w-8 rounded object-cover"
                      }
                    ) : buildPlaceholder(
                      variantOption.title || variantOption.id
                    )
                  }
                ] : void 0,
                selectedHintLabel: (id) => t(
                  "sales.documents.items.selectedVariant",
                  "Selected {{id}}",
                  {
                    id: variantOption?.title ?? id
                  }
                ),
                disabled: !productId
              },
              productId ?? "no-product"
            );
          }
        },
        {
          id: "priceId",
          label: t("sales.documents.items.price", "Price"),
          type: "custom",
          layout: "half",
          component: ({
            value,
            setValue,
            setFormValue,
            values
          }) => {
            const productId = typeof values?.productId === "string" ? values.productId : null;
            const variantId = typeof values?.variantId === "string" ? values.variantId : null;
            return /* @__PURE__ */ jsx(
              LookupSelect,
              {
                value: typeof value === "string" ? value : null,
                onChange: (next) => {
                  setValue(next ?? null);
                  const selected = next ? priceOptions.find((entry) => entry.id === next) ?? null : null;
                  applyPriceSelection(
                    selected,
                    setFormValue,
                    {
                      fallbackTaxSource: variantOption ?? productOption ?? null,
                      quantityUnit: typeof values?.quantityUnit === "string" ? values.quantityUnit : null
                    }
                  );
                },
                fetchItems: async (query) => {
                  const prices = await loadPrices(
                    productId,
                    variantId,
                    typeof values?.quantity === "string" ? values.quantity : 1,
                    typeof values?.quantityUnit === "string" ? values.quantityUnit : null
                  );
                  const needle = query?.trim().toLowerCase() ?? "";
                  return prices.filter((price) => {
                    if (!needle.length) return true;
                    const haystack = [
                      price.label,
                      price.priceKindTitle,
                      price.priceKindCode,
                      price.currencyCode,
                      ...price.scopeTags ?? []
                    ].filter(Boolean).join(" ").toLowerCase();
                    return haystack.includes(needle);
                  }).map((price) => ({
                    id: price.id,
                    title: price.label,
                    subtitle: price.priceKindTitle ?? price.priceKindCode ?? void 0,
                    description: price.scopeReason ?? void 0,
                    rightLabel: price.currencyCode ?? void 0,
                    icon: /* @__PURE__ */ jsx(DollarSign, { className: "h-5 w-5 text-muted-foreground" })
                  }));
                },
                minQuery: 0,
                loading: priceLoading,
                searchPlaceholder: t(
                  "sales.documents.items.priceSearch",
                  "Select price"
                ),
                selectLabel: t("ui.lookupSelect.select", "Select"),
                selectedLabel: t("ui.lookupSelect.selected", "Selected"),
                clearLabel: t(
                  "ui.lookupSelect.clearSelection",
                  "Clear selection"
                ),
                emptyLabel: t("ui.lookupSelect.noResults", "No results"),
                loadingLabel: t("ui.lookupSelect.searching", "Searching\u2026"),
                startTypingLabel: t(
                  "ui.lookupSelect.startTyping",
                  "Start typing to search."
                ),
                disabled: !productId
              },
              productId ? `${productId}-${variantId ?? "no-variant"}` : "price"
            );
          }
        }
      ] : [],
      {
        id: "unitPrice",
        label: t("sales.documents.items.unitPrice", "Unit price"),
        type: "custom",
        layout: "half",
        component: ({
          value,
          setValue,
          setFormValue,
          values
        }) => {
          const mode = values?.priceMode === "net" ? "net" : "gross";
          const selectedPriceId = typeof values?.priceId === "string" ? values.priceId : null;
          const selectedPrice = selectedPriceId ? priceOptions.find((entry) => entry.id === selectedPriceId) ?? null : null;
          const selectedCurrency = selectedPrice?.currencyCode ?? (typeof values?.currencyCode === "string" ? values.currencyCode : null);
          const quantityUnitCode = normalizeUnitCode(values?.quantityUnit);
          const baseUnitCode = unitOptions.find((option) => option.isBase)?.code ?? null;
          const selectedUnitOption = quantityUnitCode ? unitOptions.find((option) => option.code === quantityUnitCode) ?? null : null;
          const unitFactor = (() => {
            if (!quantityUnitCode) return null;
            if (baseUnitCode && quantityUnitCode === baseUnitCode) return 1;
            const value2 = normalizeNumber(selectedUnitOption?.toBaseFactor, Number.NaN);
            return Number.isFinite(value2) && value2 > 0 ? value2 : null;
          })();
          const selectedBaseAmount = selectedPrice ? mode === "net" ? selectedPrice.amountNet ?? selectedPrice.amountGross ?? null : selectedPrice.amountGross ?? selectedPrice.amountNet ?? null : null;
          const convertedAmount = selectedBaseAmount !== null && unitFactor !== null && Number.isFinite(selectedBaseAmount * unitFactor) ? selectedBaseAmount * unitFactor : null;
          const isCatalogLine = lineMode !== "custom";
          return /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
            /* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
              /* @__PURE__ */ jsx(
                Input,
                {
                  value: typeof value === "string" ? value : value == null ? "" : String(value),
                  onChange: (event) => setValue(event.target.value),
                  placeholder: "0.00"
                }
              ),
              /* @__PURE__ */ jsxs(
                Select,
                {
                  value: mode,
                  onValueChange: (value2) => {
                    const nextMode = value2 === "net" ? "net" : "gross";
                    setFormValue?.("priceMode", nextMode);
                  },
                  children: [
                    /* @__PURE__ */ jsx(SelectTrigger, { className: "w-32", children: /* @__PURE__ */ jsx(SelectValue, {}) }),
                    /* @__PURE__ */ jsxs(SelectContent, { children: [
                      /* @__PURE__ */ jsx(SelectItem, { value: "gross", children: t("sales.documents.items.priceGross", "Gross") }),
                      /* @__PURE__ */ jsx(SelectItem, { value: "net", children: t("sales.documents.items.priceNet", "Net") })
                    ] })
                  ]
                }
              )
            ] }),
            isCatalogLine && selectedPrice && quantityUnitCode && baseUnitCode ? unitFactor !== null && convertedAmount !== null ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
              "sales.documents.items.priceBasisTemplate",
              "Catalog price basis: {{baseAmount}} / {{baseUnit}}. Converted for {{unit}}: {{baseAmount}} \xD7 {{factor}} = {{convertedAmount}}.",
              {
                baseAmount: formatMoney(selectedBaseAmount, selectedCurrency),
                baseUnit: baseUnitCode,
                unit: quantityUnitCode,
                factor: unitFactor,
                convertedAmount: formatMoney(convertedAmount, selectedCurrency)
              }
            ) }) : /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
              "sales.documents.items.priceBasisMissingConversion",
              "Catalog price basis is base unit, but conversion for selected unit is missing."
            ) }) : null,
            isCatalogLine && !selectedPrice ? /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
              "sales.documents.items.priceBasisManual",
              "Manual unit price mode. Price will not auto-convert until you select a catalog price."
            ) }) : null
          ] });
        }
      },
      {
        id: "taxRateId",
        label: t("sales.documents.items.taxRate", "Tax class"),
        type: "custom",
        layout: "half",
        component: ({
          value,
          setValue,
          setFormValue,
          values
        }) => {
          const resolvedValue = typeof value === "string" && value.trim().length ? value : findTaxRateIdByValue(values?.taxRate);
          const selectedTaxRate = resolvedValue ? taxRateMap.get(resolvedValue) ?? null : null;
          const handleChange = (event) => {
            const nextId = event.target.value || null;
            const option = nextId ? taxRateMap.get(nextId) ?? null : null;
            setValue(nextId);
            const rate = normalizeNumber(option?.rate);
            setFormValue?.("taxRate", Number.isFinite(rate) ? rate : null);
          };
          return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
            /* @__PURE__ */ jsxs(
              Select,
              {
                value: resolvedValue || void 0,
                onValueChange: (value2) => handleChange({ target: { value: value2 } }),
                disabled: !taxRates.length,
                children: [
                  /* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(
                    SelectValue,
                    {
                      placeholder: taxRates.length ? t(
                        "sales.documents.items.taxRate.none",
                        "No tax class selected"
                      ) : t(
                        "sales.documents.items.taxRate.empty",
                        "No tax classes available"
                      ),
                      children: selectedTaxRate ? `${selectedTaxRate.name}${selectedTaxRate.code ? ` \u2022 ${selectedTaxRate.code.toUpperCase()}` : ""}${Number.isFinite(selectedTaxRate.rate) ? ` \u2022 ${selectedTaxRate.rate}%` : ""}` : void 0
                    }
                  ) }),
                  /* @__PURE__ */ jsx(SelectContent, { children: taxRates.map((rate) => /* @__PURE__ */ jsxs(SelectItem, { value: rate.id, children: [
                    rate.name,
                    rate.code ? ` \u2022 ${rate.code.toUpperCase()}` : "",
                    Number.isFinite(rate.rate) ? ` \u2022 ${rate.rate}%` : ""
                  ] }, rate.id)) })
                ]
              }
            ),
            /* @__PURE__ */ jsx(
              Button,
              {
                type: "button",
                variant: "ghost",
                size: "icon",
                onClick: () => {
                  if (typeof window !== "undefined") {
                    window.open(
                      "/backend/config/sales?section=tax-rates",
                      "_blank",
                      "noopener,noreferrer"
                    );
                  }
                },
                title: t(
                  "catalog.products.create.taxRates.manage",
                  "Manage tax classes"
                ),
                children: /* @__PURE__ */ jsx(Settings, { className: "h-4 w-4" })
              }
            )
          ] });
        }
      },
      {
        id: "quantityUnit",
        label: t("sales.documents.items.quantityUnit", "Unit"),
        type: "custom",
        layout: "half",
        component: ({ value, setValue, setFormValue, values }) => {
          const productId = typeof values?.productId === "string" ? values.productId : null;
          const variantId = typeof values?.variantId === "string" ? values.variantId : null;
          if (isCustomLine) {
            return /* @__PURE__ */ jsx(
              Input,
              {
                value: typeof value === "string" ? value : "",
                onChange: (event) => setValue(event.target.value || null),
                placeholder: t(
                  "sales.documents.items.quantityUnitPlaceholder",
                  "e.g. pc"
                )
              }
            );
          }
          return /* @__PURE__ */ jsxs(
            Select,
            {
              value: typeof value === "string" && value ? value : void 0,
              onValueChange: (next) => {
                const nextValue = next || null;
                const nextUnitPrice = convertUnitPriceForUnitChange(
                  values?.unitPrice,
                  typeof value === "string" ? value : null,
                  nextValue
                );
                setValue(nextValue);
                if (nextUnitPrice) {
                  setFormValue?.("unitPrice", nextUnitPrice);
                }
                if (productId) {
                  const selectedPriceId = typeof values?.priceId === "string" ? values.priceId : null;
                  const selectedPriceKindId = selectedPriceId ? priceOptions.find(
                    (entry) => entry.id === selectedPriceId
                  )?.priceKindId ?? null : null;
                  void loadPrices(
                    productId,
                    variantId,
                    typeof values?.quantity === "string" ? values.quantity : 1,
                    nextValue
                  ).then((prices) => {
                    if (!selectedPriceId) return;
                    const nextSelected = selectPriceAfterRefresh(
                      prices,
                      selectedPriceId,
                      selectedPriceKindId
                    );
                    if (!nextSelected) {
                      setFormValue?.("priceId", null);
                      return;
                    }
                    applyPriceSelection(
                      nextSelected,
                      setFormValue,
                      {
                        fallbackTaxSource: variantOption ?? productOption ?? null,
                        quantityUnit: nextValue
                      }
                    );
                  });
                }
              },
              disabled: !productId,
              children: [
                /* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(
                  SelectValue,
                  {
                    placeholder: t("sales.documents.items.quantityUnitSelect", "Select unit")
                  }
                ) }),
                /* @__PURE__ */ jsx(SelectContent, { children: unitOptions.map((option) => /* @__PURE__ */ jsx(SelectItem, { value: option.code, children: option.isBase ? `${option.code} (${t("sales.documents.items.baseUnitTag", "base")})` : option.code }, option.code)) })
              ]
            }
          );
        }
      },
      {
        id: "quantity",
        label: t("sales.documents.items.quantity", "Quantity"),
        type: "custom",
        layout: "half",
        component: ({ value, setValue, setFormValue, values }) => /* @__PURE__ */ jsx(
          Input,
          {
            value: typeof value === "string" ? value : value == null ? "" : String(value),
            onChange: (event) => {
              const nextQuantity = event.target.value;
              setValue(nextQuantity);
              const productId = typeof values?.productId === "string" ? values.productId : null;
              const variantId = typeof values?.variantId === "string" ? values.variantId : null;
              const quantityUnit = typeof values?.quantityUnit === "string" ? values.quantityUnit : null;
              if (productId) {
                const selectedPriceId = typeof values?.priceId === "string" ? values.priceId : null;
                const selectedPriceKindId = selectedPriceId ? priceOptions.find((entry) => entry.id === selectedPriceId)?.priceKindId ?? null : null;
                void loadPrices(
                  productId,
                  variantId,
                  nextQuantity,
                  quantityUnit
                ).then((prices) => {
                  if (!selectedPriceId) return;
                  const nextSelected = selectPriceAfterRefresh(
                    prices,
                    selectedPriceId,
                    selectedPriceKindId
                  );
                  if (!nextSelected) {
                    setFormValue?.("priceId", null);
                    return;
                  }
                  applyPriceSelection(
                    nextSelected,
                    setFormValue,
                    {
                      fallbackTaxSource: variantOption ?? productOption ?? null,
                      quantityUnit
                    }
                  );
                });
              }
            },
            placeholder: "1"
          }
        )
      },
      {
        id: "uomPreview",
        label: t("sales.documents.items.uomPreview", "Normalized quantity"),
        type: "custom",
        layout: "full",
        component: ({ values }) => {
          if (isCustomLine) return null;
          const quantity = normalizeNumber(values?.quantity, Number.NaN);
          const enteredUnit = normalizeUnitCode(values?.quantityUnit);
          if (!Number.isFinite(quantity) || quantity <= 0 || !enteredUnit) {
            return /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
              "sales.documents.items.uomPreviewEmpty",
              "Select unit and quantity to preview normalization."
            ) });
          }
          const selectedOption = unitOptions.find((option) => option.code === enteredUnit) ?? null;
          const baseOption = unitOptions.find((option) => option.isBase) ?? null;
          const factor = selectedOption?.toBaseFactor;
          if (!Number.isFinite(factor) || !factor || !baseOption) {
            return /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
              "sales.documents.items.uomPreviewUnavailable",
              "Missing conversion to base unit."
            ) });
          }
          const normalized = normalizeQuantityPreview(quantity * factor);
          return /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(
            "sales.documents.items.uomPreviewTemplate",
            "{{quantity}} {{unit}} \u2192 {{normalized}} {{baseUnit}}",
            {
              quantity,
              unit: enteredUnit,
              normalized,
              baseUnit: baseOption.code
            }
          ) });
        }
      },
      {
        id: "statusEntryId",
        label: t("sales.documents.items.status", "Status"),
        type: "custom",
        layout: "half",
        component: ({ value, setValue }) => /* @__PURE__ */ jsx(
          LookupSelect,
          {
            value: typeof value === "string" ? value : null,
            onChange: (next) => setValue(next ?? null),
            placeholder: t(
              "sales.documents.items.statusPlaceholder",
              "Select status"
            ),
            emptyLabel: t("sales.documents.items.statusEmpty", "No status"),
            fetchItems: fetchLineStatusItems,
            loadingLabel: t(
              "sales.documents.items.statusLoading",
              "Loading statuses\u2026"
            ),
            selectLabel: t("ui.lookupSelect.select", "Select"),
            selectedLabel: t("ui.lookupSelect.selected", "Selected"),
            clearLabel: t("ui.lookupSelect.clearSelection", "Clear selection"),
            startTypingLabel: t(
              "ui.lookupSelect.startTyping",
              "Start typing to search."
            ),
            minQuery: 0
          }
        )
      },
      {
        id: "name",
        label: t("sales.documents.items.name", "Name"),
        type: "text",
        placeholder: t(
          "sales.documents.items.namePlaceholder",
          "Optional line name"
        ),
        layout: "full",
        required: isCustomLine
      }
    ];
  }, [
    applyPriceSelection,
    convertUnitPriceForUnitChange,
    currencyCode,
    findTaxRateIdByValue,
    loadPrices,
    loadProductUnits,
    loadProductOptions,
    loadVariantOptions,
    fetchLineStatusItems,
    deletedCatalogReference,
    priceLoading,
    priceOptions,
    productOption,
    unitOptions,
    lineMode,
    variantOption,
    t,
    taxRateMap,
    taxRates,
    resolveTaxSelection,
    selectPriceAfterRefresh,
    hasTaxMetadata
  ]);
  const groups = React.useMemo(() => {
    return [
      { id: "line-core", fields },
      {
        id: "line-custom",
        column: 2,
        title: t("entities.customFields.title", "Custom fields"),
        kind: "customFields"
      }
    ];
  }, [fields, t]);
  React.useEffect(() => {
    if (!open) return;
    if (!initialLine) {
      resetForm();
      return;
    }
    let cancelled = false;
    setDeletedCatalogReference(false);
    setEditingId(initialLine.id);
    const nextForm = defaultForm(initialLine.currencyCode ?? currencyCode);
    const meta = initialLine.metadata ?? {};
    const snapshot = initialLine.catalogSnapshot ?? null;
    const snapshotProduct = snapshot && typeof snapshot === "object" && typeof snapshot.product === "object" && snapshot.product ? snapshot.product : null;
    const snapshotVariant = snapshot && typeof snapshot === "object" && typeof snapshot.variant === "object" && snapshot.variant ? snapshot.variant : null;
    const metaRec = typeof meta === "object" && meta ? meta : null;
    const metaLineMode = typeof metaRec?.lineMode === "string" && (metaRec.lineMode === "custom" || metaRec.lineMode === "catalog") ? metaRec.lineMode : metaRec?.customLine ? "custom" : void 0;
    nextForm.productId = initialLine.productId;
    nextForm.variantId = initialLine.productVariantId;
    nextForm.quantity = initialLine.quantity.toString();
    nextForm.quantityUnit = normalizeUnitCode(initialLine.quantityUnit) ?? null;
    const metaMode = metaRec?.priceMode;
    const resolvedPriceMode = metaMode === "net" || metaMode === "gross" ? metaMode : initialLine.priceMode ?? "gross";
    nextForm.unitPrice = resolvedPriceMode === "net" ? initialLine.unitPriceNet.toString() : initialLine.unitPriceGross.toString();
    nextForm.priceMode = resolvedPriceMode;
    nextForm.taxRate = Number.isFinite(initialLine.taxRate) ? initialLine.taxRate : null;
    nextForm.name = initialLine.name ?? "";
    nextForm.catalogSnapshot = snapshot ?? null;
    nextForm.customFieldSetId = initialLine.customFieldSetId ?? null;
    nextForm.statusEntryId = initialLine.statusEntryId ?? null;
    nextForm.lineMode = metaLineMode ?? (initialLine.productId || initialLine.productVariantId ? "catalog" : "custom");
    const metaTaxRateId = typeof metaRec?.taxRateId === "string" ? metaRec.taxRateId : null;
    const fallbackTaxRateId = findTaxRateIdByValue(nextForm.taxRate);
    nextForm.taxRateId = metaTaxRateId ?? fallbackTaxRateId ?? (defaultTaxRateRef.current ? defaultTaxRateRef.current.id : null);
    if (!Number.isFinite(nextForm.taxRate) && nextForm.taxRateId) {
      const matched = taxRatesRef.current.find(
        (rate) => rate.id === nextForm.taxRateId
      );
      const numericRate = normalizeNumber(matched?.rate);
      if (Number.isFinite(numericRate)) {
        nextForm.taxRate = numericRate;
      }
    }
    let resolvedProductOption = null;
    let resolvedVariantOption = null;
    if (metaRec) {
      const metaRecord = metaRec;
      const mode = metaRecord.priceMode;
      if (mode === "net" || mode === "gross") {
        nextForm.priceMode = mode;
        nextForm.unitPrice = mode === "net" ? initialLine.unitPriceNet.toString() : initialLine.unitPriceGross.toString();
      }
      nextForm.priceId = typeof metaRecord.priceId === "string" ? metaRecord.priceId : null;
      const productTitle = typeof metaRecord.productTitle === "string" ? metaRecord.productTitle : initialLine.name;
      const productSku = typeof metaRecord.productSku === "string" ? metaRecord.productSku : null;
      const productThumbnail = typeof metaRecord.productThumbnail === "string" ? metaRecord.productThumbnail : null;
      if (productTitle && initialLine.productId) {
        const snapshotUom = snapshotProduct ? getUomProductFields(snapshotProduct) : { defaultUnit: null, defaultSalesUnit: null, defaultSalesUnitQuantity: Number.NaN };
        const option = {
          id: initialLine.productId,
          title: productTitle,
          sku: productSku,
          thumbnailUrl: productThumbnail,
          defaultUnit: snapshotUom.defaultUnit,
          defaultSalesUnit: snapshotUom.defaultSalesUnit,
          defaultSalesUnitQuantity: Number.isFinite(snapshotUom.defaultSalesUnitQuantity) ? snapshotUom.defaultSalesUnitQuantity : null
        };
        productOptionsRef.current.set(initialLine.productId, option);
        resolvedProductOption = option;
      }
      const variantTitle = typeof metaRecord.variantTitle === "string" ? metaRecord.variantTitle : null;
      const variantSku = typeof metaRecord.variantSku === "string" ? metaRecord.variantSku : null;
      const variantThumb = typeof metaRecord.variantThumbnail === "string" ? metaRecord.variantThumbnail : productThumbnail;
      if (variantTitle && initialLine.productVariantId) {
        const option = {
          id: initialLine.productVariantId,
          title: variantTitle,
          sku: variantSku,
          thumbnailUrl: variantThumb ?? null
        };
        variantOptionsRef.current.set(initialLine.productVariantId, option);
        resolvedVariantOption = option;
      }
    }
    if (!resolvedProductOption && initialLine.productId && snapshotProduct) {
      const sp = snapshotProduct;
      const snapshotTitle = typeof sp.title === "string" && sp.title.trim().length ? sp.title : initialLine.name ?? initialLine.productId;
      const snapshotSku = typeof sp.sku === "string" && sp.sku.trim().length ? sp.sku : null;
      const snapshotThumb = typeof sp.thumbnailUrl === "string" ? sp.thumbnailUrl : typeof sp.thumbnail_url === "string" ? sp.thumbnail_url : null;
      const snapshotTaxRate = normalizeNumber(sp.taxRate, Number.NaN);
      const option = {
        id: initialLine.productId,
        title: snapshotTitle,
        sku: snapshotSku,
        thumbnailUrl: snapshotThumb,
        taxRateId: typeof sp.taxRateId === "string" ? sp.taxRateId : null,
        taxRate: Number.isFinite(snapshotTaxRate) ? snapshotTaxRate : null,
        defaultUnit: normalizeUnitCode(sp.defaultUnit ?? sp.default_unit),
        defaultSalesUnit: normalizeUnitCode(sp.defaultSalesUnit ?? sp.default_sales_unit),
        defaultSalesUnitQuantity: (() => {
          const raw = normalizeNumber(sp.defaultSalesUnitQuantity ?? sp.default_sales_unit_quantity, Number.NaN);
          return Number.isFinite(raw) ? raw : null;
        })()
      };
      productOptionsRef.current.set(initialLine.productId, option);
      resolvedProductOption = option;
    }
    if (!resolvedVariantOption && initialLine.productVariantId && snapshotVariant) {
      const sv = snapshotVariant;
      const snapshotTitle = typeof sv.title === "string" && sv.title.trim().length ? sv.title : initialLine.name ?? initialLine.productVariantId;
      const snapshotSku = typeof sv.sku === "string" && sv.sku.trim().length ? sv.sku : null;
      const snapshotThumb = typeof sv.thumbnailUrl === "string" ? sv.thumbnailUrl : typeof sv.thumbnail_url === "string" ? sv.thumbnail_url : resolvedProductOption?.thumbnailUrl ?? productOptionsRef.current.get(initialLine.productId ?? "")?.thumbnailUrl ?? null;
      const snapshotTaxRate = normalizeNumber(sv.taxRate, Number.NaN);
      const option = {
        id: initialLine.productVariantId,
        title: snapshotTitle,
        sku: snapshotSku,
        thumbnailUrl: snapshotThumb,
        taxRateId: typeof sv.taxRateId === "string" ? sv.taxRateId : null,
        taxRate: Number.isFinite(snapshotTaxRate) ? snapshotTaxRate : null
      };
      variantOptionsRef.current.set(initialLine.productVariantId, option);
      resolvedVariantOption = option;
    }
    if (resolvedProductOption) {
      setProductOption(resolvedProductOption);
    } else {
      setProductOption(null);
    }
    if (resolvedVariantOption) {
      setVariantOption(resolvedVariantOption);
    } else {
      setVariantOption(null);
    }
    const customValues = extractCustomFieldValues(
      initialLine
    );
    const merged = { ...nextForm, ...customValues };
    const markDeletedCatalogReference = () => {
      if (cancelled) return;
      const nextMerged = { ...merged, lineMode: "custom" };
      setDeletedCatalogReference(true);
      setProductOption(null);
      setVariantOption(null);
      setPriceOptions([]);
      setUnitOptions([]);
      setInitialValues(nextMerged);
      setLineMode("custom");
      setFormResetKey((prev) => prev + 1);
    };
    setInitialValues(merged);
    setLineMode(merged.lineMode);
    setFormResetKey((prev) => prev + 1);
    if (initialLine.productId) {
      void (async () => {
        try {
          const currentProduct = await loadProductOptionById(
            initialLine.productId
          );
          if (cancelled) return;
          if (!currentProduct) {
            markDeletedCatalogReference();
            return;
          }
          if (merged.lineMode !== "custom") {
            setProductOption(currentProduct);
          }
          if (initialLine.productVariantId) {
            await loadVariantOptions(
              initialLine.productId,
              currentProduct.thumbnailUrl
            );
            if (cancelled) return;
            const currentVariant = variantOptionsRef.current.get(initialLine.productVariantId) ?? null;
            if (!currentVariant) {
              markDeletedCatalogReference();
              return;
            }
            if (merged.lineMode !== "custom") {
              setVariantOption(currentVariant);
            }
          }
          setDeletedCatalogReference(false);
        } catch (err) {
          logger.error("sales.document.items.verifyCatalogReference", { err });
        }
      })();
      void loadProductUnits(initialLine.productId, resolvedProductOption).then(
        (options) => {
          if (cancelled) return;
          const requestedUnit = normalizeUnitCode(nextForm.quantityUnit);
          if (requestedUnit && !options.some((entry) => entry.code === requestedUnit)) {
            setUnitOptions((prev) => [
              ...prev,
              { code: requestedUnit, toBaseFactor: null, isBase: false }
            ]);
          }
        }
      );
      void loadPrices(
        initialLine.productId,
        initialLine.productVariantId,
        nextForm.quantity,
        nextForm.quantityUnit
      );
    } else {
      setPriceOptions([]);
      setUnitOptions([]);
    }
    return () => {
      cancelled = true;
    };
  }, [
    currencyCode,
    findTaxRateIdByValue,
    initialLine,
    loadProductOptionById,
    loadPrices,
    loadProductUnits,
    loadVariantOptions,
    open,
    resetForm
  ]);
  const handleSubmitForm = React.useCallback(
    () => dialogContentRef.current?.querySelector("form")?.requestSubmit(),
    []
  );
  const handleKeyDown = useDialogKeyHandler({
    onConfirm: handleSubmitForm,
    onCancel: closeDialog
  });
  return /* @__PURE__ */ jsx(
    Dialog,
    {
      open,
      onOpenChange: (next) => next ? onOpenChange(true) : closeDialog(),
      children: /* @__PURE__ */ jsxs(
        DialogContent,
        {
          className: "sm:max-w-5xl",
          ref: dialogContentRef,
          onKeyDown: handleKeyDown,
          children: [
            /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsx(DialogTitle, { children: editingId ? t("sales.documents.items.editTitle", "Edit line") : t("sales.documents.items.addTitle", "Add line") }) }),
            /* @__PURE__ */ jsx(
              CrudForm,
              {
                embedded: true,
                fields,
                groups,
                entityId: customFieldEntityId,
                initialValues,
                submitLabel: editingId ? t("sales.documents.items.save", "Save changes") : t("sales.documents.items.addLine", "Add item"),
                onSubmit: handleFormSubmit,
                loadingMessage: t("sales.documents.items.loading", "Loading items\u2026")
              },
              formResetKey
            )
          ]
        }
      )
    }
  );
}
export {
  LineItemDialog
};
//# sourceMappingURL=LineItemDialog.js.map
