import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var _excluded = ["valid", "invalid", "capitalized", "fullWidth", "value", "size", "color", "className", "options"];

var _ICON_SIZE_MAP;

import * as React from 'react';
import classnames from 'classnames';
import Icon, { ICON_COLOR } from '../icons/Icon';
export var COLOR = {
  DEAFAULT: 'default',
  WHITE: 'white'
};
export var SIZE = {
  S: 's',
  M: 'm',
  L: 'l'
};
var ICON_SIZE_MAP = (_ICON_SIZE_MAP = {}, _defineProperty(_ICON_SIZE_MAP, SIZE.L, 32), _defineProperty(_ICON_SIZE_MAP, SIZE.M, 24), _defineProperty(_ICON_SIZE_MAP, SIZE.S, 16), _ICON_SIZE_MAP);

var getOptionElement = function getOptionElement(_ref) {
  var value = _ref.value,
      text = _ref.text;
  return /*#__PURE__*/React.createElement("option", {
    key: value,
    value: value
  }, text);
};

var Select = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _classnames;

  var valid = props.valid,
      invalid = props.invalid,
      capitalized = props.capitalized,
      fullWidth = props.fullWidth,
      value = props.value,
      _props$size = props.size,
      size = _props$size === void 0 ? SIZE.M : _props$size,
      color = props.color,
      className = props.className,
      _props$options = props.options,
      options = _props$options === void 0 ? [] : _props$options,
      additionalProps = _objectWithoutProperties(props, _excluded);

  if (valid === true && invalid === true) {
    throw {
      name: 'WrongValidation',
      message: 'Select can be either valid or invalid!'
    };
  }

  var selectClass = classnames('sg-select', (_classnames = {
    'sg-select--selected': value,
    'sg-select--valid': valid,
    'sg-select--invalid': invalid,
    'sg-select--capitalized': capitalized,
    'sg-select--full-width': fullWidth
  }, _defineProperty(_classnames, "sg-select--".concat(String(color)), color), _defineProperty(_classnames, "sg-select--".concat(String(size)), size && size !== 'm'), _classnames), className);
  var optionsElements = options.map(function (item, index) {
    if ('options' in item) {
      return /*#__PURE__*/React.createElement("optgroup", {
        key: item.label + index,
        label: item.label
      }, item.options.map(getOptionElement));
    }

    if (item.text || item.value) {
      return getOptionElement(item);
    }

    return null;
  });
  return /*#__PURE__*/React.createElement("div", {
    className: selectClass
  }, /*#__PURE__*/React.createElement("div", {
    className: "sg-select__icon"
  }, /*#__PURE__*/React.createElement(Icon, {
    type: "caret_down",
    color: ICON_COLOR['icon-gray-50'],
    size: ICON_SIZE_MAP[size]
  })), /*#__PURE__*/React.createElement("select", _extends({}, additionalProps, {
    className: "sg-select__element",
    value: value,
    ref: ref
  }), optionsElements));
});
Select.displayName = 'Select';
export default Select;