function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { createContext } from 'react';
import { useSales } from "../../../businessModel/SalesModel";
import { getSpecialResourceListApi } from "./serve";
import { useRequest } from 'ahooks';
import { useCart } from "../Cart";
import { getIamgeFromMedia } from "../utils";
import { useInit } from "../Init";

// 定义步骤数据接口

// 定义上下文接口

// 默认步骤状态
var defaultState = {};

// 创建步骤上下文
export var ResourceContext = /*#__PURE__*/createContext(undefined);
export var ResourceProvider = function ResourceProvider(_ref) {
  var children = _ref.children;
  var _useSales = useSales(),
    data = _useSales.data,
    setData = _useSales.setData,
    getCurrentData = _useSales.getCurrentData;
  var _useInit = useInit(),
    resetInitPartData = _useInit.resetInitPartData;
  var _useCart = useCart(),
    updateCurrentCartItem = _useCart.updateCurrentCartItem;

  // const resourceData = useMemo(() => {
  //   return (data.resourceData as ResourceData) || {};
  // }, [data]);

  var setResourceData = function setResourceData(newResourceData) {
    setData({
      resourceData: _objectSpread(_objectSpread({}, getCurrentData().resourceData || {}), newResourceData)
    });
  };
  var formatResourceList = function formatResourceList(res) {
    var _res$data;
    var resourceMap = 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(function (item) {
        var scCampus = (item === null || item === void 0 ? void 0 : item.sc_campus) || '';
        var source = resourceMap.get(scCampus);
        var 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]
          });
        }
      });
    }
    var renderData = [];
    resourceMap.forEach(function (v) {
      return renderData.push(v);
    });
    return renderData;
  };
  var getResourceList = useRequest(getSpecialResourceListApi, {
    manual: true,
    onSuccess: function onSuccess(res) {
      var formatData = formatResourceList(res);
      setResourceData({
        resourceList: formatData
      });
    }
  });

  /**
   * @title: 选择资源
   * @description: 如果当前选择的资源与数据中的资源不同，需要清除之前的数据
   * @param {any} resourceItem
   * @return {*}
   * @Author: xiangfeng.xue
   */
  var selectResource = function 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: resourceItem
    });
  };
  var contextValue = {
    getResourceList: getResourceList,
    selectResource: selectResource
  };
  return /*#__PURE__*/React.createElement(ResourceContext.Provider, {
    value: contextValue
  }, children);
};

/**
 * 使用步骤管理的Hook
 * @throws {Error} 如果在StepProvider外部使用会抛出错误
 * @returns {StepContextType} 步骤管理上下文
 */
export var useResource = function useResource() {
  var context = React.useContext(ResourceContext);
  if (!context) {
    throw new Error('useResource must be used within a ResourceProvider');
  }
  return context;
};
export default {
  defaultState: defaultState
};