(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(['exports', 'react', 'classnames'], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require('react'), require('classnames'));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.react, global.classnames);
    global.IronSelector = mod.exports;
  }
})(this, function (exports, _react, _classnames2) {
  'use strict';

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

  var _react2 = _interopRequireDefault(_react);

  var _classnames3 = _interopRequireDefault(_classnames2);

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

  function _defineProperty(obj, key, value) {
    if (key in obj) {
      Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
      });
    } else {
      obj[key] = value;
    }

    return obj;
  }

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

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

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

  var IronSelector = function (_React$Component) {
    _inherits(IronSelector, _React$Component);

    function IronSelector(props) {
      _classCallCheck(this, IronSelector);

      var _this = _possibleConstructorReturn(this, (IronSelector.__proto__ || Object.getPrototypeOf(IronSelector)).call(this, props));

      _this.displayName = 'IronSelector';
      _this.state = {
        selected: props.selected || 0,
        selectedItem: props.selectedItem || null,
        selectedClassName: props.selectedClassName || 'iron-selected',
        propForSelected: props.propForSelect || null
      };
      _this._items = [];
      _this._keys = [];
      return _this;
    }

    _createClass(IronSelector, [{
      key: '_renderChildren',
      value: function _renderChildren() {
        var selected = this.props.selected;
        var children = this.props.children;
        var child = children[selected];
        if (child) {
          return _react2.default.createElement(
            'div',
            null,
            child
          );
        }
      }
    }, {
      key: 'componentWillReceiveProps',
      value: function componentWillReceiveProps(nextProps) {}
    }, {
      key: 'componentDidUpdate',
      value: function componentDidUpdate() {
        if (this.props.onChange) {
          this.props.onChange(this.state);
        }
      }
    }, {
      key: '_renderTitles',
      value: function _renderTitles() {
        this._keys = [];
        function labels(child, index) {
          var propForSelect = index;
          this._items.push(child);
          if (this.props.propForSelect) {
            propForSelect = child.props[this.props.propForSelect];
            this._keys.push(propForSelect);
          } else {
            this._keys.push(index);
          }
          var selectedClassName = this.props.selectedClassName;
          var selected = this.props.selected === this._getIndexForValue(propForSelect);
          var baseClasses = (0, _classnames3.default)(_defineProperty({}, '' + selectedClassName, selected));
          return _react2.default.createElement(
            'li',
            { key: index, className: baseClasses },
            child
          );
        }
        var nodes = this.props.children.map(labels.bind(this));
        return nodes;
      }
    }, {
      key: '_getIndexForValue',
      value: function _getIndexForValue(val) {
        return this._keys.indexOf(val);
      }
    }, {
      key: 'render',
      value: function render() {
        var _props = this.props,
            children = _props.children,
            className = _props.className,
            selected = _props.selected,
            selectedItem = _props.selectedItem,
            _props$style = _props.style,
            style = _props$style === undefined ? {
          listStyleType: 'none',
          margin: 0,
          padding: 0
        } : _props$style;


        var baseClassnames = (0, _classnames3.default)('iron-selector', className);

        return _react2.default.createElement(
          'ul',
          { className: baseClassnames, style: style },
          this._renderTitles()
        );
      }
    }]);

    return IronSelector;
  }(_react2.default.Component);

  IronSelector.defaultProps = {
    selectedClassName: 'iron-selected',
    selectedStyle: null,
    selected: 0,
    selectedItem: null,
    propForSelect: null
  };
  exports.default = IronSelector;
});