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); }
var _excluded = ["type", "current", "defaultCurrent", "direction", "size", "status", "initial", "items", "labelPlacement", "responsive", "onChange", "className", "style", "progressDot", "clickable", "allowClickFinished", "gridLayout", "gridColumns", "itemGap", "enableAnchor", "anchorContainer", "mobileMode", "mobileBreakpoint", "enableScrollMode", "scrollActionsConfig", "__designMode", "__id", "other"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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); }
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { Steps as AntdSteps } from 'antd';
import React, { useMemo } from 'react';
import "./PisellSteps.less";
import { useStepsState, useStepClick, useAnchor, useResponsive } from "./hooks";
import { getStepStatus, mergeStepClassName } from "./utils";
import PisellScrollView from "../PisellScrollView";

/**
 * PisellSteps 步骤条组件
 *
 * 基于 Ant Design Steps 组件进行二次封装和增强
 * 支持多种展示样式、布局方式和丰富的交互功能
 *
 * @author Pisell Team
 * @version 1.0.0
 */
var PisellSteps = function PisellSteps(_ref) {
  var _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'default' : _ref$type,
    controlledCurrent = _ref.current,
    _ref$defaultCurrent = _ref.defaultCurrent,
    defaultCurrent = _ref$defaultCurrent === void 0 ? 0 : _ref$defaultCurrent,
    _ref$direction = _ref.direction,
    direction = _ref$direction === void 0 ? 'horizontal' : _ref$direction,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 'default' : _ref$size,
    _ref$status = _ref.status,
    status = _ref$status === void 0 ? 'process' : _ref$status,
    _ref$initial = _ref.initial,
    initial = _ref$initial === void 0 ? 0 : _ref$initial,
    _ref$items = _ref.items,
    items = _ref$items === void 0 ? [] : _ref$items,
    _ref$labelPlacement = _ref.labelPlacement,
    labelPlacement = _ref$labelPlacement === void 0 ? 'horizontal' : _ref$labelPlacement,
    _ref$responsive = _ref.responsive,
    responsive = _ref$responsive === void 0 ? true : _ref$responsive,
    onChange = _ref.onChange,
    className = _ref.className,
    style = _ref.style,
    progressDot = _ref.progressDot,
    _ref$clickable = _ref.clickable,
    clickable = _ref$clickable === void 0 ? false : _ref$clickable,
    _ref$allowClickFinish = _ref.allowClickFinished,
    allowClickFinished = _ref$allowClickFinish === void 0 ? true : _ref$allowClickFinish,
    _ref$gridLayout = _ref.gridLayout,
    gridLayout = _ref$gridLayout === void 0 ? false : _ref$gridLayout,
    gridColumns = _ref.gridColumns,
    itemGap = _ref.itemGap,
    _ref$enableAnchor = _ref.enableAnchor,
    enableAnchor = _ref$enableAnchor === void 0 ? false : _ref$enableAnchor,
    anchorContainer = _ref.anchorContainer,
    _ref$mobileMode = _ref.mobileMode,
    mobileMode = _ref$mobileMode === void 0 ? 'full' : _ref$mobileMode,
    _ref$mobileBreakpoint = _ref.mobileBreakpoint,
    mobileBreakpoint = _ref$mobileBreakpoint === void 0 ? 744 : _ref$mobileBreakpoint,
    _ref$enableScrollMode = _ref.enableScrollMode,
    enableScrollMode = _ref$enableScrollMode === void 0 ? false : _ref$enableScrollMode,
    scrollActionsConfig = _ref.scrollActionsConfig,
    __designMode = _ref.__designMode,
    __id = _ref.__id,
    other = _ref.other,
    restProps = _objectWithoutProperties(_ref, _excluded);
  // 状态管理：支持受控和非受控模式
  var _useStepsState = useStepsState(controlledCurrent, defaultCurrent, onChange),
    _useStepsState2 = _slicedToArray(_useStepsState, 2),
    currentStep = _useStepsState2[0],
    setCurrentStep = _useStepsState2[1];

  // 处理步骤点击逻辑
  var handleStepClick = useStepClick(currentStep, clickable, allowClickFinished, setCurrentStep);

  // 锚链接功能
  useAnchor(currentStep, enableAnchor, anchorContainer);

  // 响应式监听（使用自定义断点）
  var isMobile = useResponsive(mobileBreakpoint);

  // 判断是否显示移动端当前步骤视图
  var showMobileCurrentView = useMemo(function () {
    if (mobileMode === 'current') return true;
    if (mobileMode === 'auto' && isMobile) return true;
    return false;
  }, [mobileMode, isMobile]);

  // 确保 items 始终是数组
  var safeItems = useMemo(function () {
    if (!items) return [];
    if (!Array.isArray(items)) return [];
    return items;
  }, [items]);

  // 获取当前步骤信息（用于移动端显示）
  var currentStepItem = useMemo(function () {
    var visibleItems = safeItems.filter(function (item) {
      return !item.hidden;
    });
    var adjustedIndex = currentStep - initial;
    // 确保索引在有效范围内
    if (adjustedIndex < 0 || adjustedIndex >= visibleItems.length) {
      return null;
    }
    return visibleItems[adjustedIndex] || null;
  }, [safeItems, currentStep, initial]);

  // 处理步骤数据
  var processedItems = useMemo(function () {
    // 过滤隐藏的步骤
    var visibleItems = safeItems.filter(function (item) {
      return !item.hidden;
    });

    // 设计模式下提供默认数据
    if (__designMode === 'design' && visibleItems.length === 0) {
      return [{
        title: '步骤一',
        description: 'This is a description.',
        status: 'finish'
      }, {
        title: '步骤二',
        description: 'This is a description.',
        status: 'process'
      }, {
        title: '步骤三',
        description: 'This is a description.',
        status: 'wait'
      }];
    }

    // 处理每个步骤项
    return visibleItems.filter(function (item) {
      return item != null;
    }) // 过滤掉 null 或 undefined 的项
    .map(function (item, index) {
      var adjustedIndex = index + initial;

      // 计算步骤状态
      // 优先级：item.status > 当前步骤使用全局status > 根据索引自动计算
      var stepStatus;
      if (item.status) {
        // 如果步骤项自己有状态，优先使用
        stepStatus = item.status;
      } else if (adjustedIndex === currentStep) {
        // 如果是当前步骤，使用全局的 status
        stepStatus = status;
      } else {
        // 其他步骤根据索引自动计算
        stepStatus = getStepStatus(adjustedIndex, currentStep);
      }

      // 合并类名
      var stepClassName = mergeStepClassName('pisell-steps-item', stepStatus, item.className);

      // 处理点击事件
      var handleClick = function handleClick() {
        if (!item.disabled) {
          var _item$onClick;
          (_item$onClick = item.onClick) === null || _item$onClick === void 0 || _item$onClick.call(item);
          handleStepClick(adjustedIndex, item);
        }
      };
      return _objectSpread(_objectSpread({}, item), {}, {
        status: stepStatus,
        className: stepClassName,
        onClick: clickable || item.onClick ? handleClick : undefined
      });
    });
  }, [safeItems, currentStep, initial, status, clickable, handleStepClick, __designMode]);

  // 合并样式类名
  var wrapperClassName = useMemo(function () {
    var classes = ['pisell-steps'];
    if (className) classes.push(className);
    // type 只支持 'default' 和 'navigation'，dot 模式通过 progressDot 控制
    if (type === 'navigation') classes.push('pisell-steps-navigation');
    // 如果设置了 progressDot，添加 dot 样式类
    if (progressDot) classes.push('pisell-steps-dot');
    if (size !== 'default') classes.push("pisell-steps-".concat(size));
    if (direction === 'vertical') classes.push('pisell-steps-vertical');
    if (responsive) classes.push('pisell-steps-responsive');
    if (clickable) classes.push('pisell-steps-clickable');
    if (gridLayout) classes.push('pisell-steps-grid-layout');
    // 如果设置了 itemGap，添加标记类名
    if (itemGap !== undefined && itemGap !== null) {
      classes.push('pisell-steps-has-item-gap');
    }
    // labelPlacement 通过 antd Steps 的 labelPlacement 属性控制，不需要额外的类名

    return classes.join(' ');
  }, [className, type, progressDot, size, direction, responsive, clickable, gridLayout, itemGap]);

  // 合并自定义样式
  var wrapperStyle = useMemo(function () {
    var customStyle = _objectSpread({}, style);

    // itemGap 用于控制图标和文本之间的间距
    // 只有当 itemGap 有值时才设置 CSS 变量，否则保持 antd 原样式
    if (itemGap !== undefined && itemGap !== null) {
      customStyle['--pisell-steps-item-gap'] = "".concat(itemGap, "px");
      // 添加标记类名，用于样式选择器
      customStyle['--pisell-steps-has-item-gap'] = '1';
    }
    if (gridColumns && gridLayout) {
      customStyle['--pisell-steps-grid-columns'] = gridColumns;
    }
    return customStyle;
  }, [style, itemGap, gridColumns, gridLayout]);

  // 默认滚动箭头配置
  var defaultScrollActionsConfig = useMemo(function () {
    return _objectSpread({
      show: true,
      type: 'lrArrows',
      scrollDistance: 200
    }, scrollActionsConfig);
  }, [scrollActionsConfig]);

  // 渲染步骤条内容
  var renderStepsContent = function renderStepsContent() {
    var forceHorizontal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
    return /*#__PURE__*/React.createElement(AntdSteps, _extends({
      current: Math.max(0, currentStep - initial)
      // 滚动模式下强制水平方向
      ,
      direction: forceHorizontal ? 'horizontal' : direction,
      size: size === 'large' ? 'default' : size,
      status: status,
      items: processedItems,
      labelPlacement: labelPlacement,
      type: type,
      progressDot: progressDot
      // 滚动模式下禁用 Ant Design 的响应式行为
      ,
      responsive: forceHorizontal ? false : responsive
    }, restProps, other || {}));
  };

  // 渲染桌面端步骤条（可能带滚动）
  var renderDesktopSteps = function renderDesktopSteps() {
    if (enableScrollMode) {
      return /*#__PURE__*/React.createElement(PisellScrollView, {
        className: "pisell-steps-scroll-view",
        overflow: "x",
        scrollbarStyle: "none",
        scrollActionsConfig: defaultScrollActionsConfig,
        snapScroll: {
          enabled: false
        }
      }, /*#__PURE__*/React.createElement("div", {
        className: "pisell-steps-scroll-content"
      }, renderStepsContent(true)));
    }
    return renderStepsContent(false);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: showMobileCurrentView ? "pisell-steps pisell-steps-mobile-current ".concat(className || '') : wrapperClassName,
    style: showMobileCurrentView ? style : wrapperStyle,
    "data-component": "pisell-steps",
    "data-id": __id
  }, showMobileCurrentView ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-steps-mobile-current__info"
  }, (currentStepItem === null || currentStepItem === void 0 ? void 0 : currentStepItem.title) && /*#__PURE__*/React.createElement("span", {
    className: "pisell-steps-mobile-current__number"
  }, currentStepItem.title), (currentStepItem === null || currentStepItem === void 0 ? void 0 : currentStepItem.description) && /*#__PURE__*/React.createElement("span", {
    className: "pisell-steps-mobile-current__title"
  }, currentStepItem.description)) : /*#__PURE__*/React.createElement(React.Fragment, null, renderDesktopSteps()));
};

// 添加 displayName 用于调试
PisellSteps.displayName = 'PisellSteps';
export default PisellSteps;