import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useSharedState from "../../hooks/useSharedState.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { Context, Provider, formatColumn } from "./model.js";
import Tabs$1 from "./Tabs/index.js";
import Actions from "./Actions/index.js";
import Header from "./Header/index.js";
import Summary from "./Summary/index.js";
import { clearTableSettingToLocalStorage, getCurrentViewModeFromLocalStorage, getHash, getTableSettingByCurrentViewMode, getTableSettingFromLocalStorage, mergeColumnSetting, omit, stringify } from "./Table/utils.js";
import View from "./View/index.js";
import useTableSetting from "./hooks/useTableSetting.js";
import useTransDataSourceGroupSetting from "./hooks/useTransDataSourceGroupSetting.js";
import useTransFilterSetting from "./hooks/useTransFilterSetting.js";
import useTransSortSetting from "./hooks/useTransSortSetting.js";
import useTriggerValuesChange from "./hooks/useTriggerValuesChange.js";
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from "react";
import { Form } from "antd";
import classNames from "classnames";
import { useDebounce, useSize } from "ahooks";
import "./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: []
};
var table_default = forwardRef(Provider(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 = _objectWithoutProperties(props, _excluded);
	const modal = useSharedState(Context);
	const [form] = Form.useForm();
	const { defaultViewMode, multiple } = view;
	const viewMode = Form.useWatch("view_mode", {
		form,
		preserve: true
	});
	const context = useEngineContext();
	const wrapRef = useRef(null);
	const tableSettingRef = useRef({});
	const filterDefaultRef = useRef({});
	const initialValuesRef = useRef({});
	const { width } = useSize(wrapRef) || {};
	const debouncedWidth = 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 = 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 = 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 = useMemo(() => {
		return propsColumns.filter((item) => !item.hidden);
	}, [propsColumns]);
	const originColumnsMap = useMemo(() => new Map(columns.map((item) => [item.key, formatColumn(item)])), [columns]);
	const sort = useTransSortSetting({
		sort: propsSort,
		originColumnsMap
	});
	const filter = useTransFilterSetting({
		filter: propsFilter,
		originColumnsMap
	});
	const dataSourceGroup = useTransDataSourceGroupSetting({
		dataSourceGroup: propsDataSourceGroup,
		originColumnsMap
	});
	const currentViewMode = useMemo(() => {
		const localCurrentMode = getCurrentViewModeFromLocalStorage(tableId);
		if (multiple) return viewMode || localCurrentMode;
		return viewMode || defaultViewMode;
	}, [
		viewMode,
		defaultViewMode,
		multiple
	]);
	const removeTitleColumns = useMemo(() => {
		return columns.map((item) => {
			return _objectSpread2(_objectSpread2({}, item), {}, { title: void 0 });
		});
	}, [columns]);
	const currentSettingHash = useMemo(() => {
		return currentSettingKey || getHash(removeTitleColumns) || "";
	}, [currentSettingKey, JSON.stringify(removeTitleColumns)]);
	useEffect(() => {
		if (getTableSettingFromLocalStorage(tableId).currentSettingHash !== currentSettingHash) clearTableSettingToLocalStorage(tableId);
	}, [currentSettingHash, tableId]);
	useEffect(() => {
		if (debouncedWidth) modal.dispatch({
			type: "setTableWidth",
			payload: debouncedWidth || {}
		});
	}, [debouncedWidth]);
	const { setTableSetting, tableSetting } = useTableSetting({
		tableId,
		filter,
		columns,
		columnSetting,
		dataSourceGroup,
		sort,
		view,
		currentViewMode,
		gallery,
		currentSettingHash,
		localSettings: _localSettings
	});
	const getIsTriggerValuesChange = useTriggerValuesChange({
		filter,
		columnSetting,
		dataSourceGroup,
		sort,
		currentViewMode
	});
	useEffect(() => {
		modal.dispatch({
			type: "setUtils",
			payload: omit(utils, ["pisellos"]) || {}
		});
		modal.dispatch({
			type: "setIsTerminal",
			payload: isTerminal
		});
	}, [utils, isTerminal]);
	useEffect(() => {
		modal.dispatch({
			type: "setDefaultColumns",
			payload: mergeColumnSetting({
				columns,
				tableId,
				currentViewMode
			})
		});
		modal.dispatch({
			type: "setOriginColumns",
			payload: columns
		});
	}, [
		columns,
		tableId,
		currentViewMode
	]);
	useEffect(() => {
		modal.dispatch({
			type: "setDataSource",
			payload: dataSource
		});
		modal.dispatch({
			type: "setOriginDataSource",
			payload: dataSource
		});
	}, [dataSource]);
	useEffect(() => {
		modal.dispatch({
			type: "setTableId",
			payload: tableId
		});
	}, [tableId]);
	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]);
	useEffect(() => {
		setTimeout(() => {
			document.body.id = "body";
		}, 200);
	}, []);
	const filterDefaultValues = useMemo(() => {
		var _other$pagination3, _other$pagination4;
		return ((filter === null || filter === void 0 ? void 0 : filter.list) || []).reduce((p, c) => {
			var _c$other;
			return _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 = _objectSpread2(_objectSpread2({}, filterDefaultValues), tableSetting);
	useImperativeHandle(ref, () => {
		if (wrapRef.current) {
			wrapRef.current.formInstance = form;
			wrapRef.current.getCachedFilters = () => {
				try {
					if (!tableId) return {};
					const setting = 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;
	});
	useEffect(() => {
		form.setFieldsValue(initialValuesRef.current);
	}, [currentViewMode]);
	useEffect(() => {
		form.setFieldValue("column_setting", {});
		form.setFieldValue("column_setting", tableSettingRef.current.column_setting);
	}, [stringify(removeTitleColumns)]);
	useEffect(() => {
		form.setFieldsValue(initialValuesRef.current);
	}, [stringify(propsFilter)]);
	return /* @__PURE__ */ React.createElement(Form, {
		form,
		layout: "vertical",
		onValuesChange: (cur, values) => {
			let _values = _objectSpread2(_objectSpread2({}, form.getFieldsValue(true)), values.filters || {});
			let _cur = _objectSpread2(_objectSpread2({}, cur), cur.filters || {});
			if (Object.keys(cur)[0] !== "pagination") _values.pagination = _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(omit(_cur, ["filters"]), _values);
		},
		className: "materials-grid-form-wrap"
	}, /* @__PURE__ */ React.createElement("div", {
		className: classNames("materials-grid", { "materials-grid-terminal": isTerminal }),
		style,
		ref: wrapRef
	}, /* @__PURE__ */ React.createElement(Header, {
		title,
		showTitle,
		subTitle,
		showSubTitle,
		buttons
	}), (tabs === null || tabs === void 0 ? void 0 : tabs.show) && /* @__PURE__ */ React.createElement(Form.Item, {
		name: "tabs",
		noStyle: true
	}, /* @__PURE__ */ React.createElement(Tabs$1, tabs)), /* @__PURE__ */ React.createElement(Actions, {
		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.createElement(Summary, { summary }), /* @__PURE__ */ React.createElement(View, _objectSpread2(_objectSpread2({}, props), {}, {
		filter,
		sort,
		setTableSetting,
		dataSourceGroup
	}))));
})));
//#endregion
export { table_default as default };
