var _excluded = ["innerProps"],
  _excluded2 = ["minWidth", "size"],
  _excluded3 = ["isCreatable"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React, { useRef, useMemo } from "react";
import styled from "styled-components";
import ReactSelect, { components as defaultComponents } from "react-select";
import Creatable from "react-select/creatable";
import { Icon } from "../icon";
import { useVirtualizer } from "@tanstack/react-virtual";
var useDataAttrs = function useDataAttrs(props, name) {
  var _props$selectProps = props.selectProps,
    dataGA = _props$selectProps["data-ga"],
    dataTestId = _props$selectProps["data-testid"];
  var ga = useMemo(function () {
    if (!dataGA) return dataGA;
    var gaParts = dataGA.split("::");
    if (!gaParts[1]) return dataGA;
    gaParts[1] = gaParts[1] + "-" + name;
    return gaParts.join("::");
  }, [dataGA]);
  var testId = "" + (dataTestId || "") + name;
  return {
    "data-ga": ga,
    "data-testid": testId
  };
};
var withDataAttrs = function withDataAttrs(Component, name) {
  return function (_ref) {
    var innerProps = _ref.innerProps,
      props = _objectWithoutPropertiesLoose(_ref, _excluded);
    var dataProps = useDataAttrs(props, name);
    var componentProps = innerProps ? _objectSpread(_objectSpread({}, props), {}, {
      innerProps: _objectSpread(_objectSpread({}, innerProps), dataProps)
    }) : _objectSpread(_objectSpread({}, props), dataProps);
    return /*#__PURE__*/React.createElement(Component, componentProps);
  };
};
var withDOMDataAttrs = function withDOMDataAttrs(Component, name) {
  return function (props) {
    var dataProps = useDataAttrs(props, name);
    return /*#__PURE__*/React.createElement(Component, _extends({}, props, dataProps));
  };
};
var OriginalOption = defaultComponents.Option;
var Option = function Option(props) {
  if (props.data.icon) {
    return /*#__PURE__*/React.createElement(OriginalOption, props, /*#__PURE__*/React.createElement(Icon, {
      name: props.data.icon,
      color: "textLite",
      margin: [0, 1, 0, 0]
    }), /*#__PURE__*/React.createElement("span", null, props.data.label));
  }
  return /*#__PURE__*/React.createElement(OriginalOption, props);
};
var customComponents = _objectSpread(_objectSpread({}, defaultComponents), {}, {
  ClearIndicator: withDataAttrs(defaultComponents.ClearIndicator, "ClearIndicator"),
  Control: withDataAttrs(defaultComponents.Control, "Control"),
  DropdownIndicator: withDataAttrs(defaultComponents.DropdownIndicator, "DropdownIndicator"),
  DownChevron: withDataAttrs(defaultComponents.DownChevron, "DownChevron"),
  CrossIcon: withDataAttrs(defaultComponents.CrossIcon, "CrossIcon"),
  Group: withDataAttrs(defaultComponents.Group, "Group"),
  GroupHeading: withDataAttrs(defaultComponents.GroupHeading, "GroupHeading"),
  IndicatorsContainer: withDataAttrs(defaultComponents.IndicatorsContainer, "IndicatorsContainer"),
  IndicatorSeparator: withDataAttrs(defaultComponents.IndicatorSeparator, "IndicatorSeparator"),
  Input: withDOMDataAttrs(defaultComponents.Input, "Input"),
  LoadingIndicator: withDataAttrs(defaultComponents.LoadingIndicator, "LoadingIndicator"),
  Menu: withDataAttrs(defaultComponents.Menu, "Menu"),
  MenuList: withDataAttrs(defaultComponents.MenuList, "MenuList"),
  MenuPortal: withDataAttrs(defaultComponents.MenuPortal, "MenuPortal"),
  LoadingMessage: withDataAttrs(defaultComponents.LoadingMessage, "LoadingMessage"),
  MultiValue: withDataAttrs(defaultComponents.MultiValue, "MultiValue"),
  MultiValueContainer: withDataAttrs(defaultComponents.MultiValueContainer, "MultiValueContainer"),
  MultiValueLabel: withDataAttrs(defaultComponents.MultiValueLabel, "MultiValueLabel"),
  MultiValueRemove: withDataAttrs(defaultComponents.MultiValueRemove, "MultiValueRemove"),
  NoOptionsMessage: withDataAttrs(defaultComponents.NoOptionsMessage, "NoOptionsMessage"),
  Option: withDataAttrs(Option, "Option"),
  Placeholder: withDataAttrs(defaultComponents.Placeholder, "Placeholder"),
  SelectContainer: withDataAttrs(defaultComponents.SelectContainer, "SelectContainer"),
  SingleValue: withDataAttrs(defaultComponents.SingleValue, "SingleValue"),
  ValueContainer: withDataAttrs(defaultComponents.ValueContainer, "ValueContainer")
});
var VirtualItem = function VirtualItem(_ref2) {
  var virtualRow = _ref2.virtualRow,
    child = _ref2.child;
  return /*#__PURE__*/React.createElement("div", {
    key: virtualRow.key,
    style: {
      transform: "translateY(" + virtualRow.start + "px)",
      top: 0,
      left: 0,
      right: 0,
      position: "absolute"
    },
    "data-index": virtualRow.index
  }, child);
};
var VirtualList = function VirtualList(_ref3) {
  var children = _ref3.children,
    parentRef = _ref3.parentRef;
  var virtualizer = useVirtualizer({
    count: children.length,
    getScrollElement: function getScrollElement() {
      return parentRef.current;
    },
    estimateSize: function estimateSize() {
      return 35;
    },
    overscan: 5
  });
  var virtualItems = virtualizer.getVirtualItems();
  return /*#__PURE__*/React.createElement("div", {
    style: {
      height: virtualizer.getTotalSize() + "px",
      position: "relative"
    }
  }, virtualItems.map(function (virtualRow) {
    return /*#__PURE__*/React.createElement(VirtualItem, {
      key: virtualRow.key,
      virtualRow: virtualRow,
      child: children[virtualRow.index]
    });
  }));
};
var VirtualizedMenuList = function VirtualizedMenuList(props) {
  var parentRef = useRef();
  return /*#__PURE__*/React.createElement(customComponents.MenuList, _extends({}, props, {
    innerRef: parentRef,
    styles: {
      height: "300px",
      overflow: "auto",
      position: "relative"
    }
  }), /*#__PURE__*/React.createElement(VirtualList, {
    parentRef: parentRef
  }, props.children));
};
var makeCustomTheme = function makeCustomTheme(theme) {
  return function (selectTheme) {
    return _objectSpread(_objectSpread({}, selectTheme), {}, {
      borderRadius: 4,
      colors: _objectSpread(_objectSpread({}, selectTheme.colors), {}, {
        primary: theme.colors.border,
        primary25: theme.colors.selected,
        primary50: theme.colors.border,
        primary75: theme.colors.tooltip,
        danger: theme.colors.text,
        dangerLight: theme.colors.border,
        neutral0: theme.colors.inputBg,
        neutral5: theme.colors.mainBackgroundDisabled,
        neutral30: theme.colors.controlFocused,
        neutral60: theme.colors.placeholder,
        neutral80: theme.colors.text,
        neutral10: theme.colors.border,
        neutral20: theme.colors.placeholder
      })
    });
  };
};
var getOptionColor = function getOptionColor(theme, state) {
  if (state.isDisabled) return theme.colors.placeholder;
  return theme.colors.text;
};
var getOptionBackground = function getOptionBackground(theme, state) {
  if (state.isSelected) return theme.colors.menuItemSelected;
  return null;
};
var makeCustomStyles = function makeCustomStyles(theme, _ref4) {
  if (_ref4 === void 0) {
    _ref4 = {};
  }
  var _ref5 = _ref4,
    minWidth = _ref5.minWidth,
    size = _ref5.size,
    providedStyles = _objectWithoutPropertiesLoose(_ref5, _excluded2);
  return _objectSpread(_objectSpread({
    control: function control(styles, state) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        borderColor: state.isFocused ? theme.colors.inputBorderFocus : theme.colors.inputBorder,
        boxShadow: "none",
        minHeight: 18,
        minWidth: minWidth || 160,
        ":hover": {
          borderColor: theme.colors.inputBorderHover
        }
      });
    },
    input: function input(styles, state) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        color: state.isDisabled ? theme.colors.placeholder : theme.colors.grey140
      }, size === "tiny" ? {
        lineHeight: "18px",
        paddingBottom: 0,
        paddingTop: 0
      } : {});
    },
    menu: function menu(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        zIndex: 100
      });
    },
    menuPortal: function menuPortal(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        zIndex: 9999
      });
    },
    multiValue: function multiValue(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        fontSize: size === "tiny" ? "12px" : "14px",
        flexDirection: "row-reverse"
      }, size === "tiny" ? {
        minHeight: 18
      } : {});
    },
    multiValueLabel: function multiValueLabel(styles, state) {
      return _objectSpread(_objectSpread(_objectSpread({}, styles), {}, {
        backgroundColor: theme.colors.grey40,
        borderRadius: "0 2px 2px 0",
        color: state.isDisabled ? theme.colors.placeholder : theme.colors.grey140
      }, size === "tiny" ? {
        padding: "1px"
      } : {}), {}, {
        paddingRight: state.data.isDisabled ? "8px" : ""
      });
    },
    multiValueRemove: function multiValueRemove(styles, state) {
      return _objectSpread({
        color: state.isDisabled ? theme.colors.placeholder : theme.colors.grey140
      }, state.data.isDisabled ? _objectSpread(_objectSpread({}, styles), {}, {
        display: "none"
      }) : _objectSpread(_objectSpread({}, styles), {}, {
        borderRadius: "2px 0 0 2px",
        background: theme.colors.grey40,
        ":hover": {
          background: theme.colors.tabsBorder
        }
      }));
    },
    option: function option(styles, state) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        display: "flex",
        alignItems: "center",
        color: getOptionColor(theme, state),
        backgroundColor: getOptionBackground(theme, state),
        ":hover": {
          backgroundColor: theme.colors.secondaryHighlight,
          color: theme.colors.text
        }
      }, size === "tiny" ? {
        fontSize: "12px",
        minHeight: 28,
        padding: "4px 8px"
      } : {});
    },
    placeholder: function placeholder(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        color: theme.colors.placeholder
      }, size === "tiny" ? {
        fontSize: "12px",
        lineHeight: "18px"
      } : {});
    },
    singleValue: function singleValue(styles, state) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        color: state.isDisabled ? theme.colors.placeholder : theme.colors.grey140,
        fontSize: size === "tiny" ? "12px" : "14px"
      });
    }
  }, size === "tiny" ? {
    dropdownIndicator: function dropdownIndicator(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        padding: "3px"
      });
    },
    clearIndicator: function clearIndicator(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        padding: "3px"
      });
    },
    indicatorsContainer: function indicatorsContainer(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        minHeight: 18
      });
    },
    valueContainer: function valueContainer(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        minHeight: 18,
        padding: "1px 6px"
      });
    }
  } : {
    dropdownIndicator: function dropdownIndicator(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        padding: "3px"
      });
    },
    clearIndicator: function clearIndicator(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        padding: "3px"
      });
    },
    indicatorsContainer: function indicatorsContainer(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        minHeight: 28
      });
    },
    valueContainer: function valueContainer(styles) {
      return _objectSpread(_objectSpread({}, styles), {}, {
        minHeight: 28,
        padding: "1px 6px"
      });
    }
  }), providedStyles);
};
var components = _objectSpread(_objectSpread({}, customComponents), {}, {
  MenuList: VirtualizedMenuList
});
var getAttrs = function getAttrs(props) {
  return _objectSpread(_objectSpread({}, props), {}, {
    components: props.components ? _objectSpread(_objectSpread({}, components), props.components) : components,
    theme: makeCustomTheme(props.theme),
    styles: makeCustomStyles(props.theme, props.styles)
  });
};
var SelectComponent = styled(ReactSelect).attrs(getAttrs).withConfig({
  displayName: "select__SelectComponent",
  componentId: "sc-4oqdin-0"
})([""]);
var CreatableComponent = styled(Creatable).attrs(getAttrs).withConfig({
  displayName: "select__CreatableComponent",
  componentId: "sc-4oqdin-1"
})([""]);
var Select = function Select(_ref6) {
  var isCreatable = _ref6.isCreatable,
    props = _objectWithoutPropertiesLoose(_ref6, _excluded3);
  if (isCreatable) return /*#__PURE__*/React.createElement(CreatableComponent, props);
  return /*#__PURE__*/React.createElement(SelectComponent, props);
};
export default Select;