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 { Context } from "../model.js";
import { FormPagination } from "../../Pagination/index.js";
import { calcFilterSort } from "../Table/utils.js";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { isArr } from "@pisell/utils";
import { Form, Table } from "antd";
//#region src/components/table/BasicTable/index.tsx
const _excluded = [
	"dataSource",
	"pagination",
	"value",
	"virtual"
];
const BasicTable = (props) => {
	var _pagination$pageSizeO;
	const { dataSource, pagination, value, virtual } = props, other = _objectWithoutProperties(props, _excluded);
	const [filter, setFilter] = useState();
	const [sort, setSort] = useState();
	const form = Form.useFormInstance();
	const formPagination = Form.useWatch("pagination", {
		form,
		preserve: true
	}) || {
		page: 1,
		size: 10
	};
	const group = Form.useWatch("group_by", {
		form,
		preserve: true
	});
	const { dispatch } = useSharedState(Context);
	const isGroup = useMemo(() => isArr(group) && group.length > 0, [group]);
	const { showTotal, localPagination, showSizeChange } = pagination;
	const _showTotal = useCallback((total, range) => {
		return showTotal(total, range, formPagination.size || 10);
	}, [
		showTotal,
		formPagination === null || formPagination === void 0 ? void 0 : formPagination.page,
		formPagination === null || formPagination === void 0 ? void 0 : formPagination.size
	]);
	useEffect(() => {
		let _size = formPagination.size || 100;
		showSizeChange && showSizeChange(formPagination === null || formPagination === void 0 ? void 0 : formPagination.page, _size);
	}, [formPagination === null || formPagination === void 0 ? void 0 : formPagination.page, formPagination === null || formPagination === void 0 ? void 0 : formPagination.size]);
	const _dataSource = useMemo(() => {
		let _page = formPagination.page;
		let _size = formPagination.size || 10;
		let list = [];
		let _total = (pagination === null || pagination === void 0 ? void 0 : pagination.total) || 0;
		if (localPagination) {
			list = calcFilterSort({
				dataSource,
				columns: other.columns,
				sort,
				filter
			});
			_total = list.length;
			list = list.slice((_page - 1) * _size, _page * _size);
		} else list = dataSource;
		return {
			list,
			total: _total
		};
	}, [
		localPagination,
		dataSource,
		formPagination === null || formPagination === void 0 ? void 0 : formPagination.page,
		formPagination === null || formPagination === void 0 ? void 0 : formPagination.size,
		filter,
		sort,
		other.columns,
		pagination === null || pagination === void 0 ? void 0 : pagination.total
	]);
	const total = useMemo(() => {
		if (localPagination) return (_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.total) || 0;
		return (pagination === null || pagination === void 0 ? void 0 : pagination.total) || (_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.total) || 0;
	}, [
		_dataSource,
		pagination,
		localPagination
	]);
	const defaultExpandedRowKeys = useMemo(() => {
		var _dataSource$list;
		let _expandedRowKeys = [];
		_dataSource === null || _dataSource === void 0 || (_dataSource$list = _dataSource.list) === null || _dataSource$list === void 0 || _dataSource$list.forEach((item) => {
			if (item.__is_group) _expandedRowKeys.push(item["__rowKey"]);
		});
		return _expandedRowKeys;
	}, [_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.list]);
	useEffect(() => {
		dispatch({
			type: "setCurrentDataSource",
			payload: (_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.list) || []
		});
	}, [JSON.stringify(_dataSource === null || _dataSource === void 0 ? void 0 : _dataSource.list)]);
	return /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Table, _objectSpread2(_objectSpread2({ key: defaultExpandedRowKeys.join("-") }, other), {}, {
		rowSelection: isGroup ? false : other === null || other === void 0 ? void 0 : other.rowSelection,
		virtual,
		onChange: (pagination, filters, sorter) => {
			form.setFieldsValue({ pagination: _objectSpread2(_objectSpread2({}, formPagination), {}, { page: 1 }) });
			setFilter(filters);
			setSort(sorter);
		},
		pagination: false,
		dataSource: _dataSource.list,
		onRow: (record, index) => {
			var _other$onRow;
			return _objectSpread2(_objectSpread2({}, (other === null || other === void 0 || (_other$onRow = other.onRow) === null || _other$onRow === void 0 ? void 0 : _other$onRow.call(other, record, index)) || {}), {}, {
				__is_group: record.__is_group,
				__is_drag_sort: other.dragSort,
				className: record.__is_group ? "pisell-lowcode-table-group-row-wrap" : ""
			});
		},
		expandable: { defaultExpandAllRows: true }
	})), total > ((pagination === null || pagination === void 0 ? void 0 : pagination.pageSize) || (pagination === null || pagination === void 0 || (_pagination$pageSizeO = pagination.pageSizeOptions) === null || _pagination$pageSizeO === void 0 ? void 0 : _pagination$pageSizeO[0]) || 10) || (pagination === null || pagination === void 0 ? void 0 : pagination.showPagination) && total > 10 ? /* @__PURE__ */ React.createElement(Form.Item, {
		name: "pagination",
		style: { margin: 0 }
	}, /* @__PURE__ */ React.createElement(FormPagination, _objectSpread2(_objectSpread2({}, pagination), {}, {
		showTotal: _showTotal,
		total
	}))) : null);
};
//#endregion
export { BasicTable as default };
