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, useMemo } from 'react';
import { useSales } from "../../../businessModel/SalesModel";
// import { useHistory } from '@/hooks';
import useEngineContext from "../../../hooks/useEngineContext";

// 定义步骤接口

// 定义步骤键名类型

// 定义步骤数据接口

// 定义上下文接口

// 定义步骤配置
export var steps = [{
  key: 'select-category',
  title: 'Step 1',
  description: 'Select category'
}, {
  key: 'select-event',
  title: 'Step 2',
  description: 'Select event'
}, {
  key: 'confirm-information',
  title: 'Step 3',
  description: 'Confirm information'
}];

// 创建步骤映射表
var stepMaps = steps.reduce(function (acc, step) {
  acc[step.key] = step;
  return acc;
}, {});

// 默认步骤状态
var defaultState = {
  currentStep: steps[0].key,
  isAutoOperate: true
};

// 创建步骤上下文
export var StepContext = /*#__PURE__*/createContext(undefined);
export var StepProvider = function StepProvider(_ref) {
  var children = _ref.children;
  var _useSales = useSales(),
    data = _useSales.data,
    setData = _useSales.setData;
  // const history = useHistory();
  var context = useEngineContext();
  var utils = context.appHelper.utils || {};

  // 获取步骤数据
  var stepData = useMemo(function () {
    return data.stepData || {};
  }, [data]);

  // 获取当前步骤信息
  var currentStepData = useMemo(function () {
    return stepMaps[stepData.currentStep];
  }, [stepData]);

  // 更新步骤数据
  var setStepData = function setStepData(newStepData) {
    setData({
      stepData: _objectSpread(_objectSpread({}, data.stepData), newStepData)
    });
  };
  // 获取下一步骤的key
  var getNextStep = function getNextStep() {
    var currentStepIndex = steps.findIndex(function (step) {
      return step.key === stepData.currentStep;
    });
    if (currentStepIndex < steps.length - 1) {
      return steps[currentStepIndex + 1].key;
    }
    return null;
  };

  // 获取上一步骤的key
  var getPrevStep = function getPrevStep() {
    var currentStepIndex = steps.findIndex(function (step) {
      return step.key === stepData.currentStep;
    });
    if (currentStepIndex > 0) {
      return steps[currentStepIndex - 1].key;
    }
    return null;
  };

  // 前进到下一步
  var nextStep = function nextStep() {
    var nextStep = getNextStep();
    if (nextStep) {
      setStepData({
        currentStep: nextStep,
        isAutoOperate: true
      });
    }
  };

  // 返回上一步
  var prevStep = function prevStep() {
    var prevStep = getPrevStep();
    if (prevStep) {
      setStepData({
        currentStep: prevStep,
        isAutoOperate: false
      });
    } else {
      closePage();
    }
  };

  // 回到第一步
  var toFirstStep = function toFirstStep() {
    setStepData({
      currentStep: steps[0].key
    });
  };

  // 关闭页面
  var closePage = function closePage() {
    var _utils$history;
    utils === null || utils === void 0 || (_utils$history = utils.history) === null || _utils$history === void 0 || _utils$history.replace('/');
  };

  /**
   * @title: 是否是第一步
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-18 22:23
   */
  var isFirstStep = useMemo(function () {
    return (currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key) === steps[0].key;
  }, [currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key]);

  /**
   * @title: 是否是最后一步
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-18 22:24
   */
  var isLastStep = useMemo(function () {
    return (currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key) === steps[steps.length - 1].key;
  }, [currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key]);

  /**
   * @title: 是否是第二步
   * @description:
   * @param {*} useMemo
   * @return {*}
   * @Author: WangHan
   * @Date: 2025-01-18 22:39
   */
  var isTwoStep = useMemo(function () {
    return (currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key) === steps[1].key;
  }, [currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key]);
  var changeAutoOperate = function changeAutoOperate(newAutoOperate) {
    setStepData({
      isAutoOperate: newAutoOperate
    });
  };
  var contextValue = {
    setStepData: setStepData,
    currentStepData: currentStepData,
    nextStep: nextStep,
    prevStep: prevStep,
    closePage: closePage,
    toFirstStep: toFirstStep,
    isFirstStep: isFirstStep,
    isLastStep: isLastStep,
    isTwoStep: isTwoStep,
    changeAutoOperate: changeAutoOperate
  };
  return /*#__PURE__*/React.createElement(StepContext.Provider, {
    value: contextValue
  }, children);
};

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