import React, { createContext } from 'react';
import useEngineContext from "../../../hooks/useEngineContext";

// 定义上下文接口

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

// 创建步骤上下文
export var CustomerContext = /*#__PURE__*/createContext(undefined);
export var CustomerProvider = function CustomerProvider(_ref) {
  var children = _ref.children;
  var context = useEngineContext();
  var utils = context.appHelper.utils || {};
  var isLogin = function isLogin() {
    var _utils$token, _utils$token$get;
    return !!(utils !== null && utils !== void 0 && (_utils$token = utils.token) !== null && _utils$token !== void 0 && (_utils$token$get = _utils$token.get) !== null && _utils$token$get !== void 0 && _utils$token$get.call(_utils$token));
  };
  var openLoginModal = function openLoginModal(params) {
    var _ref2 = params || {},
      callback = _ref2.callback;
    utils.action({
      type: 'pisell1.login',
      data: {
        type: 'plugin'
      },
      callback: callback
    });
  };
  var getUserInfo = function getUserInfo() {
    var _utils$getStore$getSt;
    var userInfo = (_utils$getStore$getSt = utils.getStore().getState()) === null || _utils$getStore$getSt === void 0 || (_utils$getStore$getSt = _utils$getStore$getSt.global) === null || _utils$getStore$getSt === void 0 ? void 0 : _utils$getStore$getSt.userInfo;
    var localUserInfo = JSON.parse(utils.storage.get('customer') || '{}');
    return userInfo !== null && userInfo !== void 0 && userInfo.id ? userInfo : localUserInfo;
  };
  var contextValue = {
    appContext: context,
    appUtils: utils,
    isLogin: isLogin,
    openLoginModal: openLoginModal,
    getUserInfo: getUserInfo
  };
  return /*#__PURE__*/React.createElement(CustomerContext.Provider, {
    value: contextValue
  }, children);
};

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