const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_DataSourceContext = require("./DataSourceContext.js");
const require_useDataSource = require("../../hooks/useDataSource.js");
const require_useVariables = require("../../hooks/useVariables.js");
const require_useActions = require("../../hooks/useActions.js");
const require_useSetRequest = require("../../../../hooks/useSetRequest.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let ahooks = require("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 } = require_useDataSource.default();
	require_useSetRequest.default();
	const { parseVariable } = require_useVariables.default();
	const getCurrentRecord = (0, ahooks.useMemoizedFn)(() => {
		return (parseVariable === null || parseVariable === void 0 ? void 0 : parseVariable(currentValue, {})) || ctxGetCurrentRecord();
	});
	const dataSource = (0, _pisell_utils.isString)(propsDataSource) ? parseVariable === null || parseVariable === void 0 ? void 0 : parseVariable(propsDataSource) : propsDataSource;
	const standardActions = require_useActions.default({
		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 = (0, ahooks.useRequest)(list, require_objectSpread2._objectSpread2({ manual: (0, _pisell_utils.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 = (0, ahooks.useRequest)(get, { manual: (0, _pisell_utils.isBoolean)(autoRun === null || autoRun === void 0 ? void 0 : autoRun.get) ? !(autoRun === null || autoRun === void 0 ? void 0 : autoRun.get) : true });
	const destroyResult = (0, ahooks.useRequest)(destroy, { manual: true });
	const updateResult = (0, ahooks.useRequest)(update, { manual: true });
	const createResult = (0, ahooks.useRequest)(create, { manual: true });
	const value = (0, react.useMemo)(() => {
		return {
			__designMode,
			dataSource,
			getCurrentRecord,
			list: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, listResult), {}, { data: (overrideData === null || overrideData === void 0 ? void 0 : overrideData.list) || listResult.data }),
			get: require_objectSpread2._objectSpread2(require_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.default.createElement(require_DataSourceContext.default.Provider, { value }, children);
};
//#endregion
exports.default = DataSourceProvider;
