'use strict';

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

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _recompose = require('recompose');

var _TabList = require('./styles/TabList');

var _TabList2 = _interopRequireDefault(_TabList);

var _TabContainer = require('./styles/TabContainer');

var _TabContainer2 = _interopRequireDefault(_TabContainer);

var _TabContent = require('./styles/TabContent');

var _TabContent2 = _interopRequireDefault(_TabContent);

var _Tab = require('./styles/Tab');

var _Tab2 = _interopRequireDefault(_Tab);

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

var Tabs = function (_React$Component) {
  (0, _inherits3.default)(Tabs, _React$Component);

  function Tabs() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, Tabs);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Tabs.__proto__ || (0, _getPrototypeOf2.default)(Tabs)).call.apply(_ref, [this].concat(args))), _this), _this.createTabClickHandler = function (tab, index) {
      return _this.props.onTabSelected ? function (ev) {
        if (tab.props.onClick) {
          tab.props.onClick(ev);
        }
        _this.props.onTabSelected(index);
      } : tab.props.onClick;
    }, _this.renderTabs = function () {
      return _react2.default.Children.map(_this.props.children, function (child, index) {
        if (!_react2.default.isValidElement(child)) {
          return child;
        }

        // rendering Tab
        return _react2.default.cloneElement(child, {
          onClick: child.props.disabled || child.props.active ? null : _this.createTabClickHandler(child, index),
          active: index === _this.props.selectedTabIndex
        });
      });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  /**
   * Creates a click handler for a tab which preserves its own
   * onClick usage, and also hooks in so that this component can
   * appropriately call its onTabSelected callback with the right
   * index.
   */


  (0, _createClass3.default)(Tabs, [{
    key: 'render',
    value: function render() {
      var List = this.props.List;


      return _react2.default.createElement(
        List,
        null,
        this.renderTabs()
      );
    }
  }]);
  return Tabs;
}(_react2.default.Component);

Tabs.propTypes = {
  /**
   * Tab elements within container
   */
  children: _propTypes2.default.node.isRequired,
  /**
   * Called when user selects the tab
   */
  onTabSelected: _propTypes2.default.func,
  /**
   * The currently selected tab
   */
  selectedTabIndex: _propTypes2.default.number,
  /**
   * Override the component used to render the Tab list
   */
  List: _propTypes2.default.func
};
Tabs.defaultProps = {
  onTabSelected: null,
  List: _TabList2.default
};


Tabs.Vertical = (0, _recompose.withProps)({
  List: _TabList2.default.Vertical
})(Tabs);

Tabs.Tab = _Tab2.default;
Tabs.Container = _TabContainer2.default;
Tabs.Content = _TabContent2.default;

exports.default = Tabs;