"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import {
  DictionaryValue
} from "@open-mercato/core/modules/dictionaries/components/dictionaryAppearance";
import { cn } from "@open-mercato/shared/lib/utils";
import {
  extractDictionaryValue,
  isEmptyCustomValue,
  normalizeCustomFieldKey,
  resolveCustomFieldLabel,
  stringifyCustomValue
} from "./customFieldUtils.js";
function defaultEmptyLabel(label) {
  if (typeof label === "string" && label.trim().length) return label;
  return "\u2014";
}
function ensureDictionaryMap(key, normalizedKey, dictionaryMaps) {
  if (!dictionaryMaps) return null;
  if (dictionaryMaps[key]) return dictionaryMaps[key];
  if (dictionaryMaps[normalizedKey]) return dictionaryMaps[normalizedKey];
  return null;
}
function renderDictionaryValues(values, dictionaryMap, multi) {
  if (!dictionaryMap) return null;
  if (multi || Array.isArray(values)) {
    const resolved2 = (Array.isArray(values) ? values : [values]).map((entry) => extractDictionaryValue(entry)).filter((entry) => typeof entry === "string" && entry.length > 0);
    if (!resolved2.length) return null;
    return /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-1.5", children: resolved2.map((value, index) => /* @__PURE__ */ jsx(
      DictionaryValue,
      {
        value,
        map: dictionaryMap,
        className: "inline-flex items-center gap-1 rounded-full border border-border bg-card px-2 py-1 text-xs",
        iconWrapperClassName: "inline-flex h-4 w-4 items-center justify-center rounded-full border border-border bg-background",
        iconClassName: "h-3 w-3",
        colorClassName: "h-2.5 w-2.5 rounded-full"
      },
      `${value}-${index}`
    )) });
  }
  const resolved = extractDictionaryValue(values);
  if (!resolved) return null;
  return /* @__PURE__ */ jsx(
    DictionaryValue,
    {
      value: resolved,
      map: dictionaryMap,
      className: "inline-flex items-center gap-2 text-sm"
    }
  );
}
function renderPrimitiveValues(value) {
  if (Array.isArray(value)) {
    const parts = value.map((entry) => stringifyCustomValue(entry)).map((entry) => entry.trim()).filter((entry) => entry.length > 0);
    if (!parts.length) return null;
    return /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-1.5", children: parts.map((entry, index) => /* @__PURE__ */ jsx(
      "span",
      {
        className: "inline-flex items-center rounded-full bg-muted px-2 py-0.5 text-xs text-muted-foreground",
        children: entry
      },
      `${entry}-${index}`
    )) });
  }
  const label = stringifyCustomValue(value).trim();
  if (!label.length) return null;
  return /* @__PURE__ */ jsx("span", { className: "text-sm text-foreground", children: label });
}
function buildDisplayEntries(values, entries, definitions, dictionaryMaps) {
  const combined = /* @__PURE__ */ new Map();
  if (values) {
    Object.entries(values).forEach(([rawKey, value]) => {
      const normalizedKey = normalizeCustomFieldKey(rawKey);
      if (!normalizedKey) return;
      if (!combined.has(normalizedKey)) {
        combined.set(normalizedKey, { key: rawKey, value });
      } else {
        const existing = combined.get(normalizedKey);
        if (existing) {
          existing.value = value;
        }
      }
    });
  }
  if (entries) {
    entries.forEach((entry) => {
      const normalizedKey = normalizeCustomFieldKey(entry.key);
      if (!normalizedKey) return;
      const existing = combined.get(normalizedKey);
      if (existing) {
        combined.set(normalizedKey, {
          key: existing.key,
          value: entry.value,
          label: entry.label ?? existing.label
        });
      } else {
        combined.set(normalizedKey, {
          key: entry.key,
          value: entry.value,
          label: entry.label
        });
      }
    });
  }
  const ordered = [];
  const consumedKeys = /* @__PURE__ */ new Set();
  definitions.forEach((def, index) => {
    const normalizedKey = normalizeCustomFieldKey(def.key);
    if (!normalizedKey) return;
    const entry = combined.get(normalizedKey);
    if (!entry || isEmptyCustomValue(entry.value)) return;
    const label = resolveCustomFieldLabel(entry.label ?? def.label, entry.key);
    const dictionaryMap = ensureDictionaryMap(entry.key, normalizedKey, dictionaryMaps);
    ordered.push({
      id: `${normalizedKey}-${index}`,
      key: entry.key,
      normalizedKey,
      label,
      value: entry.value,
      dictionaryMap,
      multi: def.multi ?? Array.isArray(entry.value)
    });
    consumedKeys.add(normalizedKey);
  });
  const extras = [];
  combined.forEach((entry, normalizedKey) => {
    if (consumedKeys.has(normalizedKey)) return;
    if (isEmptyCustomValue(entry.value)) return;
    const label = resolveCustomFieldLabel(entry.label, entry.key);
    const dictionaryMap = ensureDictionaryMap(entry.key, normalizedKey, dictionaryMaps);
    extras.push({
      id: normalizedKey,
      key: entry.key,
      normalizedKey,
      label,
      value: entry.value,
      dictionaryMap,
      multi: Array.isArray(entry.value)
    });
  });
  extras.sort((a, b) => a.label.localeCompare(b.label));
  return [...ordered, ...extras];
}
function CustomFieldValuesList({
  values = null,
  entries,
  definitions,
  dictionaryMapsByKey,
  resources,
  emptyLabel,
  className,
  itemKeyPrefix
}) {
  const displayEntries = React.useMemo(() => {
    const defs = definitions ?? resources?.definitions ?? [];
    const maps = dictionaryMapsByKey ?? resources?.dictionaryMapsByKey ?? {};
    return buildDisplayEntries(values, entries, defs, maps);
  }, [definitions, dictionaryMapsByKey, entries, resources, values]);
  if (!displayEntries.length) return null;
  const resolvedEmptyLabel = defaultEmptyLabel(emptyLabel);
  const prefix = itemKeyPrefix ?? "custom-field";
  return /* @__PURE__ */ jsx("div", { className: cn("grid gap-3 sm:grid-cols-2", className), children: displayEntries.map((entry, index) => {
    const dictionaryContent = renderDictionaryValues(entry.value, entry.dictionaryMap, entry.multi);
    const primitiveContent = dictionaryContent ?? renderPrimitiveValues(entry.value);
    const content = dictionaryContent ?? primitiveContent ?? /* @__PURE__ */ jsx("span", { className: "text-sm text-muted-foreground", children: resolvedEmptyLabel });
    return /* @__PURE__ */ jsxs(
      "div",
      {
        className: "rounded-md border border-border/70 bg-muted/30 px-3 py-2",
        children: [
          /* @__PURE__ */ jsx("div", { className: "text-xs font-medium text-muted-foreground", children: entry.label }),
          /* @__PURE__ */ jsx("div", { className: "mt-1 text-sm text-foreground", children: content })
        ]
      },
      `${prefix}-${entry.normalizedKey}-${index}`
    );
  }) });
}
var CustomFieldValuesList_default = CustomFieldValuesList;
export {
  CustomFieldValuesList,
  CustomFieldValuesList_default as default
};
//# sourceMappingURL=CustomFieldValuesList.js.map
