import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useVariables from "../../dataSourceComponents/hooks/useVariables.js";
import useComponentId from "../../dataSourceComponents/hooks/useComponentId.js";
import useDataSourceKey from "../hooks/useDataSourceKey.js";
import useHandleAction from "../hooks/useHandleAction.js";
import { useChildComponentPropsContext } from "./ChildComponentProps.js";
import React, { createContext, useContext, useEffect } from "react";
import { isBoolean } from "@pisell/utils";
//#region src/components/pisellDataSourceContainer/provider/HandleActions.tsx
const HandleActionContext = createContext(null);
const HandleActionsProvider = (props) => {
	const { dataSource, children, autoRun, pagination, extraParams } = props;
	const { form } = useChildComponentPropsContext();
	const componentId = useComponentId(props);
	const { registerValueVariable } = useVariables();
	const { dataSourceKey } = useDataSourceKey();
	const handleAction = useHandleAction({ list: extraParams === null || extraParams === void 0 ? void 0 : extraParams.list }, pagination, form);
	useEffect(() => {
		registerValueVariable === null || registerValueVariable === void 0 || registerValueVariable(componentId, { currentDataSource: dataSource });
	}, [dataSource]);
	useEffect(() => {
		if (isBoolean(autoRun === null || autoRun === void 0 ? void 0 : autoRun.list) && (autoRun === null || autoRun === void 0 ? void 0 : autoRun.list)) {
			const formValues = form.getFieldsValue() || {};
			if (dataSourceKey) handleAction.list.onSearch(_objectSpread2({}, formValues));
			else {
				var _handleAction$list;
				(_handleAction$list = handleAction.list) === null || _handleAction$list === void 0 || _handleAction$list.mutate([]);
			}
		}
	}, [dataSourceKey, autoRun === null || autoRun === void 0 ? void 0 : autoRun.list]);
	return /* @__PURE__ */ React.createElement(HandleActionContext.Provider, { value: handleAction }, children);
};
const useHandleActionProvider = () => {
	return useContext(HandleActionContext);
};
//#endregion
export { HandleActionsProvider, useHandleActionProvider };
