const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/businessModel/SalesModel/Sales/index.tsx
const defaultData = {};
const SALES_MODEL_SESSION_STORAGE_KEY = "shop_h5_sales_model_data";
const SalesContext = (0, react.createContext)({});
const SalesProvider = (props) => {
	const { children, config } = props;
	const dataRef = (0, react.useRef)({});
	const [data, setData] = (0, react.useState)(defaultData);
	const [hooks, setHooks] = (0, react.useState)(props.hooks);
	const utils = require_useEngineContext.default().appHelper.utils || {};
	const _setData = (newData) => {
		dataRef.current = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, dataRef.current), newData);
		console.log("sales_model_newData", dataRef.current);
		if (config === null || config === void 0 ? void 0 : config.useSessionStorage) setSessionStorage();
		setData(require_objectSpread2._objectSpread2({}, dataRef.current));
	};
	const getCurrentData = () => {
		return dataRef.current;
	};
	/**
	* @title 设置sessionStorage
	* @description 获取路由参数中的cacheId，并设置到sessionStorage中
	*/
	const setSessionStorage = () => {
		var _utils$getHistory, _history$location;
		const history = (utils === null || utils === void 0 || (_utils$getHistory = utils.getHistory) === null || _utils$getHistory === void 0 ? void 0 : _utils$getHistory.call(utils)) || {};
		const cacheId = (history === null || history === void 0 || (_history$location = history.location) === null || _history$location === void 0 || (_history$location = _history$location.query) === null || _history$location === void 0 ? void 0 : _history$location.cacheId) || "";
		const sessionData = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, dataRef.current || {}), {}, { cacheId });
		sessionStorage.setItem(`${SALES_MODEL_SESSION_STORAGE_KEY}`, JSON.stringify(sessionData || {}));
	};
	/**
	* @title 获取sessionStorage
	* @description 获取路由参数中的cacheId并与sessionStorage中的cacheId进行匹配，如果匹配成功，则返回sessionStorage中的数据
	*/
	const getSessionStorage = () => {
		var _utils$getHistory2, _history$location2;
		const history = (utils === null || utils === void 0 || (_utils$getHistory2 = utils.getHistory) === null || _utils$getHistory2 === void 0 ? void 0 : _utils$getHistory2.call(utils)) || {};
		const strResult = sessionStorage.getItem(SALES_MODEL_SESSION_STORAGE_KEY);
		const result = strResult ? JSON.parse(strResult || "{}") : {};
		const cacheId = (history === null || history === void 0 || (_history$location2 = history.location) === null || _history$location2 === void 0 || (_history$location2 = _history$location2.query) === null || _history$location2 === void 0 ? void 0 : _history$location2.cacheId) || "";
		if (!cacheId && !(result === null || result === void 0 ? void 0 : result.cacheId)) return result;
		if (cacheId && (result === null || result === void 0 ? void 0 : result.cacheId) === cacheId) return result;
		return {};
	};
	const removeSessionStorage = () => {
		sessionStorage.removeItem(SALES_MODEL_SESSION_STORAGE_KEY);
	};
	return /* @__PURE__ */ react.default.createElement(SalesContext.Provider, { value: {
		data,
		setData: _setData,
		getCurrentData,
		hooks,
		setHooks,
		setSessionStorage,
		getSessionStorage,
		removeSessionStorage
	} }, children);
};
const useSales = () => {
	const context = react.default.useContext(SalesContext);
	if (!context) throw new Error("useSales must be used within a SalesProvider");
	return context;
};
//#endregion
exports.SalesContext = SalesContext;
exports.SalesProvider = SalesProvider;
exports.useSales = useSales;
