import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { useSales } from "../../../businessModel/SalesModel/Sales/index.js";
import { useCart } from "../Cart/index.js";
import { getSpecialResourceListApi } from "./serve.js";
import { getIamgeFromMedia } from "../utils/index.js";
import { useInit } from "../Init/index.js";
import React, { createContext } from "react";
import { useRequest } from "ahooks";
//#region src/components/Sales/Resource/index.tsx
const defaultState = {};
const ResourceContext = createContext(void 0);
const ResourceProvider = ({ children }) => {
	const { data, setData, getCurrentData } = useSales();
	const { resetInitPartData } = useInit();
	const { updateCurrentCartItem } = useCart();
	const setResourceData = (newResourceData) => {
		setData({ resourceData: _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: 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 = useRequest(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.createElement(ResourceContext.Provider, { value: contextValue }, children);
};
/**
* 使用步骤管理的Hook
* @throws {Error} 如果在StepProvider外部使用会抛出错误
* @returns {StepContextType} 步骤管理上下文
*/
const useResource = () => {
	const context = React.useContext(ResourceContext);
	if (!context) throw new Error("useResource must be used within a ResourceProvider");
	return context;
};
var Resource_default = { defaultState };
//#endregion
export { ResourceProvider, Resource_default as default, useResource };
