import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
var _excluded = ["components", "creatable", "error", "inverse", "helpText", "size", "success", "warning", "forwardedRef"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { forwardRef, PureComponent } from 'react';
import ReactSelect from 'react-select';
import ReactCreatableSelect from 'react-select/creatable';
import PropTypes from 'prop-types';
import { IconCloseBadgedSmallFilled, IconChevronDownSmallOutline } from '@teamleader/ui-icons';
import Box, { omitBoxProps, pickBoxProps } from '../box';
import Icon from '../icon';
import ValidationText from '../validationText';
import { COLOR } from '../../constants';
import theme from './theme.css';
import cx from 'classnames';
import uiUtilities from '@teamleader/ui-utilities';
var minHeightBySizeMap = {
  tiny: 24,
  small: 30,
  medium: 36,
  large: 48
};

var DropdownIndicator = function DropdownIndicator(_ref) {
  var inverse = _ref.selectProps.inverse;
  return /*#__PURE__*/React.createElement(Icon, {
    alignItems: "center",
    className: theme['dropdown-indicator'],
    color: inverse ? 'teal' : 'neutral',
    display: "flex",
    justifyContent: "center",
    tint: inverse ? 'lightest' : 'darkest'
  }, /*#__PURE__*/React.createElement(IconChevronDownSmallOutline, null));
};

var ClearIndicator = function ClearIndicator(_ref2) {
  var innerProps = _ref2.innerProps,
      inverse = _ref2.selectProps.inverse;
  return /*#__PURE__*/React.createElement(Icon, _extends({
    color: inverse ? 'teal' : 'neutral',
    display: "flex",
    tint: inverse ? 'lightest' : 'darkest'
  }, innerProps), /*#__PURE__*/React.createElement(IconCloseBadgedSmallFilled, null));
};

export var selectOverlayNode = document.createElement('div');
selectOverlayNode.setAttribute('data-teamleader-ui', 'select-overlay');
var activeSelects = new Set();

var Select = /*#__PURE__*/function (_PureComponent) {
  _inherits(Select, _PureComponent);

  var _super = _createSuper(Select);

  function Select() {
    var _this;

    _classCallCheck(this, Select);

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

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "getClearIndicatorStyles", function (base) {
      var inverse = _this.props.inverse;
      return _objectSpread(_objectSpread({}, base), {}, {
        color: inverse ? COLOR.TEAL.LIGHTEST : COLOR.TEAL.DARK,
        '&:hover': {
          color: inverse ? COLOR.NEUTRAL.LIGHTEST : COLOR.TEAL.DARKEST
        },
        cursor: 'pointer',
        svg: {
          height: '14px',
          width: '14px'
        }
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getControlStyles", function (base, _ref3) {
      var isDisabled = _ref3.isDisabled,
          isFocused = _ref3.isFocused;
      var _this$props = _this.props,
          error = _this$props.error,
          inverse = _this$props.inverse,
          size = _this$props.size,
          success = _this$props.success,
          warning = _this$props.warning,
          width = _this$props.width;

      var commonStyles = _objectSpread(_objectSpread({}, base), {}, {
        minHeight: minHeightBySizeMap[size],
        width: width
      });

      if (inverse) {
        return _objectSpread(_objectSpread({}, commonStyles), {}, {
          backgroundColor: isDisabled ? COLOR.TEAL.DARK : COLOR.TEAL.NORMAL,
          '&:hover': {
            borderColor: !error && !warning && !success && COLOR.TEAL.LIGHT
          },
          borderColor: error ? COLOR.RUBY.LIGHT : warning ? COLOR.GOLD.LIGHT : success ? COLOR.MINT.LIGHT : isDisabled ? COLOR.TEAL.DARK : isFocused ? COLOR.TEAL.LIGHT : COLOR.TEAL.NORMAL,
          boxShadow: error ? "0 0 0 1px ".concat(COLOR.RUBY.LIGHT) : warning ? "0 0 0 1px ".concat(COLOR.GOLD.LIGHT) : success ? "0 0 0 1px ".concat(COLOR.MINT.LIGHT) : isFocused ? "0 0 0 1px ".concat(COLOR.TEAL.LIGHT) : 'none'
        });
      }

      return _objectSpread(_objectSpread({}, commonStyles), {}, {
        backgroundColor: isDisabled ? COLOR.NEUTRAL.NORMAL : COLOR.NEUTRAL.LIGHTEST,
        '&:hover': {
          borderColor: !error && !warning && !success && COLOR.NEUTRAL.DARKEST
        },
        borderColor: error ? COLOR.RUBY.DARK : warning ? COLOR.GOLD.DARK : success ? COLOR.MINT.DARK : isDisabled ? COLOR.NEUTRAL.NORMAL : isFocused ? COLOR.NEUTRAL.DARKEST : COLOR.NEUTRAL.DARK,
        boxShadow: error ? "0 0 0 1px ".concat(COLOR.RUBY.DARK) : warning ? "0 0 0 1px ".concat(COLOR.GOLD.DARK) : success ? "0 0 0 1px ".concat(COLOR.MINT.DARK) : isFocused ? "0 0 0 1px ".concat(COLOR.NEUTRAL.DARKEST) : 'none'
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getGroupStyles", function (base) {
      var inverse = _this.props.inverse;
      return _objectSpread(_objectSpread({}, base), {}, {
        borderBottomColor: inverse ? COLOR.TEAL.LIGHT : COLOR.NEUTRAL.NORMAL,
        borderBottomStyle: 'solid',
        borderBottomWidth: '1px',
        '&:last-child': {
          borderWidth: 0
        }
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getGroupHeadingStyles", function (base) {
      var inverse = _this.props.inverse;
      return _objectSpread(_objectSpread({}, base), {}, {
        color: inverse ? COLOR.NEUTRAL.LIGHTEST : COLOR.TEAL.DARKEST,
        fontSize: '12px',
        fontWeight: '700',
        letterSpacing: '0.6px'
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getInput", function (base) {
      return _objectSpread(_objectSpread({}, base), {}, {
        marginLeft: '2px',
        paddingBottom: 0,
        paddingTop: 0
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getMenuStyles", function (base) {
      var menuHorizontalOffset = _this.props.menuHorizontalOffset;
      return _objectSpread(_objectSpread({}, base), {}, {
        marginTop: 3,
        marginBottom: 3
      }, menuHorizontalOffset ? {
        right: menuHorizontalOffset,
        position: 'absolute'
      } : {});
    });

    _defineProperty(_assertThisInitialized(_this), "getMenuPortalStyles", function (base) {
      var _this$props2 = _this.props,
          inverse = _this$props2.inverse,
          menuWidth = _this$props2.menuWidth;
      return _objectSpread(_objectSpread(_objectSpread({}, base), menuWidth && {
        width: menuWidth
      }), {}, {
        backgroundColor: inverse ? COLOR.TEAL.NORMAL : COLOR.NEUTRAL.LIGHTEST,
        fontFamily: 'Inter',
        fontSize: '14px',
        fontWeight: 400,
        fontSmoothing: 'antialiased',
        MozOsxFontSmoothing: 'grayscale',
        WebkitFontSmoothing: 'antialiased',
        zIndex: 500
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getMultiValueStyles", function (base) {
      var inverse = _this.props.inverse;
      return _objectSpread(_objectSpread({}, base), {}, {
        borderColor: inverse ? COLOR.TEAL.DARK : COLOR.NEUTRAL.NORMAL,
        borderStyle: 'solid',
        borderWidth: '1px',
        borderRadius: '4px',
        margin: '1px'
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getMultiValueLabelStyles", function (base) {
      var _this$props3 = _this.props,
          inverse = _this$props3.inverse,
          size = _this$props3.size;
      return _objectSpread(_objectSpread({}, base), {}, {
        backgroundColor: inverse ? COLOR.TEAL.DARK : COLOR.NEUTRAL.LIGHT,
        borderRadius: 0,
        color: inverse ? COLOR.NEUTRAL.LIGHTEST : COLOR.TEAL.DARKEST,
        fontFamily: 'Inter',
        fontWeight: 500,
        fontSize: size === 'tiny' || size === 'small' ? '12px' : '14px',
        lineHeight: size === 'tiny' || size === 'small' ? '1' : '18px',
        padding: size === 'tiny' ? '3px' : size === 'large' ? '9px' : '6px'
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getMultiValueRemoveStyles", function (base) {
      var _this$props4 = _this.props,
          inverse = _this$props4.inverse,
          size = _this$props4.size;
      return _objectSpread(_objectSpread({}, base), {}, {
        backgroundColor: inverse ? COLOR.TEAL.DARK : COLOR.NEUTRAL.LIGHT,
        borderRadius: 0,
        color: inverse ? COLOR.NEUTRAL.LIGHTEST : COLOR.TEAL.DARKEST,
        '&:hover': {
          backgroundColor: inverse ? COLOR.TEAL.DARKEST : COLOR.NEUTRAL.NORMAL,
          color: inverse ? COLOR.NEUTRAL.LIGHTEST : COLOR.TEAL.DARKEST
        },
        paddingLeft: size === 'tiny' ? '3px' : '6px',
        paddingRight: size === 'tiny' ? '3px' : '6px',
        transition: 'background-color .35s cubic-bezier(.4, 0, .2, 1)'
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getOptionStyles", function (base, _ref4) {
      var isDisabled = _ref4.isDisabled,
          isFocused = _ref4.isFocused,
          isSelected = _ref4.isSelected;
      var truncateOptionText = _this.props.truncateOptionText;

      var commonStyles = _objectSpread(_objectSpread(_objectSpread({}, base), truncateOptionText ? {
        overflow: 'hidden',
        textOverflow: 'ellipsis'
      } : {
        wordBreak: 'break-word'
      }), {}, {
        padding: '8px 12px'
      });

      if (_this.props.inverse) {
        return _objectSpread(_objectSpread({}, commonStyles), {}, {
          color: isDisabled ? COLOR.TEAL.LIGHT : isSelected ? COLOR.NEUTRAL.LIGHTEST : isFocused ? COLOR.TEAL.DARK : COLOR.NEUTRAL.LIGHTEST,
          backgroundColor: isSelected ? COLOR.TEAL.DARK : isFocused ? COLOR.TEAL.LIGHT : COLOR.TEAL.NORMAL,
          '&:active': {
            backgroundColor: isDisabled ? COLOR.TEAL.NORMAL : COLOR.TEAL.DARK,
            color: isDisabled ? COLOR.TEAL.LIGHT : COLOR.NEUTRAL.LIGHTEST
          }
        });
      }

      return _objectSpread(_objectSpread({}, commonStyles), {}, {
        color: isDisabled ? COLOR.NEUTRAL.DARK : COLOR.TEAL.DARK,
        backgroundColor: isSelected ? COLOR.AQUA.LIGHTEST : isFocused ? COLOR.NEUTRAL.LIGHT : COLOR.NEUTRAL.LIGHTEST,
        '&:active': {
          backgroundColor: isDisabled ? COLOR.NEUTRAL.LIGHTEST : COLOR.AQUA.LIGHTEST,
          color: isDisabled ? COLOR.NEUTRAL.DARK : COLOR.TEAL.DARK
        }
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getPlaceholderStyles", function (base, _ref5) {
      var isDisabled = _ref5.isDisabled;
      var inverse = _this.props.inverse;

      var commonStyles = _objectSpread(_objectSpread({}, base), {}, {
        marginLeft: '2px',
        marginRight: '2px',
        whiteSpace: 'nowrap'
      });

      if (inverse) {
        return _objectSpread(_objectSpread({}, commonStyles), {}, {
          color: isDisabled ? COLOR.TEAL.NORMAL : COLOR.TEAL.LIGHT
        });
      }

      return _objectSpread(_objectSpread({}, commonStyles), {}, {
        color: COLOR.NEUTRAL.DARKEST
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getSingleValueStyles", function (base) {
      return _objectSpread(_objectSpread({}, base), {}, {
        color: _this.props.inverse ? COLOR.NEUTRAL.LIGHTEST : COLOR.TEAL.DARKEST
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getValueContainerStyles", function (base, _ref6) {
      var isMulti = _ref6.isMulti,
          hasValue = _ref6.hasValue;
      var size = _this.props.size;
      return _objectSpread(_objectSpread({}, base), {}, {
        minHeight: minHeightBySizeMap[size] - 2,
        lineHeight: '18px',
        padding: isMulti && hasValue && size !== 'large' ? 0 : '0 4px'
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getStyles", function () {
      return {
        clearIndicator: _this.getClearIndicatorStyles,
        control: _this.getControlStyles,
        group: _this.getGroupStyles,
        groupHeading: _this.getGroupHeadingStyles,
        input: _this.getInput,
        menu: _this.getMenuStyles,
        menuPortal: _this.getMenuPortalStyles,
        multiValue: _this.getMultiValueStyles,
        multiValueLabel: _this.getMultiValueLabelStyles,
        multiValueRemove: _this.getMultiValueRemoveStyles,
        option: _this.getOptionStyles,
        placeholder: _this.getPlaceholderStyles,
        singleValue: _this.getSingleValueStyles,
        valueContainer: _this.getValueContainerStyles
      };
    });

    return _this;
  }

  _createClass(Select, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      activeSelects.add(this);
      var isOverlayMounted = document.contains(selectOverlayNode);

      if (!isOverlayMounted) {
        document.body.appendChild(selectOverlayNode);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var isLastSelect = activeSelects.size <= 1;

      if (isLastSelect) {
        document.body.removeChild(selectOverlayNode);
      }

      activeSelects.delete(this);
    }
  }, {
    key: "render",
    value: function render() {
      var _cx;

      var _this$props5 = this.props,
          components = _this$props5.components,
          creatable = _this$props5.creatable,
          error = _this$props5.error,
          inverse = _this$props5.inverse,
          helpText = _this$props5.helpText,
          size = _this$props5.size,
          success = _this$props5.success,
          warning = _this$props5.warning,
          forwardedRef = _this$props5.forwardedRef,
          otherProps = _objectWithoutProperties(_this$props5, _excluded);

      var boxProps = pickBoxProps(otherProps);
      var restProps = omitBoxProps(otherProps);
      var wrapperClassnames = cx(theme["is-".concat(size)], (_cx = {}, _defineProperty(_cx, theme['has-error'], error), _defineProperty(_cx, theme['is-inverse'], inverse), _cx));
      var Element = creatable ? ReactCreatableSelect : ReactSelect;
      return /*#__PURE__*/React.createElement(Box, _extends({
        className: wrapperClassnames
      }, boxProps), /*#__PURE__*/React.createElement(Element, _extends({
        ref: forwardedRef,
        className: cx(uiUtilities['reset-font-smoothing'], theme['select']),
        components: _objectSpread({
          ClearIndicator: ClearIndicator,
          DropdownIndicator: DropdownIndicator,
          IndicatorSeparator: null
        }, components),
        hideSelectedOptions: false,
        menuPlacement: "auto",
        menuPortalTarget: selectOverlayNode,
        menuShouldBlockScroll: true,
        styles: this.getStyles(),
        size: size
      }, restProps)), /*#__PURE__*/React.createElement(ValidationText, {
        error: error,
        help: helpText,
        inverse: inverse,
        success: success,
        warning: warning
      }));
    }
  }]);

  return Select;
}(PureComponent);

Select.defaultProps = {
  creatable: false,
  inverse: false,
  size: 'medium',
  width: '100%'
};
/** @type {React.ComponentType<any>} */

var ForwardedSelect = /*#__PURE__*/forwardRef(function (props, ref) {
  return /*#__PURE__*/React.createElement(Select, _extends({}, props, {
    forwardedRef: ref
  }));
});
ForwardedSelect.displayName = 'Select';
export default ForwardedSelect;