import { isDefVisible } from "./customFieldDefs.js";
function supportsCustomFieldColumn(def) {
  return def.kind !== "attachment";
}
function mapCustomFieldKindToFilterType(kind) {
  switch (kind) {
    case "boolean":
      return "boolean";
    case "integer":
    case "float":
      return "number";
    case "date":
    case "datetime":
      return "date";
    case "select":
    case "dictionary":
    case "currency":
    case "relation":
      return "select";
    default:
      return "text";
  }
}
function normalizeCustomFieldFilterOptions(options) {
  if (!Array.isArray(options)) return [];
  return options.map((option) => {
    if (option && typeof option === "object" && "value" in option) {
      const rawValue = option.value;
      const rawLabel = option.label ?? rawValue;
      return {
        value: String(rawValue),
        label: typeof rawLabel === "string" ? rawLabel : String(rawLabel)
      };
    }
    const value = String(option);
    return {
      value,
      label: value.charAt(0).toUpperCase() + value.slice(1)
    };
  });
}
function applyCustomFieldVisibility(columns, defs, mode = "list") {
  const byKey = new Map(defs.map((d) => [d.key, d]));
  const filtered = columns.filter((c) => {
    const key = String(c.accessorKey || "");
    if (!key.startsWith("cf_")) return true;
    const cfKey = key.slice(3);
    const def = byKey.get(cfKey);
    if (!def) return false;
    if (!supportsCustomFieldColumn(def)) return false;
    if (!isDefVisible(def, mode)) return false;
    const currentHeader = c.header;
    const fallbackHeader = typeof currentHeader === "string" && currentHeader.trim().length ? currentHeader : key;
    const label = def.label && def.label.trim().length ? def.label : fallbackHeader;
    if (currentHeader == null || typeof currentHeader === "string") {
      c.header = label;
    }
    const existingMeta = c.meta || {};
    const nextMeta = Object.assign({}, existingMeta, { label });
    c.meta = nextMeta;
    return true;
  });
  const cfEntries = [];
  filtered.forEach((c) => {
    const key = String(c.accessorKey || "");
    if (key.startsWith("cf_")) {
      const cfKey = key.slice(3);
      const def = byKey.get(cfKey);
      cfEntries.push({ col: c, key: cfKey, prio: def?.priority ?? 0 });
    }
  });
  cfEntries.sort((a, b) => a.prio - b.prio);
  let cfIdx = 0;
  const result = filtered.map((c) => {
    const key = String(c.accessorKey || "");
    if (!key.startsWith("cf_")) return c;
    const next = cfEntries[cfIdx++]?.col ?? c;
    return next;
  });
  const existingCfKeys = new Set(result.map((c) => String(c.accessorKey || "")).filter((k) => k.startsWith("cf_")).map((k) => k.slice(3)));
  const visibleSorted = defs.filter((d) => supportsCustomFieldColumn(d) && isDefVisible(d, mode)).sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
  const missing = visibleSorted.filter((d) => !existingCfKeys.has(d.key));
  for (const d of missing) {
    const col = {
      accessorKey: `cf_${d.key}`,
      header: d.label || `cf_${d.key}`,
      // Respect responsive priority when provided; default leaves it visible
      meta: { priority: d.priority, label: d.label || `cf_${d.key}` }
    };
    result.push(col);
  }
  return result;
}
export {
  applyCustomFieldVisibility,
  mapCustomFieldKindToFilterType,
  normalizeCustomFieldFilterOptions,
  supportsCustomFieldColumn
};
//# sourceMappingURL=customFieldColumns.js.map
