require("../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../locales/index.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useDataSource = require("./useDataSource.js");
const require_index$1 = require("../../pisellToast/index.js");
const require_useFormat = require("../dataSourceTable/hooks/useFormat.js");
const require_serve = require("../dataSourceForm/serve.js");
const require_useVariables = require("./useVariables.js");
const require_useParseActionsVariable = require("./useParseActionsVariable.js");
let react = require("react");
let ahooks = require("ahooks");
//#region src/components/dataSourceComponents/hooks/useActions.ts
const useActions = ({ dataSource: propsDataSource, currentValue: propsCurrentValue, __designMode, extraParams: propsExtraParams } = {}) => {
	var _context$appHelper;
	const { dataSource: ctxDataSource, __designMode: ctxDesignMode } = require_useDataSource.default();
	const { pisell2ApiBaseUrl, mypisellApiBaseUrl } = ((_context$appHelper = require_useEngineContext.default().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.constants) || {};
	const baseUrlMap = {
		"pisell2-api": pisell2ApiBaseUrl,
		"mypisell-api": mypisellApiBaseUrl
	};
	const { parseVariable } = require_useVariables.default();
	const { parseActions } = require_useParseActionsVariable.default();
	const currentDataSource = propsDataSource || ctxDataSource;
	const currentValue = propsCurrentValue || "server";
	const designMode = __designMode || ctxDesignMode;
	const { isCustomApi, actions: originalActions, options, headerDataSource, name, dataSourceKey, fields } = currentDataSource || {};
	const { formatListResult, formatListParams, formatCreateUpdateParams } = require_useFormat.default({
		dataSourceKey: dataSourceKey || "",
		fields: fields || []
	});
	const currentRecord = (0, react.useMemo)(() => {
		return parseVariable === null || parseVariable === void 0 ? void 0 : parseVariable(currentValue, {});
	}, [currentValue, parseVariable]);
	const extraParams = (0, react.useMemo)(() => {
		return Object.entries(propsExtraParams || {}).reduce((acc, [key, value]) => {
			acc[key] = Object.entries(value || {}).reduce((acc, [k, v]) => {
				acc[k] = parseVariable === null || parseVariable === void 0 ? void 0 : parseVariable(v, {});
				return acc;
			}, {});
			return acc;
		}, {});
	}, [propsExtraParams, parseVariable]);
	const getParsedActions = (0, ahooks.useMemoizedFn)(() => {
		return parseActions(originalActions, { currentRecord });
	});
	const genCustomApi = (0, ahooks.useMemoizedFn)(({ method, path, parsedPath, params, headers, body }) => {
		return function() {
			var _ref = require_asyncToGenerator._asyncToGenerator(function* (data) {
				const result = yield require_serve.customApi(parsedPath || path, data, {
					method,
					baseUrl: baseUrlMap[dataSourceKey] || (options === null || options === void 0 ? void 0 : options.baseUrl),
					params,
					headers,
					body,
					dataSourceKey
				});
				if (["pisell2-api", "mypisell-api"].includes(dataSourceKey)) {
					if ("mypisell-api" === dataSourceKey && designMode === "design") return result;
					return result.data;
				}
				return result;
			});
			return function(_x) {
				return _ref.apply(this, arguments);
			};
		}();
	});
	const getHeaders = (0, ahooks.useMemoizedFn)(() => {
		return headerDataSource ? { "x-data-source": headerDataSource } : {};
	});
	const designModeToast = (0, ahooks.useMemoizedFn)(() => {
		return require_index$1.default({ content: require_index.getText("pisell-design-mode-action-disabled") });
	});
	return {
		create: (0, ahooks.useMemoizedFn)(function() {
			var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (values) {
				if (designMode === "design") {
					designModeToast();
					return Promise.reject();
				}
				if (isCustomApi) {
					const actions = getParsedActions();
					return genCustomApi(actions === null || actions === void 0 ? void 0 : actions.create)(formatCreateUpdateParams(values));
				} else {
					if (!name) return {};
					return require_serve.createNocobaseData({
						key: name,
						data: formatCreateUpdateParams(values),
						headers: getHeaders()
					});
				}
			});
			return function(_x2) {
				return _ref2.apply(this, arguments);
			};
		}()),
		get: (0, ahooks.useMemoizedFn)(require_asyncToGenerator._asyncToGenerator(function* () {
			if (currentValue === "server") {
				if (isCustomApi) {
					const actions = getParsedActions();
					if (!(actions === null || actions === void 0 ? void 0 : actions.get)) return {};
					return genCustomApi(actions === null || actions === void 0 ? void 0 : actions.get)(extraParams === null || extraParams === void 0 ? void 0 : extraParams.get);
				}
				const res = yield require_serve.getNocobaseData({
					key: name || "",
					query: extraParams === null || extraParams === void 0 ? void 0 : extraParams.get,
					headers: getHeaders()
				});
				return (res === null || res === void 0 ? void 0 : res.data) || {};
			} else if (currentValue) return parseVariable === null || parseVariable === void 0 ? void 0 : parseVariable(currentValue, {});
			return {};
		})),
		update: (0, ahooks.useMemoizedFn)(function() {
			var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (values) {
				if (designMode === "design") {
					designModeToast();
					return Promise.reject();
				}
				if ((currentDataSource === null || currentDataSource === void 0 ? void 0 : currentDataSource.fromSettingMeta) || (currentDataSource === null || currentDataSource === void 0 ? void 0 : currentDataSource.fromDeviceSettingMeta)) return Promise.reject();
				if (isCustomApi) {
					const actions = getParsedActions();
					if (!actions.update) return {};
					return genCustomApi(actions === null || actions === void 0 ? void 0 : actions.update)(formatCreateUpdateParams(values));
				} else {
					const id = (extraParams === null || extraParams === void 0 ? void 0 : extraParams.filterByTk) || (values === null || values === void 0 ? void 0 : values.id);
					if (!id || !name) {
						require_index$1.default({ content: require_index.getText("pisell-action-get-data") });
						return Promise.reject();
					}
					try {
						return yield require_serve.updateNocobaseData({
							key: name,
							data: formatCreateUpdateParams(values),
							headers: getHeaders(),
							query: require_objectSpread2._objectSpread2({ filterByTk: id }, extraParams === null || extraParams === void 0 ? void 0 : extraParams.update)
						});
					} catch (error) {
						return Promise.reject();
					}
				}
			});
			return function(_x3) {
				return _ref3.apply(this, arguments);
			};
		}()),
		list: (0, ahooks.useMemoizedFn)(function() {
			var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (params) {
				const formatParams = formatListParams(params);
				if (isCustomApi) {
					const actions = getParsedActions();
					if (!(actions === null || actions === void 0 ? void 0 : actions.list)) return [];
					return formatListResult(yield genCustomApi(actions === null || actions === void 0 ? void 0 : actions.list)(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, formatParams), (extraParams === null || extraParams === void 0 ? void 0 : extraParams.list) || {})));
				}
				if (!name) return {};
				return formatListResult(yield require_serve.getNocobaseDataList({
					key: name,
					params: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, formatParams), (extraParams === null || extraParams === void 0 ? void 0 : extraParams.list) || {}),
					headers: getHeaders()
				}));
			});
			return function(_x4) {
				return _ref4.apply(this, arguments);
			};
		}()),
		destroy: (0, ahooks.useMemoizedFn)(function() {
			var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (id) {
				if (designMode === "design") {
					designModeToast();
					return Promise.reject();
				}
				if (isCustomApi) {
					const actions = getParsedActions();
					if (!actions.destroy) return {};
					return genCustomApi(actions === null || actions === void 0 ? void 0 : actions.destroy)({});
				}
				if (!name) return Promise.reject();
				return require_serve.deleteNocobaseData(name, id, getHeaders());
			});
			return function(_x5) {
				return _ref5.apply(this, arguments);
			};
		}())
	};
};
//#endregion
exports.default = useActions;
