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 _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 _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 _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; }
import { useState, useCallback, useMemo, useEffect } from 'react';

/**
 * useStepManager Hook 配置
 */

/**
 * useStepManager Hook 返回值
 */

/**
 * 步骤管理 Hook
 * 
 * @description 管理多步骤流程的状态和操作
 * @example
 * const { currentStep, goPrev, goNext } = useStepManager({
 *   stepsProps: { items: [...] },
 *   defaultCurrent: 0,
 *   onPrev: (current) => { ... },
 *   onNext: (current) => { ... },
 *   onStepsChange: (result) => { ... },
 * });
 */
export var useStepManager = function useStepManager(_ref) {
  var stepsProps = _ref.stepsProps,
    _ref$defaultCurrent = _ref.defaultCurrent,
    defaultCurrent = _ref$defaultCurrent === void 0 ? 0 : _ref$defaultCurrent,
    onPrev = _ref.onPrev,
    onNext = _ref.onNext,
    onStepsChange = _ref.onStepsChange;
  // 步骤列表
  var items = Array.isArray(stepsProps === null || stepsProps === void 0 ? void 0 : stepsProps.items) ? stepsProps.items : [];
  var totalSteps = items.length > 0 ? items.length : 1;

  // 确保 defaultCurrent 是有效数字且在有效范围内
  var safeDefaultCurrent = useMemo(function () {
    var num = Number(defaultCurrent);
    if (isNaN(num) || num < 0) return 0;
    if (num >= totalSteps) return Math.max(0, totalSteps - 1);
    return num;
  }, [defaultCurrent, totalSteps]);
  var _useState = useState(safeDefaultCurrent),
    _useState2 = _slicedToArray(_useState, 2),
    currentStep = _useState2[0],
    setCurrentStep = _useState2[1];

  // 当 totalSteps 变化时，确保 currentStep 在有效范围内
  useEffect(function () {
    if (currentStep >= totalSteps) {
      setCurrentStep(Math.max(0, totalSteps - 1));
    }
  }, [totalSteps, currentStep]);

  /**
   * 更新步骤
   */
  var updateStep = useCallback(function (step) {
    // 确保步骤在有效范围内
    var safeStep = Math.max(0, Math.min(totalSteps - 1, step));
    setCurrentStep(safeStep);
    var result = {
      success: true,
      currentStep: safeStep,
      currentItem: items[safeStep] || null
    };
    onStepsChange === null || onStepsChange === void 0 || onStepsChange(result);
    return result;
  }, [items, totalSteps, onStepsChange]);

  /**
   * 上一步
   */
  var goPrev = useCallback(function () {
    if (currentStep <= 0 || totalSteps === 0) return null;

    // 触发 onPrev 事件，如果返回 false 则阻止步骤更新
    var preventUpdate = onPrev === null || onPrev === void 0 ? void 0 : onPrev(currentStep);
    if (preventUpdate === false) return null;
    var newStep = Math.max(0, currentStep - 1);
    return updateStep(newStep);
  }, [currentStep, totalSteps, onPrev, updateStep]);

  /**
   * 下一步
   */
  var goNext = useCallback(function () {
    if (currentStep >= totalSteps - 1 || totalSteps === 0) return null;

    // 触发 onNext 事件，如果返回 false 则阻止步骤更新
    var preventUpdate = onNext === null || onNext === void 0 ? void 0 : onNext(currentStep);
    if (preventUpdate === false) return null;
    var newStep = Math.min(totalSteps - 1, currentStep + 1);
    return updateStep(newStep);
  }, [currentStep, totalSteps, onNext, updateStep]);

  /**
   * 跳转到指定步骤
   */
  var goToStep = useCallback(function (step) {
    // 验证步骤索引是否有效
    if (totalSteps === 0) return null;
    if (step < 0 || step >= totalSteps) return null;
    if (step === currentStep) return null;

    // 直接更新步骤，不触发 onPrev/onNext 事件
    return updateStep(step);
  }, [currentStep, totalSteps, updateStep]);
  return {
    currentStep: currentStep,
    items: items,
    totalSteps: totalSteps,
    goPrev: goPrev,
    goNext: goNext,
    goToStep: goToStep
  };
};