"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _tslib() {
  const data = require("tslib");

  _tslib = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

var _constant = require("../utils/constant");

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const StepsItem = _a => {
  var {
    children
  } = _a,
      props = (0, _tslib().__rest)(_a, ["children"]);
  const {
    index,
    parent: parentProps
  } = props;
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('step', prefixCls);

  if (!parentProps) {
    if (process.env.NODE_ENV !== 'production') {
      // eslint-disable-next-line no-console
      console.error('[React Vant] <Step> must be a child component of <Steps>.');
    }
  }

  const getStatus = () => {
    const active = +parentProps.active;

    if (index < active) {
      return 'finish';
    }

    return index === active ? 'process' : 'waiting';
  };

  const isActive = () => getStatus() === 'process';

  const lineStyle = (0, _react().useMemo)(() => ({
    background: getStatus() === 'finish' ? parentProps.activeColor : parentProps.inactiveColor
  }), [index, parentProps.active, parentProps.activeColor, parentProps.inactiveColor]);
  const titleStyle = (0, _react().useMemo)(() => {
    if (isActive()) {
      return {
        color: parentProps.activeColor
      };
    }

    if (!getStatus()) {
      return {
        color: parentProps.inactiveColor
      };
    }

    return {};
  }, [index, parentProps.active, parentProps.activeColor, parentProps.inactiveColor]);

  const onClickStep = () => {
    if (parentProps.onClickStep) parentProps.onClickStep(index);
  };

  const renderCircle = () => {
    var _a, _b, _c;

    const {
      activeColor
    } = parentProps;
    const finishIcon = (_a = props.finishIcon) !== null && _a !== void 0 ? _a : parentProps.finishIcon;
    const activeIcon = (_b = props.activeIcon) !== null && _b !== void 0 ? _b : parentProps.activeIcon;
    const inactiveIcon = (_c = props.inactiveIcon) !== null && _c !== void 0 ? _c : parentProps.inactiveIcon;

    if (isActive()) {
      if (activeIcon) {
        return _react().default.cloneElement(activeIcon, {
          className: (0, _clsx().default)(bem('icon', 'active')),
          color: activeColor,
          style: {
            color: activeColor
          }
        });
      }
    }

    if (getStatus() === 'finish' && finishIcon) {
      return _react().default.cloneElement(finishIcon, {
        className: (0, _clsx().default)(bem('icon', 'finish')),
        color: activeColor,
        style: {
          color: activeColor
        }
      });
    }

    if (inactiveIcon) {
      return _react().default.cloneElement(inactiveIcon, {
        className: (0, _clsx().default)(bem('icon')),
        color: activeColor,
        style: {
          color: activeColor
        }
      });
    }

    return (0, _jsxRuntime().jsx)("i", {
      className: (0, _clsx().default)(bem('circle')),
      style: lineStyle
    });
  };

  const status = getStatus();
  return (0, _jsxRuntime().jsxs)("div", Object.assign({
    style: props.style,
    className: (0, _clsx().default)(props.className, _constant.BORDER, bem([parentProps.direction, {
      [status]: status
    }]))
  }, {
    children: [(0, _jsxRuntime().jsx)("div", Object.assign({
      className: (0, _clsx().default)(bem('title', {
        active: isActive()
      })),
      style: titleStyle,
      onClick: onClickStep
    }, {
      children: children
    })), (0, _jsxRuntime().jsx)("div", Object.assign({
      className: (0, _clsx().default)(bem('circle-container')),
      onClick: onClickStep
    }, {
      children: renderCircle()
    })), (0, _jsxRuntime().jsx)("div", {
      className: (0, _clsx().default)(bem('line')),
      style: lineStyle
    })]
  }));
};

var _default = StepsItem;
exports.default = _default;