const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useSharedState = require("../../hooks/useSharedState.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_model = require("./model.js");
const require_index = require("./Tabs/index.js");
const require_index$1 = require("./Actions/index.js");
const require_index$2 = require("./Header/index.js");
const require_index$3 = require("./Summary/index.js");
const require_utils = require("./Table/utils.js");
const require_index$4 = require("./View/index.js");
const require_useTableSetting = require("./hooks/useTableSetting.js");
const require_useTransDataSourceGroupSetting = require("./hooks/useTransDataSourceGroupSetting.js");
const require_useTransFilterSetting = require("./hooks/useTransFilterSetting.js");
const require_useTransSortSetting = require("./hooks/useTransSortSetting.js");
const require_useTriggerValuesChange = require("./hooks/useTriggerValuesChange.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/table/index.tsx
const _excluded = [
	"dataSource",
	"columns",
	"dispatch",
	"style",
	"filter",
	"sort",
	"buttons",
	"actionButtons",
	"title",
	"showTitle",
	"subTitle",
	"showSubTitle",
	"summary",
	"onValuesChange",
	"columnSetting",
	"dataSourceGroup",
	"localSettings",
	"view",
	"gallery",
	"currentSettingKey",
	"exportImport",
	"currentComponentId",
	"tabs"
];
const defaultView = {
	defaultViewMode: "grid",
	multiple: false,
	modeList: []
};
const GridView = require_model.Provider((0, react.forwardRef)((props, ref) => {
	var _context$appHelper, _other$pagination2, _other$pagination5, _other$pagination6, _other$pagination8;
	const { dataSource, columns: propsColumns, dispatch, style, filter: propsFilter, sort: propsSort, buttons, actionButtons, title, showTitle = true, subTitle, showSubTitle = true, summary, onValuesChange, columnSetting, dataSourceGroup: propsDataSourceGroup, localSettings: _localSettings, view = defaultView, gallery, currentSettingKey, exportImport, currentComponentId, tabs } = props, other = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	const modal = require_useSharedState.default(require_model.Context);
	const [form] = antd.Form.useForm();
	const { defaultViewMode, multiple } = view;
	const viewMode = antd.Form.useWatch("view_mode", {
		form,
		preserve: true
	});
	const context = require_useEngineContext.default();
	const wrapRef = (0, react.useRef)(null);
	const tableSettingRef = (0, react.useRef)({});
	const filterDefaultRef = (0, react.useRef)({});
	const initialValuesRef = (0, react.useRef)({});
	const { width } = (0, ahooks.useSize)(wrapRef) || {};
	const debouncedWidth = (0, ahooks.useDebounce)(width, { wait: 100 });
	const utils = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils;
	const isTerminal = (0, react.useMemo)(() => {
		var _utils$isTerminal;
		return utils === null || utils === void 0 || (_utils$isTerminal = utils.isTerminal) === null || _utils$isTerminal === void 0 ? void 0 : _utils$isTerminal.call(utils);
	}, [utils]);
	const tableId = (0, react.useMemo)(() => {
		if (props.__designMode === "design") return "designMode-mock-table-id";
		return (props === null || props === void 0 ? void 0 : props.currentComponentId) || props.__id;
	}, [
		props.__id,
		props.__designMode,
		props === null || props === void 0 ? void 0 : props.currentComponentId
	]);
	const columns = (0, react.useMemo)(() => {
		return propsColumns.filter((item) => !item.hidden);
	}, [propsColumns]);
	const originColumnsMap = (0, react.useMemo)(() => new Map(columns.map((item) => [item.key, require_model.formatColumn(item)])), [columns]);
	const sort = require_useTransSortSetting.default({
		sort: propsSort,
		originColumnsMap
	});
	const filter = require_useTransFilterSetting.default({
		filter: propsFilter,
		originColumnsMap
	});
	const dataSourceGroup = require_useTransDataSourceGroupSetting.default({
		dataSourceGroup: propsDataSourceGroup,
		originColumnsMap
	});
	const currentViewMode = (0, react.useMemo)(() => {
		const localCurrentMode = require_utils.getCurrentViewModeFromLocalStorage(tableId);
		if (multiple) return viewMode || localCurrentMode;
		return viewMode || defaultViewMode;
	}, [
		viewMode,
		defaultViewMode,
		multiple
	]);
	const removeTitleColumns = (0, react.useMemo)(() => {
		return columns.map((item) => {
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { title: void 0 });
		});
	}, [columns]);
	const currentSettingHash = (0, react.useMemo)(() => {
		return currentSettingKey || require_utils.getHash(removeTitleColumns) || "";
	}, [currentSettingKey, JSON.stringify(removeTitleColumns)]);
	(0, react.useEffect)(() => {
		if (require_utils.getTableSettingFromLocalStorage(tableId).currentSettingHash !== currentSettingHash) require_utils.clearTableSettingToLocalStorage(tableId);
	}, [currentSettingHash, tableId]);
	(0, react.useEffect)(() => {
		if (debouncedWidth) modal.dispatch({
			type: "setTableWidth",
			payload: debouncedWidth || {}
		});
	}, [debouncedWidth]);
	const { setTableSetting, tableSetting } = require_useTableSetting.default({
		tableId,
		filter,
		columns,
		columnSetting,
		dataSourceGroup,
		sort,
		view,
		currentViewMode,
		gallery,
		currentSettingHash,
		localSettings: _localSettings
	});
	const getIsTriggerValuesChange = require_useTriggerValuesChange.default({
		filter,
		columnSetting,
		dataSourceGroup,
		sort,
		currentViewMode
	});
	(0, react.useEffect)(() => {
		modal.dispatch({
			type: "setUtils",
			payload: require_utils.omit(utils, ["pisellos"]) || {}
		});
		modal.dispatch({
			type: "setIsTerminal",
			payload: isTerminal
		});
	}, [utils, isTerminal]);
	(0, react.useEffect)(() => {
		modal.dispatch({
			type: "setDefaultColumns",
			payload: require_utils.mergeColumnSetting({
				columns,
				tableId,
				currentViewMode
			})
		});
		modal.dispatch({
			type: "setOriginColumns",
			payload: columns
		});
	}, [
		columns,
		tableId,
		currentViewMode
	]);
	(0, react.useEffect)(() => {
		modal.dispatch({
			type: "setDataSource",
			payload: dataSource
		});
		modal.dispatch({
			type: "setOriginDataSource",
			payload: dataSource
		});
	}, [dataSource]);
	(0, react.useEffect)(() => {
		modal.dispatch({
			type: "setTableId",
			payload: tableId
		});
	}, [tableId]);
	(0, react.useEffect)(() => {
		var _other$pagination;
		modal.dispatch({
			type: "setLocalPagination",
			payload: other === null || other === void 0 || (_other$pagination = other.pagination) === null || _other$pagination === void 0 ? void 0 : _other$pagination.localPagination
		});
	}, [other === null || other === void 0 || (_other$pagination2 = other.pagination) === null || _other$pagination2 === void 0 ? void 0 : _other$pagination2.localPagination]);
	(0, react.useEffect)(() => {
		setTimeout(() => {
			document.body.id = "body";
		}, 200);
	}, []);
	const filterDefaultValues = (0, react.useMemo)(() => {
		var _other$pagination3, _other$pagination4;
		return ((filter === null || filter === void 0 ? void 0 : filter.list) || []).reduce((p, c) => {
			var _c$other;
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, p), {}, { [c.name]: c === null || c === void 0 || (_c$other = c.other) === null || _c$other === void 0 ? void 0 : _c$other.defaultValue });
		}, { pagination: {
			page: ((_other$pagination3 = other.pagination) === null || _other$pagination3 === void 0 ? void 0 : _other$pagination3.current) || 1,
			size: ((_other$pagination4 = other.pagination) === null || _other$pagination4 === void 0 ? void 0 : _other$pagination4.pageSize) || 10
		} });
	}, [
		JSON.stringify(filter === null || filter === void 0 ? void 0 : filter.list),
		(_other$pagination5 = other.pagination) === null || _other$pagination5 === void 0 ? void 0 : _other$pagination5.current,
		(_other$pagination6 = other.pagination) === null || _other$pagination6 === void 0 ? void 0 : _other$pagination6.pageSize
	]);
	filterDefaultRef.current = filterDefaultValues;
	tableSettingRef.current = tableSetting;
	initialValuesRef.current = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, filterDefaultValues), tableSetting);
	(0, react.useImperativeHandle)(ref, () => {
		if (wrapRef.current) {
			wrapRef.current.formInstance = form;
			wrapRef.current.getCachedFilters = () => {
				try {
					if (!tableId) return {};
					const setting = require_utils.getTableSettingByCurrentViewMode(tableId, currentViewMode);
					const filters = (setting === null || setting === void 0 ? void 0 : setting.filters) || {};
					filters.order_by = (setting === null || setting === void 0 ? void 0 : setting.order_by) || "";
					return filters;
				} catch (e) {
					return {};
				}
			};
		}
		return wrapRef.current;
	});
	(0, react.useEffect)(() => {
		form.setFieldsValue(initialValuesRef.current);
	}, [currentViewMode]);
	(0, react.useEffect)(() => {
		form.setFieldValue("column_setting", {});
		form.setFieldValue("column_setting", tableSettingRef.current.column_setting);
	}, [require_utils.stringify(removeTitleColumns)]);
	(0, react.useEffect)(() => {
		form.setFieldsValue(initialValuesRef.current);
	}, [require_utils.stringify(propsFilter)]);
	return /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical",
		onValuesChange: (cur, values) => {
			let _values = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, form.getFieldsValue(true)), values.filters || {});
			let _cur = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, cur), cur.filters || {});
			if (Object.keys(cur)[0] !== "pagination") _values.pagination = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, _values.pagination), {}, { page: 1 });
			else {
				var _other$pagination7;
				if (other === null || other === void 0 || (_other$pagination7 = other.pagination) === null || _other$pagination7 === void 0 ? void 0 : _other$pagination7.localPagination) return;
			}
			setTableSetting(_values);
			form.setFieldsValue({ pagination: _values.pagination });
			/** 表格内部分组列配置筛选等跳过onValuesChange */
			if (!getIsTriggerValuesChange(cur, values)) return;
			onValuesChange === null || onValuesChange === void 0 || onValuesChange(require_utils.omit(_cur, ["filters"]), _values);
		},
		className: "materials-grid-form-wrap"
	}, /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("materials-grid", { "materials-grid-terminal": isTerminal }),
		style,
		ref: wrapRef
	}, /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		title,
		showTitle,
		subTitle,
		showSubTitle,
		buttons
	}), (tabs === null || tabs === void 0 ? void 0 : tabs.show) && /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "tabs",
		noStyle: true
	}, /* @__PURE__ */ react.default.createElement(require_index.default, tabs)), /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		view,
		buttons: actionButtons,
		columnSetting,
		dataSourceGroup,
		filter,
		sort,
		tableId,
		localPagination: other === null || other === void 0 || (_other$pagination8 = other.pagination) === null || _other$pagination8 === void 0 ? void 0 : _other$pagination8.localPagination,
		gallery,
		exportImport
	}), /* @__PURE__ */ react.default.createElement(require_index$3.default, { summary }), /* @__PURE__ */ react.default.createElement(require_index$4.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, {
		filter,
		sort,
		setTableSetting,
		dataSourceGroup
	}))));
}));
var table_default = (0, react.forwardRef)(GridView);
//#endregion
exports.default = table_default;
