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

/*
Copyright (c) Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import { styled, withStyle } from '../styles';
import { TYPE, SIZE } from './constants';
import { StyledList, StyledListItem } from '../menu';
import { Spinner } from '../spinner';
import { ellipsisText } from '../styles/util';

function getFont() {
  var _SIZE$mini$SIZE$compa;

  var size = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : SIZE.default;
  var typography = arguments.length > 1 ? arguments[1] : undefined;
  return (_SIZE$mini$SIZE$compa = {}, _defineProperty(_SIZE$mini$SIZE$compa, SIZE.mini, typography.font100), _defineProperty(_SIZE$mini$SIZE$compa, SIZE.compact, typography.font200), _defineProperty(_SIZE$mini$SIZE$compa, SIZE.default, typography.font300), _defineProperty(_SIZE$mini$SIZE$compa, SIZE.large, typography.font400), _SIZE$mini$SIZE$compa)[size];
}

function getControlPadding(props) {
  var _SIZE$mini, _SIZE$compact, _SIZE$default, _SIZE$large, _SIZE$mini$SIZE$compa2;

  var $theme = props.$theme,
      sizing = props.$theme.sizing,
      _props$$size = props.$size,
      $size = _props$$size === void 0 ? SIZE.default : _props$$size,
      $type = props.$type,
      $multi = props.$multi,
      $isEmpty = props.$isEmpty;
  var isSearch = $type === TYPE.search;
  var paddingLeft = isSearch ? "calc(".concat(sizing.scale1000, " - ").concat(sizing.scale0, ")") : sizing.scale400;
  var paddingStartDir = $theme.direction === 'rtl' ? 'paddingRight' : 'paddingLeft';
  var paddingEndDir = $theme.direction === 'rtl' ? 'paddingLeft' : 'paddingRight';
  return (_SIZE$mini$SIZE$compa2 = {}, _defineProperty(_SIZE$mini$SIZE$compa2, SIZE.mini, (_SIZE$mini = {
    // `sizing.scale0` based on the multi value component (Tag) top and bottom margin
    paddingTop: $multi && !$isEmpty ? 0 : sizing.scale100,
    paddingBottom: $multi && !$isEmpty ? 0 : sizing.scale100
  }, _defineProperty(_SIZE$mini, paddingStartDir, $multi && !$isEmpty ? "calc(".concat(paddingLeft, " - ").concat(sizing.scale0, ")") : paddingLeft), _defineProperty(_SIZE$mini, paddingEndDir, '0'), _SIZE$mini)), _defineProperty(_SIZE$mini$SIZE$compa2, SIZE.compact, (_SIZE$compact = {
    // `sizing.scale0` based on the multi value component (Tag) top and bottom margin
    paddingTop: $multi && !$isEmpty ? "calc(".concat(sizing.scale100, " - ").concat(sizing.scale0, ")") : sizing.scale200,
    paddingBottom: $multi && !$isEmpty ? "calc(".concat(sizing.scale100, " - ").concat(sizing.scale0, ")") : sizing.scale200
  }, _defineProperty(_SIZE$compact, paddingStartDir, $multi && !$isEmpty ? "calc(".concat(paddingLeft, " - ").concat(sizing.scale0, ")") : paddingLeft), _defineProperty(_SIZE$compact, paddingEndDir, '0'), _SIZE$compact)), _defineProperty(_SIZE$mini$SIZE$compa2, SIZE.default, (_SIZE$default = {
    // `sizing.scale0` based on the multi value component (Tag) top and bottom margin
    paddingTop: $multi && !$isEmpty ? "calc(".concat(sizing.scale400, " - ").concat(sizing.scale0, ")") : sizing.scale400,
    paddingBottom: $multi && !$isEmpty ? "calc(".concat(sizing.scale400, " - ").concat(sizing.scale0, ")") : sizing.scale400
  }, _defineProperty(_SIZE$default, paddingStartDir, $multi && !$isEmpty ? "calc(".concat(paddingLeft, " + ").concat(sizing.scale0, ")") : paddingLeft), _defineProperty(_SIZE$default, paddingEndDir, 0), _SIZE$default)), _defineProperty(_SIZE$mini$SIZE$compa2, SIZE.large, (_SIZE$large = {
    // `sizing.scale0` based on the multi value component (Tag) top and bottom margin
    paddingTop: $multi && !$isEmpty ? "calc(".concat(sizing.scale600, " - ").concat(sizing.scale0, ")") : sizing.scale550,
    paddingBottom: $multi && !$isEmpty ? "calc(".concat(sizing.scale600, " - ").concat(sizing.scale0, ")") : sizing.scale550
  }, _defineProperty(_SIZE$large, paddingStartDir, $multi && !$isEmpty ? "calc(".concat(paddingLeft, " - ").concat(sizing.scale0, ")") : paddingLeft), _defineProperty(_SIZE$large, paddingEndDir, 0), _SIZE$large)), _SIZE$mini$SIZE$compa2)[$size];
}

export var StyledDropdownContainer = styled('div', function (props) {
  return {
    width: "".concat(String(props.$width), "px")
  };
});
StyledDropdownContainer.displayName = "StyledDropdownContainer";
export var StyledDropdown = StyledList;
export var StyledDropdownListItem = StyledListItem;
export var StyledOptionContent = styled('div', function (props) {
  var $isHighlighted = props.$isHighlighted,
      $selected = props.$selected,
      $disabled = props.$disabled,
      $theme = props.$theme;
  return {
    cursor: $disabled ? 'not-allowed' : 'pointer',
    color: $selected && !$isHighlighted ? $theme.colors.menuFontSelected : null,
    fontWeight: $selected ? 'bold' : 'normal'
  };
});
StyledOptionContent.displayName = "StyledOptionContent";
export var StyledRoot = styled('div', function (props) {
  var typography = props.$theme.typography,
      $size = props.$size;
  return _objectSpread(_objectSpread({}, getFont($size, typography)), {}, {
    boxSizing: 'border-box',
    position: 'relative',
    width: '100%'
  });
});
StyledRoot.displayName = "StyledRoot";

function getControlContainerColors($disabled, $isFocused, $isPseudoFocused, $positive, $error, colors) {
  if ($disabled) {
    return {
      color: colors.inputTextDisabled,
      borderLeftColor: colors.inputFillDisabled,
      borderRightColor: colors.inputFillDisabled,
      borderTopColor: colors.inputFillDisabled,
      borderBottomColor: colors.inputFillDisabled,
      backgroundColor: colors.inputFillDisabled
    };
  }

  if ($isFocused || $isPseudoFocused) {
    return {
      color: colors.contentPrimary,
      borderLeftColor: colors.borderSelected,
      borderRightColor: colors.borderSelected,
      borderTopColor: colors.borderSelected,
      borderBottomColor: colors.borderSelected,
      backgroundColor: colors.inputFillActive
    };
  }

  if ($error) {
    return {
      color: colors.contentPrimary,
      borderLeftColor: colors.inputBorderError,
      borderRightColor: colors.inputBorderError,
      borderTopColor: colors.inputBorderError,
      borderBottomColor: colors.inputBorderError,
      backgroundColor: colors.inputFillError
    };
  }

  if ($positive) {
    return {
      color: colors.contentPrimary,
      borderLeftColor: colors.inputBorderPositive,
      borderRightColor: colors.inputBorderPositive,
      borderTopColor: colors.inputBorderPositive,
      borderBottomColor: colors.inputBorderPositive,
      backgroundColor: colors.inputFillPositive
    };
  }

  return {
    color: colors.contentPrimary,
    borderLeftColor: colors.inputBorder,
    borderRightColor: colors.inputBorder,
    borderTopColor: colors.inputBorder,
    borderBottomColor: colors.inputBorder,
    backgroundColor: colors.inputFill
  };
}

export var StyledControlContainer = styled('div', function (props) {
  var $disabled = props.$disabled,
      $error = props.$error,
      $positive = props.$positive,
      $isFocused = props.$isFocused,
      $isPseudoFocused = props.$isPseudoFocused,
      $type = props.$type,
      $searchable = props.$searchable,
      _props$$theme = props.$theme,
      borders = _props$$theme.borders,
      colors = _props$$theme.colors,
      animation = _props$$theme.animation;
  return _objectSpread({
    borderTopLeftRadius: borders.inputBorderRadius,
    borderTopRightRadius: borders.inputBorderRadius,
    borderBottomRightRadius: borders.inputBorderRadius,
    borderBottomLeftRadius: borders.inputBorderRadius,
    boxSizing: 'border-box',
    overflow: 'hidden',
    width: '100%',
    display: 'flex',
    justifyContent: 'space-between',
    cursor: $disabled ? 'not-allowed' : $searchable || $type === TYPE.search ? 'text' : 'pointer',
    borderLeftWidth: '2px',
    borderRightWidth: '2px',
    borderTopWidth: '2px',
    borderBottomWidth: '2px',
    borderLeftStyle: 'solid',
    borderRightStyle: 'solid',
    borderTopStyle: 'solid',
    borderBottomStyle: 'solid',
    transitionProperty: 'border, box-shadow, background-color',
    transitionDuration: animation.timing200,
    transitionTimingFunction: animation.easeOutCurve
  }, getControlContainerColors($disabled, $isFocused, $isPseudoFocused, $positive, $error, colors));
});
StyledControlContainer.displayName = "StyledControlContainer";
export var StyledValueContainer = styled('div', function (props) {
  var padding = getControlPadding(props);
  return _objectSpread({
    boxSizing: 'border-box',
    position: 'relative',
    flexGrow: 1,
    flexShrink: 1,
    flexBasis: '0%',
    display: 'flex',
    alignItems: 'center',
    flexWrap: props.$multi ? 'wrap' : 'nowrap',
    overflow: 'hidden'
  }, padding);
});
StyledValueContainer.displayName = "StyledValueContainer";
export var StyledPlaceholder = styled('div', function (props) {
  var $disabled = props.$disabled,
      colors = props.$theme.colors;
  return {
    color: $disabled ? colors.inputPlaceholderDisabled : colors.inputPlaceholder,
    maxWidth: '100%',
    overflow: 'hidden',
    textOverflow: 'ellipsis',
    whiteSpace: 'nowrap'
  };
});
StyledPlaceholder.displayName = "StyledPlaceholder";
export var StyledSingleValue = styled('div', function (props) {
  var _objectSpread2;

  var $searchable = props.$searchable,
      $size = props.$size,
      $theme = props.$theme,
      typography = props.$theme.typography;
  var font = getFont($size, typography);
  var marginDir = $theme.direction === 'rtl' ? 'marginRight' : 'marginLeft';
  return _objectSpread((_objectSpread2 = {
    lineHeight: !$searchable ? font.lineHeight : 'inherit',
    boxSizing: 'border-box'
  }, _defineProperty(_objectSpread2, marginDir, $theme.sizing.scale0), _defineProperty(_objectSpread2, "height", '100%'), _defineProperty(_objectSpread2, "maxWidth", '100%'), _objectSpread2), ellipsisText);
});
StyledSingleValue.displayName = "StyledSingleValue";
export var StyledInputContainer = styled('div', function (props) {
  var $size = props.$size,
      $searchable = props.$searchable,
      _props$$theme2 = props.$theme,
      typography = _props$$theme2.typography,
      sizing = _props$$theme2.sizing,
      $isEmpty = props.$isEmpty;
  var font = getFont($size, typography);
  return {
    position: 'relative',
    display: 'inline-block',
    maxWidth: '100%',
    backgroundColor: 'transparent',
    borderLeftStyle: 'none',
    borderTopStyle: 'none',
    borderRightStyle: 'none',
    borderBottomStyle: 'none',
    boxShadow: 'none',
    boxSizing: 'border-box',
    outline: 'none',
    marginTop: 0,
    marginBottom: 0,
    marginLeft: $isEmpty ? 0 : sizing.scale0,
    marginRight: 0,
    paddingTop: 0,
    paddingBottom: 0,
    paddingLeft: 0,
    paddingRight: 0,
    height: String(!$searchable ? font.lineHeight : 'auto'),
    maxHeight: String(font.lineHeight)
  };
});
StyledInputContainer.displayName = "StyledInputContainer";
export var StyledInput = styled('input', function (props) {
  var _props$$theme3 = props.$theme,
      colors = _props$$theme3.colors,
      typography = _props$$theme3.typography,
      $size = props.$size,
      $searchable = props.$searchable,
      $width = props.$width;
  return _objectSpread(_objectSpread({}, getFont($size, typography)), {}, {
    color: colors.contentPrimary,
    boxSizing: 'content-box',
    width: !$searchable ? '1px' : $width || '100%',
    maxWidth: '100%',
    background: 'transparent',
    borderLeftStyle: 'none',
    borderTopStyle: 'none',
    borderRightStyle: 'none',
    borderBottomStyle: 'none',
    boxShadow: 'none',
    display: 'inline-block',
    outline: 'none',
    marginTop: '0',
    marginBottom: '0',
    marginLeft: '0',
    marginRight: '0',
    paddingTop: '0',
    paddingBottom: '0',
    paddingLeft: '0',
    paddingRight: '0'
  });
});
StyledInput.displayName = "StyledInput";
export var StyledInputSizer = styled('div', function (_ref) {
  var _objectSpread3;

  var $size = _ref.$size,
      $theme = _ref.$theme,
      typography = _ref.$theme.typography;
  var dir = $theme.direction === 'rtl' ? 'right' : 'left';
  return _objectSpread(_objectSpread({}, getFont($size, typography)), {}, (_objectSpread3 = {
    position: 'absolute',
    top: 0
  }, _defineProperty(_objectSpread3, dir, 0), _defineProperty(_objectSpread3, "visibility", 'hidden'), _defineProperty(_objectSpread3, "height", 0), _defineProperty(_objectSpread3, "overflow", 'scroll'), _defineProperty(_objectSpread3, "whiteSpace", 'pre'), _objectSpread3));
});
StyledInputSizer.displayName = "StyledInputSizer";
export var StyledIconsContainer = styled('div', function (_ref2) {
  var $theme = _ref2.$theme,
      sizing = _ref2.$theme.sizing;
  var paddingDir = $theme.direction === 'rtl' ? 'paddingLeft' : 'paddingRight';
  return _defineProperty({
    boxSizing: 'border-box',
    position: 'relative',
    display: 'flex',
    flexShrink: 0,
    alignItems: 'center',
    alignSelf: 'stretch'
  }, paddingDir, sizing.scale500);
});
StyledIconsContainer.displayName = "StyledIconsContainer";

function getSvgStyles(_ref4) {
  var $theme = _ref4.$theme;
  return {
    display: 'inline-block',
    fill: 'currentColor',
    color: 'currentColor',
    height: $theme.sizing.scale600,
    width: $theme.sizing.scale600
  };
}

export var StyledSelectArrow = styled('svg', function (props) {
  var _sizes;

  var $theme = props.$theme,
      $disabled = props.$disabled,
      $size = props.$size;
  var colors = $theme.colors;
  var sizes = (_sizes = {}, _defineProperty(_sizes, SIZE.mini, 16), _defineProperty(_sizes, SIZE.compact, 16), _defineProperty(_sizes, SIZE.default, 20), _defineProperty(_sizes, SIZE.large, 24), _sizes);
  var size = sizes[SIZE.default];

  if ($size) {
    size = sizes[$size];
  }

  return _objectSpread(_objectSpread({}, getSvgStyles({
    $theme: $theme
  })), {}, {
    color: $disabled ? colors.inputTextDisabled : colors.contentPrimary,
    cursor: $disabled ? 'not-allowed' : 'pointer',
    height: "".concat(size, "px"),
    width: "".concat(size, "px")
  });
});
StyledSelectArrow.displayName = "StyledSelectArrow";
export var StyledClearIcon = styled('svg', function (props) {
  var _sizes2;

  var $theme = props.$theme,
      $size = props.$size;
  var colors = $theme.colors;
  var sizes = (_sizes2 = {}, _defineProperty(_sizes2, SIZE.mini, 15), _defineProperty(_sizes2, SIZE.compact, 15), _defineProperty(_sizes2, SIZE.default, 18), _defineProperty(_sizes2, SIZE.large, 22), _sizes2);
  var size = sizes[SIZE.default];

  if ($size) {
    size = sizes[$size];
  }

  return _objectSpread(_objectSpread({}, getSvgStyles({
    $theme: $theme
  })), {}, {
    color: colors.contentPrimary,
    cursor: 'pointer',
    height: "".concat(size, "px"),
    width: "".concat(size, "px")
  });
});
StyledClearIcon.displayName = "StyledClearIcon";
export var StyledLoadingIndicator = withStyle(Spinner, function (_ref5) {
  var $theme = _ref5.$theme;
  return {
    borderTopWidth: '2px',
    borderRightWidth: '2px',
    borderBottomWidth: '2px',
    borderLeftWidth: '2px',
    borderRightColor: $theme.colors.borderOpaque,
    borderBottomColor: $theme.colors.borderOpaque,
    borderLeftColor: $theme.colors.borderOpaque,
    width: '16px',
    height: '16px'
  };
});
StyledLoadingIndicator.displayName = "StyledLoadingIndicator";
export var StyledSearchIconContainer = styled('div', function (props) {
  var _objectSpread4;

  var $disabled = props.$disabled,
      $theme = props.$theme;
  var colors = $theme.colors,
      sizing = $theme.sizing;
  var dir = $theme.direction === 'rtl' ? 'right' : 'left';
  return _objectSpread(_objectSpread({}, getSvgStyles(props)), {}, (_objectSpread4 = {
    color: $disabled ? colors.inputTextDisabled : colors.contentPrimary,
    cursor: $disabled ? 'not-allowed' : 'pointer',
    position: 'absolute',
    top: 0
  }, _defineProperty(_objectSpread4, dir, sizing.scale500), _defineProperty(_objectSpread4, "display", 'flex'), _defineProperty(_objectSpread4, "alignItems", 'center'), _defineProperty(_objectSpread4, "height", '100%'), _objectSpread4));
});
StyledSearchIconContainer.displayName = "StyledSearchIconContainer";