"use client";
import * as React from "react";
import {
  mapCustomFieldKindToFilterType,
  normalizeCustomFieldFilterOptions,
  supportsCustomFieldColumn
} from "./customFieldColumns.js";
function resolveHeaderLabel(column) {
  const header = column.header;
  if (typeof header === "string") return header;
  const accessorKey = column.accessorKey;
  if (!accessorKey) return "";
  return accessorKey.replace(/^cf_/, "").replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
}
function inferFilterType(accessorKey, meta) {
  if (meta?.filterType) return meta.filterType;
  if (accessorKey.endsWith("_at") || accessorKey === "createdAt" || accessorKey === "updatedAt" || accessorKey === "expectedCloseAt" || accessorKey.endsWith("At")) return "date";
  if (accessorKey === "probability" || accessorKey === "valueAmount" || accessorKey === "value_amount") return "number";
  if (meta?.filterOptions) return "select";
  return "text";
}
function useAutoDiscoveredFields({
  columns,
  customFieldDefs
}) {
  return React.useMemo(() => {
    const filterFields = [];
    const chooserFields = [];
    const seenFilterKeys = /* @__PURE__ */ new Set();
    const seenChooserKeys = /* @__PURE__ */ new Set();
    for (let i = 0; i < columns.length; i++) {
      const col = columns[i];
      const accessorKey = col.accessorKey;
      if (!accessorKey) continue;
      const meta = col.meta;
      const label = resolveHeaderLabel(col);
      if (!label) continue;
      const filterKey = meta?.filterKey ?? accessorKey;
      if (meta?.filterable !== false && filterKey && !seenFilterKeys.has(filterKey)) {
        seenFilterKeys.add(filterKey);
        const type = inferFilterType(filterKey, meta);
        const field = {
          key: filterKey,
          label,
          type,
          group: meta?.filterGroup ?? meta?.columnChooserGroup
        };
        if (meta?.filterIconName) field.iconName = meta.filterIconName;
        if (meta?.filterOptions) field.options = meta.filterOptions;
        if (meta?.filterLoadOptions) field.loadOptions = meta.filterLoadOptions;
        filterFields.push(field);
      }
      if (!seenChooserKeys.has(accessorKey)) {
        seenChooserKeys.add(accessorKey);
        chooserFields.push({
          key: accessorKey,
          label,
          group: meta?.columnChooserGroup ?? "Columns",
          alwaysVisible: meta?.alwaysVisible ?? i === 0,
          defaultVisible: true
        });
      }
    }
    for (const def of customFieldDefs) {
      if (!supportsCustomFieldColumn(def)) continue;
      const filterKey = `cf_${def.key}`;
      if (!seenFilterKeys.has(filterKey)) {
        seenFilterKeys.add(filterKey);
        const type = mapCustomFieldKindToFilterType(def.kind);
        const field = {
          key: filterKey,
          label: def.label || def.key,
          type,
          group: "Custom Fields"
        };
        if (type === "select" && Array.isArray(def.options) && def.options.length) {
          field.options = normalizeCustomFieldFilterOptions(def.options);
        }
        filterFields.push(field);
      }
      if (!seenChooserKeys.has(filterKey)) {
        seenChooserKeys.add(filterKey);
        chooserFields.push({
          key: filterKey,
          label: def.label || def.key,
          group: def.group?.title ?? "Custom Fields",
          defaultVisible: false
        });
      }
    }
    return { advancedFilterFields: filterFields, columnChooserFields: chooserFields };
  }, [columns, customFieldDefs]);
}
export {
  useAutoDiscoveredFields
};
//# sourceMappingURL=useAutoDiscoveredFields.js.map
