function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useState, useEffect, useMemo } from 'react';
import { COLUMNS_SHARED_STATE_KEYS } from "../ColumnsSetting";
import { mergeColumnSettingSimple } from "../ColumnsSetting/utils";

/**
 * useColumns Hook 参数
 */

var useColumns = function useColumns(params) {
  var columns = params.columns,
    scopeId = params.scopeId,
    refManager = params.refManager,
    __id = params.__id;

  // 本地状态：存储列配置
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    columnSettings = _useState2[0],
    setColumnSettings = _useState2[1];
  useEffect(function () {
    if (!(columns !== null && columns !== void 0 && columns.length)) return;

    // 🔄 合并本地缓存和默认配置
    var mergedColumns = mergeColumnSettingSimple({
      columns: columns.map(function (col, index) {
        return {
          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
    });
    var settings = mergedColumns.map(function (col) {
      return {
        key: col.key,
        dataIndex: col.dataIndex,
        title: col.title,
        titleString: col.titleString,
        isShow: col.isShow,
        sortIndex: col.sortIndex,
        fixed: col.fixed
      };
    });

    // 写入共享状态（使用统一的 key，供 ColumnsSetting 读取）
    refManager.setSharedState(COLUMNS_SHARED_STATE_KEYS.columnsValue(scopeId), settings);
    setColumnSettings(settings);
  }, [columns, refManager, scopeId, __id]);

  /**
   * 订阅：监听 ColumnsSetting 的修改
   */
  useEffect(function () {
    var unsubscribe = refManager.subscribeSharedState(COLUMNS_SHARED_STATE_KEYS.columnsChange(scopeId), setColumnSettings);
    return unsubscribe;
  }, [refManager, scopeId]);
  return useMemo(function () {
    if (!(columns !== null && columns !== void 0 && columns.length)) return {
      visibleColumns: []
    };
    if (!(columnSettings !== null && columnSettings !== void 0 && columnSettings.length)) return {
      visibleColumns: columns
    };

    // 创建 dataIndex 到原始 column 的映射
    var columnMap = new Map(columns.map(function (col) {
      return [col.dataIndex, col];
    }));
    var visibleColumns = columnSettings.filter(function (setting) {
      return setting.isShow;
    }).map(function (setting) {
      return columnMap.get(setting.dataIndex);
    }).filter(Boolean);
    return {
      visibleColumns: visibleColumns
    };
  }, [columns, columnSettings]);
};
export default useColumns;