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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure " + obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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); }
/* eslint-disable no-case-declarations */
import React, { createContext, useContext } from 'react';
import { StatusEnum, ModeEnum, PayRouteEnum } from "../const";
import { cloneDeep } from "../helper";
import useThunkReducer from "./useThunkReducer";
export var EActionTypes = /*#__PURE__*/function (EActionTypes) {
  EActionTypes["UPDATE_INIT"] = "UPDATE_INIT";
  EActionTypes["UPDATE_CUSTOM"] = "UPDATE_CUSTOM";
  EActionTypes["UPDATE_KEY"] = "UPDATE_KEY";
  EActionTypes["UPDATE_STEP_KEY"] = "UPDATE_STEP_KEY";
  EActionTypes["UPDATE_DEVICE"] = "UPDATE_DEVICE";
  EActionTypes["UPDATE_NEXT_STEP"] = "UPDATE_NEXT_STEP";
  EActionTypes["UPDATE_STATUS"] = "UPDATE_STATUS";
  EActionTypes["UPDATE_TITLE"] = "UPDATE_TITLE";
  EActionTypes["UPDATE_LOADING"] = "UPDATE_LOADING";
  EActionTypes["UPDATE_COMPONENT"] = "UPDATE_COMPONENT";
  EActionTypes["UPDATE_STEPS"] = "UPDATE_STEPS";
  EActionTypes["UPDATE_STEP_CURRENT_ITEM_CUSTOM"] = "UPDATE_STEP_CURRENT_ITEM_CUSTOM";
  EActionTypes["UPDATE_STEP_ITEM"] = "UPDATE_STEP_ITEM";
  EActionTypes["BACKUP_PRODUCTION"] = "BACKUP_PRODUCTION";
  EActionTypes["BACKUP_FREE"] = "BACKUP_FREE";
  EActionTypes["UPDATE_CLIENT"] = "UPDATE_CLIENT";
  return EActionTypes;
}({});
/**
 * @title: 刷新 key
 * @description: 用户重置组件生命周期,
 * @param {number} key
 * @Author: hongbing.wang
 * @Date: 2024-08-19 09:56
 */
export var updateKey = function updateKey(key) {
  return {
    type: EActionTypes.UPDATE_KEY,
    payload: {
      key: key
    }
  };
};

/**
 * @title: 更新设备信息
 * @description: 用户选择设备将回填到此处
 * @param {object} payload
 * @Author: hongbing.wang
 * @Date: 2024-08-19 09:57
 */
export var updateDevice = function updateDevice(payload) {
  return {
    type: EActionTypes.UPDATE_DEVICE,
    payload: payload
  };
};

/**
 * @title: 刷新 步骤key
 * @description: 重置某一步生命周期
 * @Author: hongbing.wang
 * @Date: 2024-08-19 09:58
 */
export var updateStepKey = function updateStepKey() {
  return {
    type: EActionTypes.UPDATE_STEP_KEY,
    payload: {}
  };
};

/**
 * @title: 更新至下一步
 * @description: 更新至下一步
 * @param {string} warn?: 错误信息
 * @Author: hongbing.wang
 * @Date: 2024-08-19 09:58
 */
export var updateNextStep = function updateNextStep(warn) {
  return {
    type: EActionTypes.UPDATE_NEXT_STEP,
    payload: warn ? {
      warn: warn
    } : {}
  };
};

/**
 * @title: 更新自定义信息
 * @description: 更新自定义信息
 * @param {object} payload
 * @Author: hongbing.wang
 * @Date: 2024-08-19 10:03
 */
export var updateCustom = function updateCustom(payload) {
  return {
    type: EActionTypes.UPDATE_CUSTOM,
    payload: payload
  };
};

/**
 * @title: 更新 全局状态
 * @description: 更新 全局状态
 * @param {string} status : 状态
 *  * @Author: hongbing.wang
 * @Date: 2024-08-19 10:03
 */
export var updateStatus = function updateStatus(status) {
  return {
    type: EActionTypes.UPDATE_LOADING,
    payload: {
      status: status
    }
  };
};

/**
 * @title: 更新自定义组件
 * @description: 更新自定义组件
 * @param {string} component 组件名称
 * @param {boolean} render : true : 隐藏 原组件(维持原组件内状态)  ;false : 卸载
 * @return {*}
 * @Author: hongbing.wang
 * @Date: 2024-08-19 10:04
 */
export var updateComponent = function updateComponent(component, render) {
  return {
    type: EActionTypes.UPDATE_COMPONENT,
    payload: component ? {
      component: component,
      type: 'unset',
      render: typeof render === 'boolean' ? render : false
    } : {
      type: 'step',
      render: typeof render === 'boolean' ? render : true
    }
  };
};

/**
 * @title: 更新步骤信息
 * @description: 更新某一步骤信息
 * @param {State} steps 某一步骤信息
 * @Author: hongbing.wang
 * @Date: 2024-08-19 10:07
 */
export var updateSteps = function updateSteps(steps) {
  return {
    type: EActionTypes.UPDATE_STEPS,
    payload: {
      steps: steps
    }
  };
};

/**
 * @title: 更新状态管理信息
 * @description: 更新状态管理信息
 * @param {Partial} payload
 * @return {*}
 * @Author: hongbing.wang
 * @Date: 2024-08-19 10:08
 */
export var updateState = function updateState(payload) {
  return {
    type: EActionTypes.UPDATE_INIT,
    payload: payload
  };
};

/**
 * @title: 更新当前步骤自定义信息
 * @description: 更新当前步骤自定义信息
 * @Author: hongbing.wang
 * @Date: 2024-08-19 10:09
 */
export var updateCurrentCustomStemItem = function updateCurrentCustomStemItem(payload) {
  return {
    type: EActionTypes.UPDATE_STEP_CURRENT_ITEM_CUSTOM,
    payload: payload
  };
};

/**
 * @title: 更新当前步骤信息
 * @description: 更新当前步骤信息
 * @Author: hongbing.wang
 * @Date: 2024-08-19 10:09
 */
export var updateCurrentStemItem = function updateCurrentStemItem(payload) {
  return {
    type: EActionTypes.UPDATE_STEP_ITEM,
    payload: payload
  };
};

/**
 * @title: 更新当前步骤信息
 * @description: 更新当前步骤信息
 * @Author: hongbing.wang
 * @Date: 2024-08-19 10:09
 */
export var updateClient = function updateClient(payload) {
  return {
    type: EActionTypes.UPDATE_CLIENT,
    payload: {
      client: payload
    }
  };
};

/**
 * @title: 更新备份信息
 * @description: 更新备份信息
 * @Author: hongbing.wang
 * @Date: 2024-08-19 10:25
 */
export var backUpProduction = function backUpProduction(payload) {
  return {
    type: EActionTypes.BACKUP_PRODUCTION,
    payload: payload
  };
};

/**
 * @title: 释放备份信息
 * @description: 释放备份信息
 * @param {Partial} payload
 * @Author: hongbing.wang
 * @Date: 2024-08-19 10:27
 */
export var backUpFree = function backUpFree(payload) {
  return {
    type: EActionTypes.BACKUP_FREE,
    payload: _objectSpread({}, payload)
  };
};
var initialState = {
  name: '',
  symbol: '',
  amount: '',
  order_id: '',
  mode: ModeEnum.Pay,
  eftpos: '',
  key: 0,
  step: 0,
  type: PayRouteEnum.Step,
  title: '',
  net: true,
  component: '',
  status: StatusEnum.Loading,
  steps: [],
  warn: '',
  render: true
};
var StateContext = /*#__PURE__*/createContext(undefined);
var DispatchContext = /*#__PURE__*/createContext(undefined);
export function reducers(state, action) {
  switch (action.type) {
    case EActionTypes.UPDATE_INIT:
      return _objectSpread(_objectSpread({}, state), action.payload);
    case EActionTypes.UPDATE_NEXT_STEP:
      // eslint-disable-next-line no-case-declarations
      var currentStep = state.step + 1;
      if (state.step === state.steps.length - 1) {
        state.steps[state.steps.length - 1].status = 'success';
        return _objectSpread(_objectSpread({}, state), {}, {
          status: 'success',
          steps: _toConsumableArray(state.steps)
        });
      }
      state.steps[state.step].status = 'success';
      return _objectSpread(_objectSpread({}, state), {}, {
        warn: action.payload.warn || '',
        steps: _toConsumableArray(state.steps),
        status: state.steps[currentStep].status,
        step: currentStep
      });
    case EActionTypes.UPDATE_KEY:
      return _objectSpread(_objectSpread({}, state), action.payload);
    case EActionTypes.UPDATE_LOADING:
      return _objectSpread(_objectSpread({}, state), action.payload);
    case EActionTypes.UPDATE_COMPONENT:
      return _objectSpread(_objectSpread(_objectSpread({}, state), action.payload), {}, {
        form: state.component
      });
    case EActionTypes.UPDATE_STEPS:
      return _objectSpread(_objectSpread({}, state), action.payload);
    case EActionTypes.UPDATE_STEP_ITEM:
      state.steps[state.step] = _objectSpread(_objectSpread({}, state.steps[state.step]), action.payload);
      return _objectSpread(_objectSpread({}, state), {}, {
        steps: _toConsumableArray(state.steps)
      });
    case EActionTypes.UPDATE_STEP_CURRENT_ITEM_CUSTOM:
      if (state.steps[state.step].custom) {
        state.steps[state.step].custom = _objectSpread(_objectSpread({}, state.steps[state.step].custom), action.payload);
      } else {
        state.steps[state.step].custom = _objectSpread({}, action.payload);
      }
      return _objectSpread(_objectSpread({}, state), {}, {
        steps: _toConsumableArray(state.steps)
      });
    case EActionTypes.UPDATE_STEP_KEY:
      state.steps[state.step].key = Date.now();
      return _objectSpread(_objectSpread({}, state), {}, {
        steps: _toConsumableArray(state.steps)
      });
    case EActionTypes.UPDATE_DEVICE:
      return _objectSpread(_objectSpread({}, state), action.payload);
    case EActionTypes.UPDATE_CUSTOM:
      return _objectSpread(_objectSpread({}, state), {}, {
        custom: state.custom ? _objectSpread(_objectSpread({}, state.custom), action.payload) : _objectSpread({}, action.payload)
        // ,
      });
    case EActionTypes.BACKUP_PRODUCTION:
      return _objectSpread(_objectSpread({}, state), {}, {
        backup: cloneDeep(state)
      });
    case EActionTypes.BACKUP_FREE:
      if (!state.backup) {
        return state;
      }
      var s = Object.assign({}, (_objectDestructuringEmpty(state.backup), state.backup));
      return _objectSpread(_objectSpread({}, s), action.payload);
    case EActionTypes.UPDATE_CLIENT:
      return _objectSpread(_objectSpread({}, state), {}, {
        client: action.payload['client']
      });
    default:
      {
        throw new Error("Unhandled action type: ".concat(action.type));
      }
  }
}
export var Provider = function Provider(_ref) {
  var children = _ref.children,
    providerState = _ref.state;
  var _useThunkReducer = useThunkReducer(reducers, providerState || initialState),
    _useThunkReducer2 = _slicedToArray(_useThunkReducer, 2),
    state = _useThunkReducer2[0],
    dispatch = _useThunkReducer2[1];
  return /*#__PURE__*/React.createElement(StateContext.Provider, {
    value: state
  }, /*#__PURE__*/React.createElement(DispatchContext.Provider, {
    value: dispatch
  }, children));
};
export function useContextData() {
  var context = useContext(StateContext);
  if (context === undefined) {
    throw new Error('useState must be used within a Provider');
  }
  return context;
}
export function useDispatch() {
  var context = useContext(DispatchContext);
  if (context === undefined) {
    throw new Error('useDispatch must be used within a Provider');
  }
  return context;
}