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 { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useDataSource from "../hooks/useDataSource.js";
import useVariables from "../hooks/useVariables.js";
import table_default from "../../table/index.js";
import ActionsProvider from "../provider/actions/ActionsProvider.js";
import useDrawerState from "./hooks/useDrawerState.js";
import useDesignMode from "./hooks/useDesignMode.js";
import useComponentId from "../hooks/useComponentId.js";
import useTableQuery from "./hooks/useTableQuery.js";
import TableSettingProvider from "./provider/tableSetting/TableSettingProvider.js";
import useDataSourceKey from "./hooks/useDataSourceKey.js";
import useTableProps from "./hooks/useTableProps.js";
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef } from "react";
import { useUpdateEffect } from "ahooks";
//#region src/components/dataSourceComponents/dataSourceTable/BaseTable.tsx
const _excluded = [
	"columns",
	"dataSource",
	"operationContent",
	"currentSettingKey"
];
const mockData = {
	count: 1,
	data: [{}],
	page: 1,
	pageSize: 10
};
const BaseTable = (props, ref) => {
	const { columns: propsColumns, dataSource, operationContent, currentSettingKey } = props, others = _objectWithoutProperties(props, _excluded);
	const tableRef = useRef(null);
	const componentId = useComponentId(props);
	const { registerValueVariable } = useVariables();
	const { list, __designMode } = useDataSource();
	const { data: originData } = list;
	const data = useMemo(() => {
		var _originData$data;
		if (__designMode === "design" && !(originData === null || originData === void 0 || (_originData$data = originData.data) === null || _originData$data === void 0 ? void 0 : _originData$data.length)) return mockData;
		return originData;
	}, [originData, __designMode]);
	const formatCurrentSettingKey = useMemo(() => {
		return currentSettingKey || props.__id || "";
	}, [currentSettingKey]);
	const { dataSourceKey, dataSourceKeyRef } = useDataSourceKey();
	const beforeClose = useRef();
	const setBeforeClose = useCallback((fn) => {
		beforeClose.current = fn;
	}, []);
	const { drawerVisible, setDrawerVisible, openDrawer, closeDrawer, containerProps, Container, drawerContent } = useDrawerState(operationContent);
	const { queryPageConfig, handleValuesChange, refreshData, handleFilterChange, refreshCurrentPage, getInitialValues } = useTableQuery(props, formatCurrentSettingKey);
	const handleAllValuesChange = (changedValues, values) => {
		var _props$onValuesChange;
		props === null || props === void 0 || (_props$onValuesChange = props.onValuesChange) === null || _props$onValuesChange === void 0 || _props$onValuesChange.call(props, changedValues, values);
		handleValuesChange(changedValues, values);
	};
	useDesignMode(props, {
		registerValueVariable,
		openDrawer,
		closeDrawer,
		data: data === null || data === void 0 ? void 0 : data.data
	});
	useUpdateEffect(() => {
		refreshData();
	}, [dataSourceKey]);
	useEffect(() => {
		if (dataSourceKeyRef.current) {
			if ((dataSource === null || dataSource === void 0 ? void 0 : dataSource.fromSettingMeta) || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.fromDeviceSettingMeta)) return;
			list === null || list === void 0 || list.run(queryPageConfig);
		} else list === null || list === void 0 || list.mutate([]);
	}, [queryPageConfig]);
	useEffect(() => {
		registerValueVariable === null || registerValueVariable === void 0 || registerValueVariable(componentId, { currentDataSource: dataSource });
	}, [dataSource]);
	const handleCloseDrawer = function() {
		var _ref = _asyncToGenerator(function* () {
			if (beforeClose.current) {
				if (!(yield beforeClose.current())) return;
			}
			closeDrawer();
			registerValueVariable === null || registerValueVariable === void 0 || registerValueVariable(componentId, { currentRecord: null });
		});
		return function handleCloseDrawer() {
			return _ref.apply(this, arguments);
		};
	}();
	const transformProps = useTableProps({
		data,
		originProps: props,
		openDrawer,
		closeDrawer,
		refreshData,
		handleFilterChange,
		tableRef,
		getInitialValues
	});
	useImperativeHandle(ref, () => {
		if (tableRef.current) tableRef.current.dataSourceTable = {
			refreshCurrentPage,
			refreshData
		};
		return tableRef.current;
	});
	return /* @__PURE__ */ React.createElement(TableSettingProvider, null, /* @__PURE__ */ React.createElement(table_default, _objectSpread2(_objectSpread2(_objectSpread2({ ref: tableRef }, others), transformProps), {}, {
		currentSettingKey: formatCurrentSettingKey,
		dataSource: (data === null || data === void 0 ? void 0 : data.data) || [],
		loading: list === null || list === void 0 ? void 0 : list.loading,
		onValuesChange: handleAllValuesChange
	})), /* @__PURE__ */ React.createElement(ActionsProvider, {
		visible: drawerVisible,
		setVisible: setDrawerVisible,
		refreshTableData: refreshData,
		setBeforeClose,
		beforeClose: beforeClose.current
	}, /* @__PURE__ */ React.createElement(Container, _objectSpread2(_objectSpread2({}, containerProps), {}, {
		open: drawerVisible,
		onClose: handleCloseDrawer,
		onCancel: handleCloseDrawer,
		destroyOnClose: true
	}), drawerContent)));
};
var BaseTable_default = forwardRef(BaseTable);
//#endregion
export { BaseTable_default as default };
