'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || 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; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _getClassName2 = require('babel-plugin-react-css-modules/dist/browser/getClassName');

var _getClassName3 = _interopRequireDefault(_getClassName2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _styleModuleImportMap = {
  './steps.scss': {
    'primary': 'flive_steps_primary',
    'size-sm': 'flive_steps_size-sm',
    'size-md': 'flive_steps_size-md',
    'size-lg': 'flive_steps_size-lg'
  }
};

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

// eslint-disable-line no-unused-vars


function calculateSelection(children) {
  var index = 0;
  var selectedStep = 0;
  var lastEnabled = 0;
  var resultValue = null;
  var arrayChildren = _react.Children.toArray(children);
  arrayChildren.forEach(function (child) {
    var componentName = child.type.componentName,
        _child$props = child.props,
        selected = _child$props.selected,
        enabled = _child$props.enabled,
        value = _child$props.value;

    if (componentName === 'Step') {
      if (selected) {
        selectedStep = index;
        lastEnabled = index;
        resultValue = value;
      } else if (enabled) {
        if (!resultValue) resultValue = value;
        lastEnabled = index;
      }
      index += 1;
    }
  });
  return { selectedStep: selectedStep, lastEnabled: lastEnabled, value: resultValue };
}

var Steps = function (_PureComponent) {
  _inherits(Steps, _PureComponent);

  function Steps() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Steps);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Steps.__proto__ || Object.getPrototypeOf(Steps)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      selectedStep: 0,
      lastEnabled: 0,
      value: undefined,
      children: null
    }, _this.onStepClick = function (_ref2) {
      var selectedStep = _ref2.index,
          value = _ref2.value;
      var _this$props = _this.props,
          children = _this$props.children,
          onChange = _this$props.onChange,
          variant = _this$props.variant,
          size = _this$props.size,
          withBorder = _this$props.withBorder,
          withRightArrow = _this$props.withRightArrow,
          className = _this$props.className;
      var lastEnabled = _this.state.lastEnabled;

      var resultChildren = _this.buildChildren({
        children: children, selectedStep: selectedStep, lastEnabled: lastEnabled, variant: variant, size: size, withBorder: withBorder, withRightArrow: withRightArrow, className: className
      });
      _this.setState({
        selectedStep: selectedStep,
        children: resultChildren
      });
      if (typeof onChange === 'function') {
        onChange(value);
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Steps, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      var _props = this.props,
          children = _props.children,
          onChange = _props.onChange,
          variant = _props.variant,
          size = _props.size,
          withBorder = _props.withBorder,
          withRightArrow = _props.withRightArrow,
          className = _props.className;

      var _calculateSelection = calculateSelection(children),
          selectedStep = _calculateSelection.selectedStep,
          lastEnabled = _calculateSelection.lastEnabled,
          value = _calculateSelection.value;

      var resultChildren = this.buildChildren({
        children: children, selectedStep: selectedStep, lastEnabled: lastEnabled, variant: variant, size: size, withBorder: withBorder, withRightArrow: withRightArrow, className: className
      });
      this.setState({
        selectedStep: selectedStep,
        lastEnabled: lastEnabled,
        children: resultChildren
      });
      if (typeof onChange === 'function') {
        onChange(value);
      }
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var children = nextProps.children,
          onChange = nextProps.onChange,
          reCalculateSelection = nextProps.reCalculateSelection,
          variant = nextProps.variant,
          size = nextProps.size,
          withBorder = nextProps.withBorder,
          withRightArrow = nextProps.withRightArrow,
          className = nextProps.className;

      if (reCalculateSelection) {
        var _calculateSelection2 = calculateSelection(children),
            selectedStep = _calculateSelection2.selectedStep,
            lastEnabled = _calculateSelection2.lastEnabled,
            value = _calculateSelection2.value;

        if (typeof onChange === 'function') {
          onChange(value);
        }
        var resultChildren = this.buildChildren({
          children: children, selectedStep: selectedStep, lastEnabled: lastEnabled, variant: variant, size: size, withBorder: withBorder, withRightArrow: withRightArrow, className: className
        });
        this.setState({
          selectedStep: selectedStep,
          lastEnabled: lastEnabled,
          value: value,
          children: resultChildren
        });
      }
    }
  }, {
    key: 'buildChildren',
    value: function buildChildren(_ref3) {
      var _this2 = this;

      var children = _ref3.children,
          selectedStep = _ref3.selectedStep,
          lastEnabled = _ref3.lastEnabled,
          props = _objectWithoutProperties(_ref3, ['children', 'selectedStep', 'lastEnabled']);

      var childIndex = 0;
      return _react2.default.Children.map(children, function (child) {
        var componentName = child.type.componentName;

        if (componentName === 'Step') {
          var resultChild = _react2.default.cloneElement(child, _extends({
            onClick: function onClick(_ref4) {
              var index = _ref4.index,
                  value = _ref4.value;
              var onClick = child.props.onClick;

              _this2.onStepClick({ index: index, value: value });
              if (typeof onClick === 'function') {
                onClick({ index: index, value: value });
              }
            },
            enabled: lastEnabled >= childIndex,
            selected: selectedStep === childIndex,
            index: childIndex,
            lastEnabled: lastEnabled === childIndex
          }, props));
          childIndex += 1;
          return resultChild;
        }
        return child;
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var children = this.state.children;
      var _props2 = this.props,
          variant = _props2.variant,
          size = _props2.size;

      return _react2.default.createElement(
        'div',
        {
          className: (0, _getClassName3.default)((0, _classnames2.default)(variant, 'size-' + size), _styleModuleImportMap)
        },
        children
      );
    }
  }]);

  return Steps;
}(_react.PureComponent);

Steps.componentName = 'Steps';
Steps.propTypes = {
  children: _propTypes2.default.node,
  variant: _propTypes2.default.oneOf(['primary']),
  onChange: _propTypes2.default.func,
  size: _propTypes2.default.oneOf(['sm', 'md', 'lg']),
  /**
   * @ignore
   * if true on 'componentWillReceiveProps' children will be calculate
   */
  reCalculateSelection: _propTypes2.default.bool,
  withBorder: _propTypes2.default.bool,
  withRightArrow: _propTypes2.default.bool,
  className: _propTypes2.default.string
};
Steps.defaultProps = {
  children: null,
  variant: 'primary',
  onChange: null,
  size: 'lg',
  reCalculateSelection: true,
  withBorder: false,
  withRightArrow: false,
  className: undefined
};
exports.default = Steps;
module.exports = exports['default'];