require("../../../../_virtual/_rolldown/runtime.js");
const require_utils = require("../ColumnsSetting/utils.js");
const require_index = require("../ColumnsSetting/index.js");
let react = require("react");
//#region src/components/pisellDataSourceContainer/components/Table/useColumns.ts
const useColumns = (params) => {
	const { columns, scopeId, refManager, __id } = params;
	const [columnSettings, setColumnSettings] = (0, react.useState)([]);
	(0, react.useEffect)(() => {
		if (!(columns === null || columns === void 0 ? void 0 : columns.length)) return;
		const settings = require_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
		}).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(require_index.COLUMNS_SHARED_STATE_KEYS.columnsValue(scopeId), settings);
		setColumnSettings(settings);
	}, [
		columns,
		refManager,
		scopeId,
		__id
	]);
	/**
	* 订阅：监听 ColumnsSetting 的修改
	*/
	(0, react.useEffect)(() => {
		return refManager.subscribeSharedState(require_index.COLUMNS_SHARED_STATE_KEYS.columnsChange(scopeId), setColumnSettings);
	}, [refManager, scopeId]);
	return (0, react.useMemo)(() => {
		if (!(columns === null || columns === void 0 ? void 0 : columns.length)) return { visibleColumns: [] };
		if (!(columnSettings === null || columnSettings === void 0 ? void 0 : columnSettings.length)) return { visibleColumns: columns };
		const columnMap = new Map(columns.map((col) => [col.dataIndex, col]));
		return { visibleColumns: columnSettings.filter((setting) => setting.isShow).map((setting) => columnMap.get(setting.dataIndex)).filter(Boolean) };
	}, [columns, columnSettings]);
};
//#endregion
exports.default = useColumns;
