'use strict';

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

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

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

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

var SelectElement = _styledComponents2.default.select.withConfig({
  displayName: 'Select__SelectElement'
})(['margin-bottom:0;min-width:100px;color:rgba(3,14,38,0.75);background-size:20px;background-repeat:no-repeat;background-position:95% center;background-image:url(\'/static/img/partial-arrow-down.png\');appearance:none;border:none;font-size:1rem;font-weight:500;border-radius:var(--smBorderRadius);height:2.5rem;line-height:2.5rem;text-align:center;cursor:pointer;outline:none;background-color:', ' padding:', ' &::after{}@media only screen and (max-width:48rem){width:', ';'], function (props) {
  return props.transparent ? 'transparent' : 'var(--porcelain)';
}, function (props) {
  return props.transparent ? '0' : '0 1rem';
}, function (props) {
  return props.transparent ? 'auto' : '100%';
});

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

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

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

    _this.state = { value: null };
    _this.onChange = _this.onChange.bind(_this);
    return _this;
  }

  _createClass(Select, [{
    key: 'onChange',
    value: function onChange(e) {
      var _this2 = this;

      if (e.target && this.props.onChange) {
        var value = e.target.value;
        this.setState({ value: value }, function () {
          _this2.props.onChange(value);
        });
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var options = null;
      if (this.props.values) {
        options = this.props.values.map(function (val) {
          return _react2.default.createElement(
            'option',
            { key: val, value: val },
            val
          );
        });
      } else if (this.props.items) {
        options = this.props.items.map(function (val) {
          return _react2.default.createElement(
            'option',
            { key: val.value, value: val.value },
            val.text
          );
        });
      }

      return _react2.default.createElement(
        SelectElement,
        { transparent: this.props.transparent,
          value: this.state.value || this.props.value,
          onChange: this.onChange.bind(this) },
        options
      );
    }
  }], [{
    key: 'getDerivedStateFromProps',
    value: function getDerivedStateFromProps(nextProps, prevState) {
      if (nextProps.value) {
        return {
          val: nextProps.value
        };
      }
      return null;
    }
  }]);

  return Select;
}(_react.PureComponent);

exports.default = Select;