const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index = require("../../../businessModel/SalesModel/Sales/index.js");
const require_index$1 = require("../Cart/index.js");
const require_serve = require("./serve.js");
const require_index$2 = require("../utils/index.js");
const require_index$3 = require("../Init/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("ahooks");
//#region src/components/Sales/Resource/index.tsx
const defaultState = {};
const ResourceContext = (0, react.createContext)(void 0);
const ResourceProvider = ({ children }) => {
	const { data, setData, getCurrentData } = require_index.useSales();
	const { resetInitPartData } = require_index$3.useInit();
	const { updateCurrentCartItem } = require_index$1.useCart();
	const setResourceData = (newResourceData) => {
		setData({ resourceData: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, getCurrentData().resourceData || {}), newResourceData) });
	};
	const formatResourceList = (res) => {
		var _res$data;
		const resourceMap = /* @__PURE__ */ new Map();
		if ((res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 || (_res$data = _res$data.list) === null || _res$data === void 0 ? void 0 : _res$data.length) > 0) res.data.list.forEach((item) => {
			const scCampus = (item === null || item === void 0 ? void 0 : item.sc_campus) || "";
			const source = resourceMap.get(scCampus);
			const resourceItem = {
				id: item.form_record_id,
				title: item.room_name || "",
				subtitle: item.subtitle || "",
				image: require_index$2.getIamgeFromMedia(item.media, "findOne"),
				contact: item.sc_phone,
				location: item.sc_address,
				_originData: item
			};
			if (source) source.children.push(resourceItem);
			else resourceMap.set(scCampus, {
				title: scCampus,
				children: [resourceItem]
			});
		});
		const renderData = [];
		resourceMap.forEach((v) => renderData.push(v));
		return renderData;
	};
	const getResourceList = (0, ahooks.useRequest)(require_serve.getSpecialResourceListApi, {
		manual: true,
		onSuccess: (res) => {
			setResourceData({ resourceList: formatResourceList(res) });
		}
	});
	/**
	* @title: 选择资源
	* @description: 如果当前选择的资源与数据中的资源不同，需要清除之前的数据
	* @param {any} resourceItem
	* @return {*}
	* @Author: xiangfeng.xue
	*/
	const selectResource = (resourceItem) => {
		var _getCurrentData$resou;
		if (((_getCurrentData$resou = getCurrentData().resourceData) === null || _getCurrentData$resou === void 0 || (_getCurrentData$resou = _getCurrentData$resou.currentResourceItem) === null || _getCurrentData$resou === void 0 ? void 0 : _getCurrentData$resou.id) !== (resourceItem === null || resourceItem === void 0 ? void 0 : resourceItem.id)) resetInitPartData();
		setResourceData({ currentResourceItem: resourceItem });
		updateCurrentCartItem({ resourceItem });
	};
	const contextValue = {
		getResourceList,
		selectResource
	};
	return /* @__PURE__ */ react.default.createElement(ResourceContext.Provider, { value: contextValue }, children);
};
/**
* 使用步骤管理的Hook
* @throws {Error} 如果在StepProvider外部使用会抛出错误
* @returns {StepContextType} 步骤管理上下文
*/
const useResource = () => {
	const context = react.default.useContext(ResourceContext);
	if (!context) throw new Error("useResource must be used within a ResourceProvider");
	return context;
};
var Resource_default = { defaultState };
//#endregion
exports.ResourceProvider = ResourceProvider;
exports.default = Resource_default;
exports.useResource = useResource;
