"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _clsx = _interopRequireDefault(require("clsx"));

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

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

var _excluded = ["children"];

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 _getRequireWildcardCache(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; }

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; }

var StepsItem = function StepsItem(_ref) {
  var _ref2;

  var children = _ref.children,
      props = _objectWithoutPropertiesLoose(_ref, _excluded);

  var index = props.index,
      parentProps = props.parent;

  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('step', prefixCls),
      bem = _createNamespace[0];

  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>.');
    }
  }

  var getStatus = function getStatus() {
    var active = +parentProps.active;

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

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

  var isActive = function isActive() {
    return getStatus() === 'process';
  };

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

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

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

  var onClickStep = function onClickStep() {
    if (parentProps.onClickStep) parentProps.onClickStep(index);
  };

  var renderCircle = function renderCircle() {
    var _props$finishIcon, _props$activeIcon, _props$inactiveIcon;

    var activeColor = parentProps.activeColor;
    var finishIcon = (_props$finishIcon = props.finishIcon) != null ? _props$finishIcon : parentProps.finishIcon;
    var activeIcon = (_props$activeIcon = props.activeIcon) != null ? _props$activeIcon : parentProps.activeIcon;
    var inactiveIcon = (_props$inactiveIcon = props.inactiveIcon) != null ? _props$inactiveIcon : parentProps.inactiveIcon;

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

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

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

    return /*#__PURE__*/_react.default.createElement("i", {
      className: (0, _clsx.default)(bem('circle')),
      style: lineStyle
    });
  };

  var status = getStatus();
  return /*#__PURE__*/_react.default.createElement("div", {
    style: props.style,
    className: (0, _clsx.default)(props.className, _constant.BORDER, bem([parentProps.direction, (_ref2 = {}, _ref2[status] = status, _ref2)]))
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('title', {
      active: isActive()
    })),
    style: titleStyle,
    onClick: onClickStep
  }, children), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('circle-container')),
    onClick: onClickStep
  }, renderCircle()), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('line')),
    style: lineStyle
  }));
};

var _default = StepsItem;
exports.default = _default;