import { getText } from "../../../locales/index.js";
import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useSharedState from "../../../hooks/useSharedState.js";
import { Context } from "../model.js";
import { paginationConfig } from "../../Pagination/index.js";
import BasicTable from "../BasicTable/index.js";
import SortRow from "./tableConfig/SortRow/index.js";
import useTransDataSource from "../hooks/useTransDataSource.js";
import useTransColumns from "../hooks/useTransColumns.js";
import useGenTableComponents from "../hooks/useGenTableComponents.js";
import useGenScroll from "../hooks/useGenScroll.js";
import React, { memo, useCallback, useEffect, useMemo, useRef } from "react";
import { Form } from "antd";
import { useDebounce, useMemoizedFn, useSize } from "ahooks";
import "./index.less";
//#region src/components/table/Table/index.tsx
const GridViewTable = ({ tableProps, filter, setTableSetting, onDataSourceChange }) => {
	var _tableProps$paginatio, _tableProps$paginatio2;
	const { state, dispatch } = useSharedState(Context);
	const form = Form.useFormInstance();
	const column_setting = Form.useWatch("column_setting", {
		form,
		preserve: true
	}) || [];
	const tableWrapRef = useRef(null);
	const { dataSource: defaultDataSource, originDataSource, isTerminal } = state;
	const handleSave = useMemoizedFn((row) => {
		const newData = [...defaultDataSource];
		const index = newData.findIndex((item) => row.key === item.key);
		const item = newData[index];
		newData.splice(index, 1, _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] = _objectSpread2(_objectSpread2({}, nextColumns[findIndex]), {}, { width: size.width });
		form.setFieldValue("column_setting", nextColumns);
		setTableSetting(form.getFieldsValue());
	};
	const { columns } = useTransColumns({
		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 = useTransDataSource({
		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"
	});
	useEffect(() => {
		dispatch({
			type: "setOriginResultDataSource",
			payload: dataSource || []
		});
	}, [dataSource]);
	useEffect(() => {
		onDataSourceChange === null || onDataSourceChange === void 0 || onDataSourceChange(dataSource);
	}, [JSON.stringify(dataSource)]);
	const components = useGenTableComponents({ form });
	console.log(columns, "columns12");
	const showTotal = useCallback((total, range, pageSize) => {
		let _current = (range[0] - 1) / pageSize + 1;
		return getText("table-pagination-total")(_current, Math.ceil(total / pageSize));
	}, []);
	const _paginationConfig = useMemo(() => {
		if (tableProps.pagination === false) return false;
		return _objectSpread2(_objectSpread2(_objectSpread2({}, paginationConfig), tableProps.pagination), {}, { showTotal });
	}, [tableProps.pagination]);
	return /* @__PURE__ */ React.createElement("div", {
		className: "materials-grid-table-wrap",
		ref: tableWrapRef
	}, /* @__PURE__ */ React.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 } = useSize(tableWrapRef) || {};
	useDebounce(height, { wait: 500 });
	const isShowPagination = 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 = useGenScroll({
		wrapHeight: height || 500,
		scroll: tableProps.scroll,
		columns,
		isShowPagination
	});
	const debouncedScroll = useDebounce(scroll, { wait: 200 });
	const platformScroll = useMemo(() => {
		if (isTerminal) return debouncedScroll;
		return scroll;
	}, [
		debouncedScroll,
		scroll,
		isTerminal
	]);
	const basicTable = useMemo(() => {
		if (!height) return null;
		return /* @__PURE__ */ React.createElement(BasicTable, _objectSpread2(_objectSpread2({
			dataSource,
			columns,
			className: "grid-view-table",
			components
		}, tableProps), {}, {
			scroll: platformScroll,
			pagination: paginationConfig
		}));
	}, [
		height,
		platformScroll,
		dataSource,
		columns,
		components,
		paginationConfig,
		tableProps
	]);
	return /* @__PURE__ */ React.createElement(SortRow, {
		rowKey: tableProps.rowKey,
		onSortChange: tableProps.onSortChange
	}, basicTable);
};
var Table_default = memo(GridViewTable);
//#endregion
export { Table_default as default };
