import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import DataSourceContext from "./DataSourceContext.js";
import useDataSource from "../../hooks/useDataSource.js";
import useVariables from "../../hooks/useVariables.js";
import useActions from "../../hooks/useActions.js";
import useSetRequest from "../../../../hooks/useSetRequest.js";
import React, { useMemo } from "react";
import { isBoolean, isString } from "@pisell/utils";
import { useMemoizedFn, useRequest } from "ahooks";
//#region src/components/dataSourceComponents/provider/dataSource/DataSourceProvider.tsx
/**
* @title: DataSource Provider
* @description: 提供页面配置的数据源，支持外部传入Promise接口、自动调用配置和直接数据
*/
const DataSourceProvider = (props) => {
	const { children, dataSource: propsDataSource, currentValue, extraParams, actions: externalActions, overrideData, autoRun, requestOptions, __designMode } = props;
	const { getCurrentRecord: ctxGetCurrentRecord } = useDataSource();
	useSetRequest();
	const { parseVariable } = useVariables();
	const getCurrentRecord = useMemoizedFn(() => {
		return (parseVariable === null || parseVariable === void 0 ? void 0 : parseVariable(currentValue, {})) || ctxGetCurrentRecord();
	});
	const dataSource = isString(propsDataSource) ? parseVariable === null || parseVariable === void 0 ? void 0 : parseVariable(propsDataSource) : propsDataSource;
	const standardActions = useActions({
		dataSource,
		currentValue,
		__designMode,
		extraParams
	});
	const list = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.list) || standardActions.list;
	const get = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.get) || standardActions.get;
	const update = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.update) || standardActions.update;
	const create = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.create) || standardActions.create;
	const destroy = (externalActions === null || externalActions === void 0 ? void 0 : externalActions.destroy) || standardActions.destroy;
	const listResult = useRequest(list, _objectSpread2({ manual: isBoolean(autoRun === null || autoRun === void 0 ? void 0 : autoRun.list) ? !(autoRun === null || autoRun === void 0 ? void 0 : autoRun.list) : true }, requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.list));
	const getResult = useRequest(get, { manual: isBoolean(autoRun === null || autoRun === void 0 ? void 0 : autoRun.get) ? !(autoRun === null || autoRun === void 0 ? void 0 : autoRun.get) : true });
	const destroyResult = useRequest(destroy, { manual: true });
	const updateResult = useRequest(update, { manual: true });
	const createResult = useRequest(create, { manual: true });
	const value = useMemo(() => {
		return {
			__designMode,
			dataSource,
			getCurrentRecord,
			list: _objectSpread2(_objectSpread2({}, listResult), {}, { data: (overrideData === null || overrideData === void 0 ? void 0 : overrideData.list) || listResult.data }),
			get: _objectSpread2(_objectSpread2({}, getResult), {}, { data: (overrideData === null || overrideData === void 0 ? void 0 : overrideData.get) || getResult.data }),
			destroy: destroyResult,
			update: updateResult,
			create: createResult
		};
	}, [
		dataSource,
		listResult,
		destroyResult,
		getResult,
		updateResult,
		createResult,
		overrideData,
		__designMode
	]);
	return /* @__PURE__ */ React.createElement(DataSourceContext.Provider, { value }, children);
};
//#endregion
export { DataSourceProvider as default };
