const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useDataSource = require("../hooks/useDataSource.js");
const require_useVariables = require("../hooks/useVariables.js");
const require_index = require("../../table/index.js");
const require_ActionsProvider = require("../provider/actions/ActionsProvider.js");
const require_useDrawerState = require("./hooks/useDrawerState.js");
const require_useDesignMode = require("./hooks/useDesignMode.js");
const require_useComponentId = require("../hooks/useComponentId.js");
const require_useTableQuery = require("./hooks/useTableQuery.js");
const require_TableSettingProvider = require("./provider/tableSetting/TableSettingProvider.js");
const require_useDataSourceKey = require("./hooks/useDataSourceKey.js");
const require_useTableProps = require("./hooks/useTableProps.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("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 = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	const tableRef = (0, react.useRef)(null);
	const componentId = require_useComponentId.default(props);
	const { registerValueVariable } = require_useVariables.default();
	const { list, __designMode } = require_useDataSource.default();
	const { data: originData } = list;
	const data = (0, react.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 = (0, react.useMemo)(() => {
		return currentSettingKey || props.__id || "";
	}, [currentSettingKey]);
	const { dataSourceKey, dataSourceKeyRef } = require_useDataSourceKey.default();
	const beforeClose = (0, react.useRef)();
	const setBeforeClose = (0, react.useCallback)((fn) => {
		beforeClose.current = fn;
	}, []);
	const { drawerVisible, setDrawerVisible, openDrawer, closeDrawer, containerProps, Container, drawerContent } = require_useDrawerState.default(operationContent);
	const { queryPageConfig, handleValuesChange, refreshData, handleFilterChange, refreshCurrentPage, getInitialValues } = require_useTableQuery.default(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);
	};
	require_useDesignMode.default(props, {
		registerValueVariable,
		openDrawer,
		closeDrawer,
		data: data === null || data === void 0 ? void 0 : data.data
	});
	(0, ahooks.useUpdateEffect)(() => {
		refreshData();
	}, [dataSourceKey]);
	(0, react.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]);
	(0, react.useEffect)(() => {
		registerValueVariable === null || registerValueVariable === void 0 || registerValueVariable(componentId, { currentDataSource: dataSource });
	}, [dataSource]);
	const handleCloseDrawer = function() {
		var _ref = require_asyncToGenerator._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 = require_useTableProps.default({
		data,
		originProps: props,
		openDrawer,
		closeDrawer,
		refreshData,
		handleFilterChange,
		tableRef,
		getInitialValues
	});
	(0, react.useImperativeHandle)(ref, () => {
		if (tableRef.current) tableRef.current.dataSourceTable = {
			refreshCurrentPage,
			refreshData
		};
		return tableRef.current;
	});
	return /* @__PURE__ */ react.default.createElement(require_TableSettingProvider.default, null, /* @__PURE__ */ react.default.createElement(require_index.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_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.default.createElement(require_ActionsProvider.default, {
		visible: drawerVisible,
		setVisible: setDrawerVisible,
		refreshTableData: refreshData,
		setBeforeClose,
		beforeClose: beforeClose.current
	}, /* @__PURE__ */ react.default.createElement(Container, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, containerProps), {}, {
		open: drawerVisible,
		onClose: handleCloseDrawer,
		onCancel: handleCloseDrawer,
		destroyOnClose: true
	}), drawerContent)));
};
var BaseTable_default = (0, react.forwardRef)(BaseTable);
//#endregion
exports.default = BaseTable_default;
