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/components/Sales/Resource/index.tsx
var Resource_exports = {};
__export(Resource_exports, {
  ResourceContext: () => ResourceContext,
  ResourceProvider: () => ResourceProvider,
  default: () => Resource_default,
  useResource: () => useResource
});
module.exports = __toCommonJS(Resource_exports);
var import_react = __toESM(require("react"));
var import_SalesModel = require("../../../businessModel/SalesModel");
var import_serve = require("./serve");
var import_ahooks = require("ahooks");
var import_Cart = require("../Cart");
var import_utils = require("../utils");
var import_Init = require("../Init");
var defaultState = {};
var ResourceContext = (0, import_react.createContext)(
  void 0
);
var ResourceProvider = ({
  children
}) => {
  const { data, setData, getCurrentData } = (0, import_SalesModel.useSales)();
  const { resetInitPartData } = (0, import_Init.useInit)();
  const { updateCurrentCartItem } = (0, import_Cart.useCart)();
  const setResourceData = (newResourceData) => {
    setData({
      resourceData: {
        ...getCurrentData().resourceData || {},
        ...newResourceData
      }
    });
  };
  const formatResourceList = (res) => {
    var _a, _b;
    const resourceMap = /* @__PURE__ */ new Map();
    if (((_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.list) == null ? void 0 : _b.length) > 0) {
      res.data.list.forEach((item) => {
        const scCampus = (item == null ? 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: (0, import_utils.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, import_ahooks.useRequest)(import_serve.getSpecialResourceListApi, {
    manual: true,
    onSuccess: (res) => {
      const formatData = formatResourceList(res);
      setResourceData({
        resourceList: formatData
      });
    }
  });
  const selectResource = (resourceItem) => {
    var _a, _b;
    if (((_b = (_a = getCurrentData().resourceData) == null ? void 0 : _a.currentResourceItem) == null ? void 0 : _b.id) !== (resourceItem == null ? void 0 : resourceItem.id)) {
      resetInitPartData();
    }
    setResourceData({
      currentResourceItem: resourceItem
    });
    updateCurrentCartItem({ resourceItem });
  };
  const contextValue = {
    getResourceList,
    selectResource
  };
  return /* @__PURE__ */ import_react.default.createElement(ResourceContext.Provider, { value: contextValue }, children);
};
var useResource = () => {
  const context = import_react.default.useContext(ResourceContext);
  if (!context) {
    throw new Error("useResource must be used within a ResourceProvider");
  }
  return context;
};
var Resource_default = {
  defaultState
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ResourceContext,
  ResourceProvider,
  useResource
});
