var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/modules/Step/index.ts
var Step_exports = {};
__export(Step_exports, {
  StepModule: () => StepModule
});
module.exports = __toCommonJS(Step_exports);
var import_BaseModule = require("../BaseModule");
var StepModule = class extends import_BaseModule.BaseModule {
  constructor(name, version) {
    super(name, version);
    this.defaultName = "step";
    this.defaultVersion = "1.0.0";
    this.openCache = false;
  }
  async initialize(core, options) {
    var _a, _b;
    this.core = core;
    this.store = options.store;
    if (options.initialState) {
      this.store.stepList = options.initialState.stepList;
      this.store.currentStep = options.initialState.currentStep;
      this.store.currentStepIndex = options.initialState.currentStepIndex;
    }
    if ((_a = options.otherParams) == null ? void 0 : _a.cacheId) {
      this.openCache = options.otherParams.openCache;
      this.cacheId = options.otherParams.cacheId;
    }
    if ((_b = options.otherParams) == null ? void 0 : _b.fatherModule) {
      this.fatherModule = options.otherParams.fatherModule;
    }
  }
  init(steps) {
    this.setSteps(steps);
    this.setCurrentStep(0);
  }
  setSteps(stepList) {
    this.store.stepList = stepList;
  }
  setCurrentStep(stepIndex) {
    var _a;
    if (stepIndex < 0 || stepIndex >= ((_a = this.store.stepList) == null ? void 0 : _a.length)) {
      return;
    }
    this.store.currentStepIndex = stepIndex;
    this.store.currentStep = this.store.stepList[stepIndex];
  }
  getStepList() {
    return this.store.stepList;
  }
  getCurrentStep() {
    return this.store.currentStep;
  }
  getCurrentStepIndex() {
    return this.store.currentStepIndex;
  }
  /**
   * 上一个步骤
   */
  prevStep() {
    var _a;
    let prevStepIndex = this.store.currentStepIndex - 1;
    while ((_a = this.store.stepList[prevStepIndex]) == null ? void 0 : _a.isSkip) {
      prevStepIndex--;
    }
    if (prevStepIndex >= 0) {
      this.setCurrentStep(prevStepIndex);
    }
  }
  /**
   * 下一个步骤
   */
  nextStep() {
    var _a;
    let nextStepIndex = this.store.currentStepIndex + 1;
    while ((_a = this.store.stepList[nextStepIndex]) == null ? void 0 : _a.isSkip) {
      nextStepIndex++;
    }
    if (nextStepIndex < this.store.stepList.length) {
      this.setCurrentStep(nextStepIndex);
    }
  }
  /**
   * 跳转到指定步骤
   * @param stepIndex 步骤的 index
   */
  gotoStep(stepIndex) {
    this.setCurrentStep(stepIndex);
  }
  /**
   * 添加步骤
   * @param step 步骤
   * @param key 步骤的 key，如果存在则插入到该步骤之后，否则插入到当前步骤之后
   */
  addStep(step, key) {
    let stepList = this.store.stepList;
    const index = stepList.findIndex((n) => n.key === step.key);
    if (index !== -1) {
      console.log("[StepModule] 步骤已存在，添加失败");
      return;
    }
    if (key) {
      const index2 = this.store.stepList.findIndex((n) => n.key === key);
      if (index2 !== -1) {
        stepList.splice(index2 + 1, 0, step);
      } else {
        stepList = [...stepList, step];
      }
    } else {
      stepList.splice(this.store.currentStepIndex + 1, 0, step);
    }
    this.store.stepList = [...stepList];
  }
  /**
   * 删除步骤
   * @param key 步骤的 key
   */
  removeStep(key) {
    const newStepList = this.store.stepList.filter((n) => n.key !== key);
    this.store.stepList = [...newStepList];
  }
  /**
   * 更新步骤
   * @param key 步骤的 key
   * @param step 步骤
   */
  updateStep(key, step) {
    var _a, _b;
    const newStepList = (_a = this.store.stepList) == null ? void 0 : _a.map((n) => {
      if (n.key === key) {
        return step;
      }
      return n;
    });
    if (key === ((_b = this.store.currentStep) == null ? void 0 : _b.key)) {
      this.store.currentStep = step;
    }
    this.store.stepList = [...newStepList || []];
  }
  storeChange() {
    if (this.openCache) {
      this.checkSaveCache({
        cacheId: this.cacheId,
        fatherModule: this.fatherModule,
        store: this.store,
        cacheKey: ["currentStepIndex", "currentStep", "stepList"]
      });
    }
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  StepModule
});
