"use client";
import { createLocalId } from "./productForm.js";
import { isCatalogPriceAmountInputValid } from "../../lib/priceValidation.js";
const VARIANT_BASE_VALUES = {
  name: "",
  sku: "",
  barcode: "",
  gtinType: null,
  hsCode: "",
  isDefault: false,
  isActive: true,
  optionValues: {},
  metadata: {},
  mediaDraftId: "",
  mediaItems: [],
  defaultMediaId: null,
  defaultMediaUrl: "",
  prices: {},
  taxRateId: null,
  customFieldsetCode: null
};
const createVariantInitialValues = () => ({
  ...VARIANT_BASE_VALUES,
  mediaDraftId: createLocalId()
});
function normalizeOptionSchema(raw) {
  if (!Array.isArray(raw)) return [];
  return raw.map((entry) => normalizeOptionDefinition(entry)).filter((entry) => !!entry);
}
function normalizeOptionDefinition(entry) {
  if (!entry || typeof entry !== "object") return null;
  const code = extractString(entry.code) || createLocalId();
  const label = extractString(entry.label) || code;
  const values = Array.isArray(entry.values) ? entry.values.map((value) => {
    const id = extractString(value?.id) || createLocalId();
    const valueLabel = extractString(value?.label) || id;
    return { id, label: valueLabel };
  }).filter(
    (value) => value.label.length > 0
  ) : [];
  return {
    id: extractString(entry.id) || createLocalId(),
    code,
    label,
    values
  };
}
function extractString(value) {
  return typeof value === "string" ? value.trim() : "";
}
function buildVariantMetadata(values) {
  const metadata = typeof values.metadata === "object" && values.metadata ? { ...values.metadata } : {};
  return metadata;
}
function findInvalidVariantPriceKinds(priceKinds, priceDrafts) {
  const invalid = [];
  for (const kind of priceKinds) {
    const draft = priceDrafts?.[kind.id];
    const amount = typeof draft?.amount === "string" ? draft.amount.trim() : "";
    if (!amount) continue;
    if (!isCatalogPriceAmountInputValid(amount)) invalid.push(kind.id);
  }
  return invalid;
}
function mapPriceItemToDraft(item, kindDisplayModes) {
  const kindId = typeof item.price_kind_id === "string" ? item.price_kind_id : typeof item.priceKindId === "string" ? item.priceKindId : null;
  if (!kindId) return null;
  const unitNet = typeof item.unit_price_net === "string" ? item.unit_price_net : typeof item.unitPriceNet === "string" ? item.unitPriceNet : null;
  const unitGross = typeof item.unit_price_gross === "string" ? item.unit_price_gross : typeof item.unitPriceGross === "string" ? item.unitPriceGross : null;
  const kindMode = kindDisplayModes.get(kindId) ?? (unitGross ? "including-tax" : "excluding-tax");
  return {
    priceKindId: kindId,
    priceId: typeof item.id === "string" ? item.id : void 0,
    amount: kindMode === "including-tax" ? unitGross ?? unitNet ?? "" : unitNet ?? unitGross ?? "",
    currencyCode: typeof item.currency_code === "string" ? item.currency_code : typeof item.currencyCode === "string" ? item.currencyCode : null,
    displayMode: kindMode,
    updatedAt: typeof item.updatedAt === "string" ? item.updatedAt : typeof item.updated_at === "string" ? item.updated_at : null
  };
}
export {
  VARIANT_BASE_VALUES,
  buildVariantMetadata,
  createVariantInitialValues,
  findInvalidVariantPriceKinds,
  mapPriceItemToDraft,
  normalizeOptionSchema
};
//# sourceMappingURL=variantForm.js.map
