const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../locales/index.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useSharedState = require("../../../hooks/useSharedState.js");
const require_model = require("../model.js");
const require_index$1 = require("../../Pagination/index.js");
const require_index$2 = require("../BasicTable/index.js");
const require_index$3 = require("./tableConfig/SortRow/index.js");
const require_useTransDataSource = require("../hooks/useTransDataSource.js");
const require_useTransColumns = require("../hooks/useTransColumns.js");
const require_useGenTableComponents = require("../hooks/useGenTableComponents.js");
const require_useGenScroll = require("../hooks/useGenScroll.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/table/Table/index.tsx
const GridViewTable = ({ tableProps, filter, setTableSetting, onDataSourceChange }) => {
	var _tableProps$paginatio, _tableProps$paginatio2;
	const { state, dispatch } = require_useSharedState.default(require_model.Context);
	const form = antd.Form.useFormInstance();
	const column_setting = antd.Form.useWatch("column_setting", {
		form,
		preserve: true
	}) || [];
	const tableWrapRef = (0, react.useRef)(null);
	const { dataSource: defaultDataSource, originDataSource, isTerminal } = state;
	const handleSave = (0, ahooks.useMemoizedFn)((row) => {
		const newData = [...defaultDataSource];
		const index = newData.findIndex((item) => row.key === item.key);
		const item = newData[index];
		newData.splice(index, 1, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), row));
		dispatch({
			type: "setDataSource",
			payload: newData
		});
	});
	const handleResize = (index, record) => (e, { size }) => {
		const findIndex = column_setting.findIndex((item) => item.dataIndex === record.dataIndex);
		const nextColumns = [...column_setting];
		nextColumns[findIndex] = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, nextColumns[findIndex]), {}, { width: size.width });
		form.setFieldValue("column_setting", nextColumns);
		setTableSetting(form.getFieldsValue());
	};
	const { columns } = require_useTransColumns.default({
		columns: column_setting,
		form,
		localPagination: !!(tableProps === null || tableProps === void 0 || (_tableProps$paginatio = tableProps.pagination) === null || _tableProps$paginatio === void 0 ? void 0 : _tableProps$paginatio.localPagination),
		handleResize,
		handleSave,
		dragSort: tableProps === null || tableProps === void 0 ? void 0 : tableProps.dragSort,
		bordered: tableProps.bordered
	});
	const dataSource = require_useTransDataSource.default({
		dataSource: originDataSource,
		form,
		localPagination: !!(tableProps === null || tableProps === void 0 || (_tableProps$paginatio2 = tableProps.pagination) === null || _tableProps$paginatio2 === void 0 ? void 0 : _tableProps$paginatio2.localPagination),
		rowKey: tableProps.rowKey,
		filter,
		currentView: "table"
	});
	(0, react.useEffect)(() => {
		dispatch({
			type: "setOriginResultDataSource",
			payload: dataSource || []
		});
	}, [dataSource]);
	(0, react.useEffect)(() => {
		onDataSourceChange === null || onDataSourceChange === void 0 || onDataSourceChange(dataSource);
	}, [JSON.stringify(dataSource)]);
	const components = require_useGenTableComponents.default({ form });
	console.log(columns, "columns12");
	const showTotal = (0, react.useCallback)((total, range, pageSize) => {
		let _current = (range[0] - 1) / pageSize + 1;
		return require_index.getText("table-pagination-total")(_current, Math.ceil(total / pageSize));
	}, []);
	const _paginationConfig = (0, react.useMemo)(() => {
		if (tableProps.pagination === false) return false;
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, require_index$1.paginationConfig), tableProps.pagination), {}, { showTotal });
	}, [tableProps.pagination]);
	return /* @__PURE__ */ react.default.createElement("div", {
		className: "materials-grid-table-wrap",
		ref: tableWrapRef
	}, /* @__PURE__ */ react.default.createElement(AutoSizerContent, {
		isTerminal,
		tableWrapRef,
		dataSource,
		tableProps,
		columns,
		paginationConfig: _paginationConfig,
		components
	}));
};
const AutoSizerContent = (props) => {
	var _tableProps$paginatio8, _tableProps$paginatio9, _tableProps$paginatio10;
	const { tableProps, dataSource, paginationConfig, columns, components, tableWrapRef, isTerminal } = props;
	const { height } = (0, ahooks.useSize)(tableWrapRef) || {};
	(0, ahooks.useDebounce)(height, { wait: 500 });
	const isShowPagination = (0, react.useMemo)(() => {
		var _tableProps$paginatio3, _tableProps$paginatio4, _tableProps$paginatio5, _tableProps$paginatio6, _tableProps$paginatio7;
		let total = (tableProps === null || tableProps === void 0 || (_tableProps$paginatio3 = tableProps.pagination) === null || _tableProps$paginatio3 === void 0 ? void 0 : _tableProps$paginatio3.total) || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.length) || 0;
		if (tableProps === null || tableProps === void 0 || (_tableProps$paginatio4 = tableProps.pagination) === null || _tableProps$paginatio4 === void 0 ? void 0 : _tableProps$paginatio4.localPagination) total = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.length) || 0;
		return total > ((tableProps === null || tableProps === void 0 || (_tableProps$paginatio5 = tableProps.pagination) === null || _tableProps$paginatio5 === void 0 ? void 0 : _tableProps$paginatio5.pageSize) || (tableProps === null || tableProps === void 0 || (_tableProps$paginatio6 = tableProps.pagination) === null || _tableProps$paginatio6 === void 0 || (_tableProps$paginatio6 = _tableProps$paginatio6.pageSizeOptions) === null || _tableProps$paginatio6 === void 0 ? void 0 : _tableProps$paginatio6[0]) || 10) || (tableProps === null || tableProps === void 0 || (_tableProps$paginatio7 = tableProps.pagination) === null || _tableProps$paginatio7 === void 0 ? void 0 : _tableProps$paginatio7.showPagination) && total > 10;
	}, [
		tableProps === null || tableProps === void 0 || (_tableProps$paginatio8 = tableProps.pagination) === null || _tableProps$paginatio8 === void 0 ? void 0 : _tableProps$paginatio8.localPagination,
		tableProps === null || tableProps === void 0 || (_tableProps$paginatio9 = tableProps.pagination) === null || _tableProps$paginatio9 === void 0 ? void 0 : _tableProps$paginatio9.total,
		dataSource.length,
		tableProps === null || tableProps === void 0 || (_tableProps$paginatio10 = tableProps.pagination) === null || _tableProps$paginatio10 === void 0 || (_tableProps$paginatio10 = _tableProps$paginatio10.pageSizeOptions) === null || _tableProps$paginatio10 === void 0 ? void 0 : _tableProps$paginatio10[0]
	]);
	const scroll = require_useGenScroll.default({
		wrapHeight: height || 500,
		scroll: tableProps.scroll,
		columns,
		isShowPagination
	});
	const debouncedScroll = (0, ahooks.useDebounce)(scroll, { wait: 200 });
	const platformScroll = (0, react.useMemo)(() => {
		if (isTerminal) return debouncedScroll;
		return scroll;
	}, [
		debouncedScroll,
		scroll,
		isTerminal
	]);
	const basicTable = (0, react.useMemo)(() => {
		if (!height) return null;
		return /* @__PURE__ */ react.default.createElement(require_index$2.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
			dataSource,
			columns,
			className: "grid-view-table",
			components
		}, tableProps), {}, {
			scroll: platformScroll,
			pagination: paginationConfig
		}));
	}, [
		height,
		platformScroll,
		dataSource,
		columns,
		components,
		paginationConfig,
		tableProps
	]);
	return /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		rowKey: tableProps.rowKey,
		onSortChange: tableProps.onSortChange
	}, basicTable);
};
var Table_default = (0, react.memo)(GridViewTable);
//#endregion
exports.default = Table_default;
