"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _reactSelect = _interopRequireWildcard(require("react-select"));
var _creatable = _interopRequireDefault(require("react-select/creatable"));
var _icon = require("../icon");
var _reactVirtual = require("@tanstack/react-virtual");
var _excluded = ["innerProps"],
  _excluded2 = ["minWidth"],
  _excluded3 = ["isCreatable"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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 _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; }
var useDataAttrs = function useDataAttrs(props, name) {
  var _props$selectProps = props.selectProps,
    dataGA = _props$selectProps["data-ga"],
    dataTestId = _props$selectProps["data-testid"];
  var ga = (0, _react.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 ? _extends({}, props, {
      innerProps: _extends({}, innerProps, dataProps)
    }) : _extends({}, props, dataProps);
    return /*#__PURE__*/_react["default"].createElement(Component, componentProps);
  };
};
var withDOMDataAttrs = function withDOMDataAttrs(Component, name) {
  return function (props) {
    var dataProps = useDataAttrs(props, name);
    return /*#__PURE__*/_react["default"].createElement(Component, _extends({}, props, dataProps));
  };
};
var OriginalOption = _reactSelect.components.Option;
var Option = function Option(props) {
  if (props.data.icon) {
    return /*#__PURE__*/_react["default"].createElement(OriginalOption, props, /*#__PURE__*/_react["default"].createElement(_icon.Icon, {
      name: props.data.icon,
      color: "textLite",
      margin: [0, 1, 0, 0]
    }), /*#__PURE__*/_react["default"].createElement("span", null, props.data.label));
  }
  return /*#__PURE__*/_react["default"].createElement(OriginalOption, props);
};
var customComponents = _extends({}, _reactSelect.components, {
  ClearIndicator: withDataAttrs(_reactSelect.components.ClearIndicator, "ClearIndicator"),
  Control: withDataAttrs(_reactSelect.components.Control, "Control"),
  DropdownIndicator: withDataAttrs(_reactSelect.components.DropdownIndicator, "DropdownIndicator"),
  DownChevron: withDataAttrs(_reactSelect.components.DownChevron, "DownChevron"),
  CrossIcon: withDataAttrs(_reactSelect.components.CrossIcon, "CrossIcon"),
  Group: withDataAttrs(_reactSelect.components.Group, "Group"),
  GroupHeading: withDataAttrs(_reactSelect.components.GroupHeading, "GroupHeading"),
  IndicatorsContainer: withDataAttrs(_reactSelect.components.IndicatorsContainer, "IndicatorsContainer"),
  IndicatorSeparator: null,
  Input: withDOMDataAttrs(_reactSelect.components.Input, "Input"),
  LoadingIndicator: withDataAttrs(_reactSelect.components.LoadingIndicator, "LoadingIndicator"),
  Menu: withDataAttrs(_reactSelect.components.Menu, "Menu"),
  MenuList: withDataAttrs(_reactSelect.components.MenuList, "MenuList"),
  MenuPortal: withDataAttrs(_reactSelect.components.MenuPortal, "MenuPortal"),
  LoadingMessage: withDataAttrs(_reactSelect.components.LoadingMessage, "LoadingMessage"),
  MultiValue: withDataAttrs(_reactSelect.components.MultiValue, "MultiValue"),
  MultiValueContainer: withDataAttrs(_reactSelect.components.MultiValueContainer, "MultiValueContainer"),
  MultiValueLabel: withDataAttrs(_reactSelect.components.MultiValueLabel, "MultiValueLabel"),
  MultiValueRemove: withDataAttrs(_reactSelect.components.MultiValueRemove, "MultiValueRemove"),
  NoOptionsMessage: withDataAttrs(_reactSelect.components.NoOptionsMessage, "NoOptionsMessage"),
  Option: withDataAttrs(Option, "Option"),
  Placeholder: withDataAttrs(_reactSelect.components.Placeholder, "Placeholder"),
  SelectContainer: withDataAttrs(_reactSelect.components.SelectContainer, "SelectContainer"),
  SingleValue: withDataAttrs(_reactSelect.components.SingleValue, "SingleValue"),
  ValueContainer: withDataAttrs(_reactSelect.components.ValueContainer, "ValueContainer")
});
var VirtualItem = function VirtualItem(_ref2) {
  var virtualRow = _ref2.virtualRow,
    child = _ref2.child,
    virtualizer = _ref2.virtualizer;
  return /*#__PURE__*/_react["default"].createElement("div", {
    key: virtualRow.key,
    style: {
      transform: "translateY(" + virtualRow.start + "px)",
      top: 0,
      left: 0,
      right: 0,
      position: "absolute"
    },
    ref: virtualizer.measureElement,
    "data-index": virtualRow.index
  }, child);
};
var VirtualList = function VirtualList(_ref3) {
  var children = _ref3.children,
    parentRef = _ref3.parentRef;
  var virtualizer = (0, _reactVirtual.useVirtualizer)({
    count: children.length,
    getScrollElement: function getScrollElement() {
      return parentRef.current;
    },
    estimateSize: function estimateSize() {
      return 35;
    },
    overscan: 5
  });
  var virtualItems = virtualizer.getVirtualItems();
  return /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      height: virtualizer.getTotalSize() + "px",
      position: "relative"
    }
  }, virtualItems.map(function (virtualRow) {
    return /*#__PURE__*/_react["default"].createElement(VirtualItem, {
      key: virtualRow.key,
      virtualRow: virtualRow,
      child: children[virtualRow.index],
      virtualizer: virtualizer
    });
  }));
};
var VirtualizedMenuList = function VirtualizedMenuList(props) {
  var parentRef = (0, _react.useRef)();
  return /*#__PURE__*/_react["default"].createElement(customComponents.MenuList, _extends({}, props, {
    innerRef: parentRef,
    styles: {
      height: "300px",
      overflow: "auto",
      position: "relative"
    }
  }), /*#__PURE__*/_react["default"].createElement(VirtualList, {
    parentRef: parentRef
  }, props.children));
};
var makeCustomTheme = function makeCustomTheme(theme) {
  return function (selectTheme) {
    return _extends({}, selectTheme, {
      borderRadius: 4,
      colors: _extends({}, 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 fontSize = "12px";
var lineHeight = "16px";
var makeCustomStyles = function makeCustomStyles(theme, _temp) {
  var _ref4 = _temp === void 0 ? {} : _temp,
    minWidth = _ref4.minWidth,
    providedStyles = _objectWithoutPropertiesLoose(_ref4, _excluded2);
  return _extends({
    control: function control(styles, state) {
      return _extends({}, styles, {
        borderColor: state.isFocused ? theme.colors.inputBorderFocus : theme.colors.inputBorder,
        borderRadius: "2px",
        boxShadow: "none",
        minHeight: 18,
        minWidth: minWidth || 160,
        ":hover": {
          borderColor: theme.colors.inputBorderHover
        }
      });
    },
    input: function input(styles, state) {
      return _extends({}, styles, {
        color: state.isDisabled ? theme.colors.placeholder : theme.colors.grey140,
        fontSize: fontSize,
        lineHeight: lineHeight,
        paddingBottom: 0,
        paddingTop: 0
      });
    },
    menu: function menu(styles) {
      return _extends({}, styles, {
        borderRadius: "2px",
        zIndex: 100
      });
    },
    menuPortal: function menuPortal(styles) {
      return _extends({}, styles, {
        zIndex: 9999
      });
    },
    multiValue: function multiValue(styles) {
      return _extends({}, styles, {
        fontSize: fontSize,
        flexDirection: "row-reverse",
        minHeight: 18
      });
    },
    multiValueLabel: function multiValueLabel(styles, state) {
      return _extends({}, styles, {
        backgroundColor: theme.colors.grey40,
        borderRadius: "0 2px 2px 0",
        color: state.isDisabled ? theme.colors.placeholder : theme.colors.grey140,
        padding: "1px",
        paddingRight: state.data.isDisabled ? "8px" : ""
      });
    },
    multiValueRemove: function multiValueRemove(styles, state) {
      return _extends({
        color: state.isDisabled ? theme.colors.placeholder : theme.colors.grey140
      }, state.data.isDisabled ? _extends({}, styles, {
        display: "none"
      }) : _extends({}, styles, {
        borderRadius: "2px 0 0 2px",
        background: theme.colors.grey40,
        ":hover": {
          background: theme.colors.tabsBorder
        }
      }));
    },
    option: function option(styles, state) {
      return _extends({}, styles, {
        display: "flex",
        alignItems: "center",
        color: getOptionColor(theme, state),
        backgroundColor: getOptionBackground(theme, state),
        ":hover": {
          backgroundColor: theme.colors.secondaryHighlight,
          color: theme.colors.text
        },
        fontSize: fontSize,
        lineHeight: lineHeight,
        minHeight: 28,
        padding: "4px 8px"
      });
    },
    placeholder: function placeholder(styles) {
      return _extends({}, styles, {
        color: theme.colors.placeholder,
        fontSize: fontSize,
        lineHeight: lineHeight
      });
    },
    singleValue: function singleValue(styles, state) {
      return _extends({}, styles, {
        color: state.isDisabled ? theme.colors.placeholder : theme.colors.grey140,
        fontSize: fontSize,
        lineHeight: lineHeight
      });
    },
    indicatorsContainer: function indicatorsContainer(styles) {
      return _extends({}, styles, {
        minHeight: 18
      });
    },
    dropdownIndicator: function dropdownIndicator(styles) {
      return _extends({}, styles, {
        padding: "1px"
      });
    },
    clearIndicator: function clearIndicator(styles) {
      return _extends({}, styles, {
        padding: "1px"
      });
    },
    valueContainer: function valueContainer(styles) {
      return _extends({}, styles, {
        minHeight: 18,
        padding: "0px 6px"
      });
    }
  }, providedStyles);
};
var components = _extends({}, customComponents, {
  MenuList: VirtualizedMenuList
});
var getAttrs = function getAttrs(props) {
  return _extends({}, props, {
    components: props.components ? _extends({}, components, props.components) : components,
    theme: makeCustomTheme(props.theme),
    styles: makeCustomStyles(props.theme, props.styles)
  });
};
var SelectComponent = (0, _styledComponents["default"])(_reactSelect["default"]).attrs(getAttrs).withConfig({
  displayName: "select__SelectComponent",
  componentId: "sc-4oqdin-0"
})([""]);
var CreatableComponent = (0, _styledComponents["default"])(_creatable["default"]).attrs(getAttrs).withConfig({
  displayName: "select__CreatableComponent",
  componentId: "sc-4oqdin-1"
})([""]);
var Select = function Select(_ref5) {
  var isCreatable = _ref5.isCreatable,
    props = _objectWithoutPropertiesLoose(_ref5, _excluded3);
  if (isCreatable) return /*#__PURE__*/_react["default"].createElement(CreatableComponent, props);
  return /*#__PURE__*/_react["default"].createElement(SelectComponent, props);
};
var _default = exports["default"] = Select;