var _excluded = ["children"];

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 React, { useContext, useMemo } from 'react';
import cls from 'clsx';
import { BORDER } from '../utils/constant';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

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

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

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

  var _useContext = useContext(ConfigProviderContext),
      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 = useMemo(function () {
    return {
      background: getStatus() === 'finish' ? parentProps.activeColor : parentProps.inactiveColor
    };
  }, [index, parentProps.active, parentProps.activeColor, parentProps.inactiveColor]);
  var titleStyle = 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.cloneElement(activeIcon, {
          className: cls(bem('icon', 'active')),
          color: activeColor,
          style: {
            color: activeColor
          }
        });
      }
    }

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

    if (inactiveIcon) {
      return /*#__PURE__*/React.cloneElement(inactiveIcon, {
        className: cls(bem('icon')),
        color: activeColor,
        style: {
          color: activeColor
        }
      });
    }

    return /*#__PURE__*/React.createElement("i", {
      className: cls(bem('circle')),
      style: lineStyle
    });
  };

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

export default StepsItem;