import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getColumnSettingFromLocal, saveColumnSettingToLocal } from "@pisell/utils";
//#region src/components/pisellDataSourceContainer/components/ColumnsSetting/utils.ts
const saveColumnSettingToLocal$1 = (params) => {
	saveColumnSettingToLocal({
		componentId: params.componentId,
		columnSetting: params.columnSetting
	});
};
const mergeColumnSettingSimple = ({ columns, componentId }) => {
	if (!componentId) return columns.map((item, index) => {
		var _item$isShow;
		return _objectSpread2(_objectSpread2({}, item), {}, {
			isShow: (_item$isShow = item.isShow) !== null && _item$isShow !== void 0 ? _item$isShow : true,
			sortIndex: index
		});
	});
	const cachedColumns = getColumnSettingFromLocal(componentId);
	if (!cachedColumns || !Array.isArray(cachedColumns)) return columns.map((item, index) => {
		var _item$isShow2;
		return _objectSpread2(_objectSpread2({}, item), {}, {
			isShow: (_item$isShow2 = item.isShow) !== null && _item$isShow2 !== void 0 ? _item$isShow2 : true,
			sortIndex: index
		});
	});
	const cachedMap = new Map(cachedColumns.map((item) => [item.dataIndex, item]));
	const mergedColumns = columns.map((item, index) => {
		var _item$isShow3;
		const cached = cachedMap.get(item.dataIndex);
		if (cached) {
			var _cached$isShow, _cached$sortIndex;
			return _objectSpread2(_objectSpread2({}, item), {}, {
				isShow: (_cached$isShow = cached.isShow) !== null && _cached$isShow !== void 0 ? _cached$isShow : true,
				sortIndex: (_cached$sortIndex = cached.sortIndex) !== null && _cached$sortIndex !== void 0 ? _cached$sortIndex : index
			});
		}
		return _objectSpread2(_objectSpread2({}, item), {}, {
			isShow: (_item$isShow3 = item.isShow) !== null && _item$isShow3 !== void 0 ? _item$isShow3 : true,
			sortIndex: index
		});
	});
	mergedColumns.sort((a, b) => {
		var _a$sortIndex, _b$sortIndex;
		return ((_a$sortIndex = a.sortIndex) !== null && _a$sortIndex !== void 0 ? _a$sortIndex : 0) - ((_b$sortIndex = b.sortIndex) !== null && _b$sortIndex !== void 0 ? _b$sortIndex : 0);
	});
	return mergedColumns;
};
//#endregion
export { mergeColumnSettingSimple, saveColumnSettingToLocal$1 as saveColumnSettingToLocal };
