var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessModel/SalesModel/Sales/index.tsx
var Sales_exports = {};
__export(Sales_exports, {
  SalesContext: () => SalesContext,
  SalesProvider: () => SalesProvider,
  useSales: () => useSales
});
module.exports = __toCommonJS(Sales_exports);
var import_react = __toESM(require("react"));
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var defaultData = {};
var SALES_MODEL_SESSION_STORAGE_KEY = "shop_h5_sales_model_data";
var SalesContext = (0, import_react.createContext)(
  {}
);
var SalesProvider = (props) => {
  const { children, config } = props;
  const dataRef = (0, import_react.useRef)({});
  const [data, setData] = (0, import_react.useState)(defaultData);
  const [hooks, setHooks] = (0, import_react.useState)(props.hooks);
  const context = (0, import_useEngineContext.default)();
  const utils = context.appHelper.utils || {};
  const _setData = (newData) => {
    dataRef.current = { ...dataRef.current, ...newData };
    console.log("sales_model_newData", dataRef.current);
    if (config == null ? void 0 : config.useSessionStorage) {
      setSessionStorage();
    }
    setData({ ...dataRef.current });
  };
  const getCurrentData = () => {
    return dataRef.current;
  };
  const setSessionStorage = () => {
    var _a, _b, _c;
    const history = ((_a = utils == null ? void 0 : utils.getHistory) == null ? void 0 : _a.call(utils)) || {};
    const cacheId = ((_c = (_b = history == null ? void 0 : history.location) == null ? void 0 : _b.query) == null ? void 0 : _c.cacheId) || "";
    const sessionData = { ...dataRef.current || {}, cacheId };
    sessionStorage.setItem(
      `${SALES_MODEL_SESSION_STORAGE_KEY}`,
      JSON.stringify(sessionData || {})
    );
  };
  const getSessionStorage = () => {
    var _a, _b, _c;
    const history = ((_a = utils == null ? void 0 : utils.getHistory) == null ? void 0 : _a.call(utils)) || {};
    const strResult = sessionStorage.getItem(SALES_MODEL_SESSION_STORAGE_KEY);
    const result = strResult ? JSON.parse(strResult || "{}") : {};
    const cacheId = ((_c = (_b = history == null ? void 0 : history.location) == null ? void 0 : _b.query) == null ? void 0 : _c.cacheId) || "";
    if (!cacheId && !(result == null ? void 0 : result.cacheId)) {
      return result;
    }
    if (cacheId && (result == null ? void 0 : result.cacheId) === cacheId) {
      return result;
    }
    return {};
  };
  const removeSessionStorage = () => {
    sessionStorage.removeItem(SALES_MODEL_SESSION_STORAGE_KEY);
  };
  return /* @__PURE__ */ import_react.default.createElement(
    SalesContext.Provider,
    {
      value: {
        data,
        setData: _setData,
        getCurrentData,
        hooks,
        setHooks,
        setSessionStorage,
        getSessionStorage,
        removeSessionStorage
      }
    },
    children
  );
};
var useSales = () => {
  const context = import_react.default.useContext(SalesContext);
  if (!context) {
    throw new Error("useSales must be used within a SalesProvider");
  }
  return context;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SalesContext,
  SalesProvider,
  useSales
});
