var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellDataSourceContainer/components/Table/useColumns.ts
var useColumns_exports = {};
__export(useColumns_exports, {
  default: () => useColumns_default
});
module.exports = __toCommonJS(useColumns_exports);
var import_react = require("react");
var import_ColumnsSetting = require("../ColumnsSetting");
var import_utils = require("../ColumnsSetting/utils");
var useColumns = (params) => {
  const { columns, scopeId, refManager, __id } = params;
  const [columnSettings, setColumnSettings] = (0, import_react.useState)([]);
  (0, import_react.useEffect)(() => {
    if (!(columns == null ? void 0 : columns.length)) return;
    const mergedColumns = (0, import_utils.mergeColumnSettingSimple)({
      columns: columns.map((col, index) => ({
        key: col.key || col.dataIndex,
        dataIndex: col.dataIndex,
        title: col.title,
        titleString: typeof col.title === "string" ? col.title : col.dataIndex,
        isShow: typeof col.isShow === "boolean" ? col.isShow : true,
        sortIndex: index,
        fixed: col.fixed
      })),
      componentId: __id
    });
    const settings = mergedColumns.map((col) => ({
      key: col.key,
      dataIndex: col.dataIndex,
      title: col.title,
      titleString: col.titleString,
      isShow: col.isShow,
      sortIndex: col.sortIndex,
      fixed: col.fixed
    }));
    refManager.setSharedState(
      import_ColumnsSetting.COLUMNS_SHARED_STATE_KEYS.columnsValue(scopeId),
      settings
    );
    setColumnSettings(settings);
  }, [columns, refManager, scopeId, __id]);
  (0, import_react.useEffect)(() => {
    const unsubscribe = refManager.subscribeSharedState(
      import_ColumnsSetting.COLUMNS_SHARED_STATE_KEYS.columnsChange(scopeId),
      setColumnSettings
    );
    return unsubscribe;
  }, [refManager, scopeId]);
  return (0, import_react.useMemo)(() => {
    if (!(columns == null ? void 0 : columns.length)) return { visibleColumns: [] };
    if (!(columnSettings == null ? void 0 : columnSettings.length)) return { visibleColumns: columns };
    const columnMap = new Map(columns.map((col) => [col.dataIndex, col]));
    const visibleColumns = columnSettings.filter((setting) => setting.isShow).map((setting) => columnMap.get(setting.dataIndex)).filter(Boolean);
    return { visibleColumns };
  }, [columns, columnSettings]);
};
var useColumns_default = useColumns;
