var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellRecordBoard/utils/recordBoardColumns.tsx
var recordBoardColumns_exports = {};
__export(recordBoardColumns_exports, {
  computeUniformColumnSummary: () => computeUniformColumnSummary,
  deriveFilterFromColumns: () => deriveFilterFromColumns,
  deriveSortFromColumns: () => deriveSortFromColumns,
  getRecordBoardColumnKey: () => getRecordBoardColumnKey,
  processColumnsForGrid: () => processColumnsForGrid
});
module.exports = __toCommonJS(recordBoardColumns_exports);
var import_react = __toESM(require("react"));
var import_pisellSingleLineText = require("../../pisellSingleLineText");
var import_pisellLongText = require("../../pisellLongText");
var import_pisellEmail = require("../../pisellEmail");
var import_pisellPhone = require("../../pisellPhone");
var import_pisellUrl = require("../../pisellUrl");
var import_pisellNumber = require("../../pisellNumber");
var import_pisellCurrency = require("../../pisellCurrency");
var import_pisellPercent = require("../../pisellPercent");
var import_pisellRating = require("../../pisellRating");
var import_pisellSingleSelect = require("../../pisellSingleSelect");
var import_pisellMultipleSelect = require("../../pisellMultipleSelect");
var import_pisellDateTimeDisplay = require("../../pisellDateTimeDisplay");
var import_locales = require("../../../locales");
var FIELD_TYPE_TO_READ_COMPONENT = {
  singleLineText: import_pisellSingleLineText.PisellSingleLineText,
  longText: import_pisellLongText.PisellLongText,
  email: import_pisellEmail.PisellEmail,
  phone: import_pisellPhone.PisellPhone,
  url: import_pisellUrl.PisellUrl,
  number: import_pisellNumber.PisellNumber,
  currency: import_pisellCurrency.PisellCurrency,
  percent: import_pisellPercent.PisellPercent,
  rating: import_pisellRating.PisellRating,
  singleSelect: import_pisellSingleSelect.PisellSingleSelect,
  multipleSelect: import_pisellMultipleSelect.PisellMultipleSelect,
  dateTime: import_pisellDateTimeDisplay.PisellDateTimeDisplay
};
var FIELD_TYPE_TO_SORT_OPTIONS_DEFAULT = {
  singleLineText: { type: "string" },
  longText: { type: "string" },
  email: { type: "string" },
  phone: { type: "string" },
  url: { type: "string" },
  number: { type: "number" },
  currency: { type: "number" },
  percent: { type: "number" },
  rating: { type: "number" },
  singleSelect: { type: "option" },
  multipleSelect: { type: "option" },
  dateTime: { type: "date" }
};
function getSortOptionForFieldType(fieldType) {
  const Com = FIELD_TYPE_TO_READ_COMPONENT[fieldType];
  const fromComponent = Com && Com.SortOptions;
  const fallback = FIELD_TYPE_TO_SORT_OPTIONS_DEFAULT[fieldType];
  const opts = fromComponent ?? fallback;
  return opts ? { ...fallback, ...opts } : null;
}
var FIELD_TYPE_TO_FILTER_TYPE = {
  singleLineText: "input",
  longText: "input",
  email: "input",
  phone: "input",
  url: "input",
  number: "input",
  currency: "input",
  percent: "input",
  rating: "single",
  singleSelect: "single",
  multipleSelect: "single",
  dateTime: "rangePickerNew"
};
function getColumnKey(column) {
  const key = column.key ?? (Array.isArray(column.dataIndex) ? column.dataIndex.join(".") : column.dataIndex);
  return typeof key === "string" ? key : "";
}
function getRecordBoardColumnKey(column) {
  return getColumnKey(column);
}
function getColumnLabel(column) {
  const title = column.title;
  if (typeof title === "string") return title;
  return getColumnKey(column) || "";
}
function getRecordValue(record, dataIndex) {
  if (record == null || dataIndex == null) return void 0;
  const path = Array.isArray(dataIndex) ? dataIndex : [dataIndex];
  return path.reduce((r, k) => r != null && typeof r === "object" ? r[k] : void 0, record);
}
function isEmptyValue(v) {
  return v == null || v === "";
}
function isComparablePrimitive(v) {
  return typeof v === "string" || typeof v === "number";
}
function computeUniformColumnSummary(data, columns) {
  const uniformHiddenColumnKeys = [];
  const summaryItems = [];
  if (!Array.isArray(data) || data.length === 0) return { uniformHiddenColumnKeys, summaryItems };
  for (const column of columns) {
    const collapseWhenUniform = column.collapseWhenUniform !== false;
    if (!collapseWhenUniform) continue;
    const columnKey = getColumnKey(column);
    if (!columnKey) continue;
    const dataIndex = column.dataIndex;
    const getVal = typeof column.uniformCompare === "function" ? (record) => column.uniformCompare(
      getRecordValue(record, Array.isArray(dataIndex) ? dataIndex : dataIndex),
      record
    ) : (record) => getRecordValue(record, Array.isArray(dataIndex) ? dataIndex : dataIndex);
    const values = data.map(getVal);
    const first = values[0];
    if (first === void 0 || first === null) continue;
    const cellValue = getRecordValue(data[0], Array.isArray(dataIndex) ? dataIndex : dataIndex);
    if (values.every((v) => isEmptyValue(v))) {
      uniformHiddenColumnKeys.push(columnKey);
      summaryItems.push({
        columnKey,
        title: getColumnLabel(column),
        type: "empty",
        value: void 0,
        cellValue,
        record: data[0]
      });
      continue;
    }
    const comparable = isComparablePrimitive(first);
    if (!comparable) continue;
    const allSame = values.every((v) => isEmptyValue(v) && isEmptyValue(first) || v === first);
    if (!allSame) continue;
    uniformHiddenColumnKeys.push(columnKey);
    summaryItems.push({
      columnKey,
      title: getColumnLabel(column),
      type: "uniform",
      displayValue: first != null && first !== "" ? String(first) : void 0,
      value: first,
      cellValue,
      record: data[0]
    });
  }
  return { uniformHiddenColumnKeys, summaryItems };
}
function normalizeOptions(list) {
  if (!Array.isArray(list)) return [];
  return list.map(
    (item) => item && typeof item === "object" && "label" in item && "value" in item ? { label: String(item.label), value: item.value } : { label: String(item), value: item }
  );
}
function AsyncOptionsReadCell({
  getOptions,
  value,
  fieldType,
  fieldProps
}) {
  const [options, setOptions] = (0, import_react.useState)([]);
  const Com = FIELD_TYPE_TO_READ_COMPONENT[fieldType];
  (0, import_react.useEffect)(() => {
    const result = getOptions();
    const resolved = Promise.resolve(result);
    resolved.then((list) => setOptions(normalizeOptions(list)));
  }, [getOptions]);
  if (Com == null) return value;
  const common = { value, ...fieldProps, options };
  if (fieldType === "singleSelect" || fieldType === "multipleSelect") {
    return /* @__PURE__ */ import_react.default.createElement(Com, { ...common, readOnly: true });
  }
  return /* @__PURE__ */ import_react.default.createElement(Com, { ...common, mode: "read" });
}
function createReadRender(column, fieldType) {
  const Com = FIELD_TYPE_TO_READ_COMPONENT[fieldType];
  const fieldProps = column.fieldProps ?? {};
  const getOptions = fieldProps.getOptions;
  const useAsyncOptions = (fieldType === "singleSelect" || fieldType === "multipleSelect") && typeof getOptions === "function";
  return (value) => {
    if (Com == null) return value;
    if (useAsyncOptions) {
      return /* @__PURE__ */ import_react.default.createElement(
        AsyncOptionsReadCell,
        {
          getOptions,
          value,
          fieldType,
          fieldProps
        }
      );
    }
    const common = { value, ...fieldProps };
    if (fieldType === "singleSelect" || fieldType === "multipleSelect") {
      return /* @__PURE__ */ import_react.default.createElement(Com, { ...common, readOnly: true });
    }
    if (fieldType === "dateTime") {
      return /* @__PURE__ */ import_react.default.createElement(Com, { ...common });
    }
    return /* @__PURE__ */ import_react.default.createElement(Com, { ...common, mode: "read" });
  };
}
function processColumnsForGrid(columns) {
  if (!Array.isArray(columns) || columns.length === 0) return columns ?? [];
  return columns.map((col) => {
    const fieldType = col.type;
    if (!fieldType || !FIELD_TYPE_TO_READ_COMPONENT[fieldType]) {
      return col;
    }
    const existingRender = col.render;
    const readRender = createReadRender(col, fieldType);
    return {
      ...col,
      render: existingRender ?? ((value, record, index) => readRender(value, record, index))
    };
  });
}
function columnToFilterItem(column) {
  const key = getColumnKey(column);
  const label = getColumnLabel(column);
  if (!key) return null;
  if (column.filterable === false) return null;
  const hasExplicitFilter = column.filter && (column.filter.type != null || column.filter.component);
  if (column.type && !hasExplicitFilter) {
    const fieldType = column.type;
    const filterType2 = FIELD_TYPE_TO_FILTER_TYPE[fieldType];
    const fieldProps = column.fieldProps ?? {};
    const hasGetOptions2 = typeof fieldProps.getOptions === "function";
    const options = fieldType === "singleSelect" || fieldType === "multipleSelect" || fieldType === "rating" ? fieldProps.options ?? (fieldType === "rating" ? [1, 2, 3, 4, 5].map((v) => ({ label: String(v), value: v })) : hasGetOptions2 ? void 0 : []) : void 0;
    return {
      type: filterType2,
      key,
      name: key,
      label,
      props: filterType2 === "single" ? {
        options: options ?? [],
        getOptions: fieldProps.getOptions,
        ...fieldProps
      } : {
        placeholder: fieldProps.placeholder ?? String(
          (0, import_locales.getText)("pisell2.recordBoard.filter.placeholderEnter") ?? ""
        ).replace(/\{label\}/g, label),
        ...fieldProps
      },
      other: { fieldType, getOptions: fieldProps.getOptions, ...fieldProps }
    };
  }
  if (!column.filter) return null;
  const f = column.filter;
  if (f.component) {
    return {
      type: "custom",
      key,
      name: key,
      label: f.label ?? label,
      props: f.props ?? {},
      other: { component: f.component, ...f.props || {} }
    };
  }
  const filterType = f.type;
  if (!filterType) return null;
  const hasGetOptions = typeof f.getOptions === "function";
  return {
    type: filterType,
    key,
    name: key,
    label: f.label ?? label,
    props: f.props ?? (filterType === "single" ? { options: f.options ?? [], getOptions: f.getOptions } : {}),
    other: filterType === "single" && hasGetOptions ? { getOptions: f.getOptions } : {}
  };
}
function columnToSortItem(column) {
  const key = getColumnKey(column);
  const label = getColumnLabel(column);
  if (!key) return null;
  if (column.sortable === false) return null;
  const hasExplicitSort = column.sort && (column.sort.type != null || typeof column.sort.getSortItems === "function");
  if (column.type && !hasExplicitSort) {
    const fieldType = column.type;
    if (!FIELD_TYPE_TO_READ_COMPONENT[fieldType]) return null;
    const opts = getSortOptionForFieldType(fieldType);
    if (!opts) return null;
    const hasGetSortItems2 = "getSortItems" in opts && typeof opts.getSortItems === "function";
    return {
      ...hasGetSortItems2 ? {} : { type: "type" in opts ? opts.type : void 0 },
      ...hasGetSortItems2 && "getSortItems" in opts ? { getSortItems: opts.getSortItems } : {},
      name: key,
      label: "label" in opts ? opts.label ?? label : label,
      isCustom: false,
      ..."localFilter" in opts && opts.localFilter !== void 0 && { localFilter: opts.localFilter },
      ..."isHidden" in opts && opts.isHidden !== void 0 && { isHidden: opts.isHidden }
    };
  }
  if (!column.type && column.sortable !== true) return null;
  if (!column.sort) return null;
  const s = column.sort;
  const hasGetSortItems = typeof s.getSortItems === "function";
  const sortItem = {
    name: key,
    label: s.label ?? label,
    isCustom: false,
    ...s.isHidden !== void 0 && { isHidden: s.isHidden }
  };
  if (hasGetSortItems && s.getSortItems) {
    sortItem.getSortItems = s.getSortItems;
  } else if (s.type != null) {
    sortItem.type = s.type;
  }
  return sortItem;
}
function deriveSortFromColumns(columns) {
  if (!Array.isArray(columns) || columns.length === 0) return [];
  const list = [];
  for (const col of columns) {
    const hasType = Boolean(col.type && FIELD_TYPE_TO_READ_COMPONENT[col.type]);
    if (hasType && col.sortable === false) continue;
    const item = columnToSortItem(col);
    if (item) list.push(item);
  }
  return list;
}
function deriveFilterFromColumns(columns) {
  var _a, _b;
  const quickFilter = [];
  const otherFilter = [];
  if (!Array.isArray(columns) || columns.length === 0) {
    return { quickFilter, otherFilter };
  }
  for (const col of columns) {
    const key = getColumnKey(col);
    if (!key) continue;
    const hasType = Boolean(col.type && FIELD_TYPE_TO_READ_COMPONENT[col.type]);
    if (hasType && col.filterable === false) continue;
    const useQuick = hasType ? ((_a = col.filter) == null ? void 0 : _a.quickFilter) === true : Boolean(col.filterable && ((_b = col.filter) == null ? void 0 : _b.quickFilter));
    const item = columnToFilterItem(col);
    if (!item) continue;
    if (useQuick) {
      quickFilter.push(item);
    } else {
      otherFilter.push(item);
    }
  }
  return { quickFilter, otherFilter };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  computeUniformColumnSummary,
  deriveFilterFromColumns,
  deriveSortFromColumns,
  getRecordBoardColumnKey,
  processColumnsForGrid
});
