var _excluded = ["hideShadow", "itemProps", "items", "onItemClick", "dropTitle", "dropTitlePadding", "Item", "Footer", "value", "hasSearch", "searchMargin", "gap", "estimateSize", "close"];
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 (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
import React, { forwardRef, useMemo, useState, useRef } from "react";
import styled from "styled-components";
import { useVirtualizer } from "@tanstack/react-virtual";
import Flex from "../../templates/flex";
import Search from "../../search";
import Box from "../../templates/box";
import { mergeRefs } from "../../../utils";
var Container = styled(Flex).withConfig({
  displayName: "dropdown__Container",
  componentId: "sc-1nyt35p-0"
})(["", " list-style-type:none;"], function (_ref) {
  var hideShadow = _ref.hideShadow;
  return !hideShadow && "box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2), 0 8px 10px 1px rgba(0, 0, 0, 0.14), 0 3px 14px 2px rgba(0, 0, 0, 0.12);";
});
var defaultEstimateSize = function defaultEstimateSize() {
  return 28;
};
var Dropdown = /*#__PURE__*/forwardRef(function (_ref2, forwardedRef) {
  var hideShadow = _ref2.hideShadow,
    itemProps = _ref2.itemProps,
    items = _ref2.items,
    onItemClick = _ref2.onItemClick,
    dropTitle = _ref2.dropTitle,
    _ref2$dropTitlePaddin = _ref2.dropTitlePadding,
    dropTitlePadding = _ref2$dropTitlePaddin === void 0 ? [3, 3, 0] : _ref2$dropTitlePaddin,
    Item = _ref2.Item,
    Footer = _ref2.Footer,
    value = _ref2.value,
    hasSearch = _ref2.hasSearch,
    _ref2$searchMargin = _ref2.searchMargin,
    searchMargin = _ref2$searchMargin === void 0 ? [4] : _ref2$searchMargin,
    _ref2$gap = _ref2.gap,
    gap = _ref2$gap === void 0 ? 0 : _ref2$gap,
    _ref2$estimateSize = _ref2.estimateSize,
    estimateSize = _ref2$estimateSize === void 0 ? defaultEstimateSize : _ref2$estimateSize,
    close = _ref2.close,
    rest = _objectWithoutPropertiesLoose(_ref2, _excluded);
  var _useState = useState(""),
    searchValue = _useState[0],
    setSearchValue = _useState[1];
  var filteredItems = useMemo(function () {
    if (!searchValue) return items;
    var searchLowerCase = searchValue.toLowerCase();
    return items.filter(function (_ref3) {
      var label = _ref3.label,
        val = _ref3.value;
      if (typeof label === "string" && label.toLowerCase().includes(searchLowerCase)) return true;
      if (typeof val === "string" && val.toLowerCase().includes(searchLowerCase)) return true;
      return false;
    });
  }, [items, searchValue]);
  var ref = useRef();
  var rowVirtualizer = useVirtualizer({
    count: filteredItems.length,
    getScrollElement: function getScrollElement() {
      return ref.current;
    },
    scrollOffsetFn: function scrollOffsetFn(event) {
      return event ? event.target.scrollTop - ref.current.offsetTop : 0;
    },
    overscan: 3,
    enableSmoothScroll: false,
    estimateSize: estimateSize
  });
  return /*#__PURE__*/React.createElement(Container, _extends({
    as: "ul",
    role: "listbox",
    background: "dropdown",
    hideShadow: hideShadow,
    padding: [0],
    margin: [1, 0],
    column: true,
    tabindex: "-1",
    width: "auto"
  }, rest), dropTitle && /*#__PURE__*/React.createElement(Flex, {
    padding: dropTitlePadding
  }, dropTitle), hasSearch && /*#__PURE__*/React.createElement(Box, {
    margin: searchMargin
  }, /*#__PURE__*/React.createElement(Search, {
    "data-testid": "dropdown-search",
    placeholder: "Search",
    onChange: setSearchValue
  })), /*#__PURE__*/React.createElement("div", {
    ref: mergeRefs(ref, forwardedRef),
    style: {
      height: "100%",
      overflow: "auto"
    }
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      minHeight: rowVirtualizer.getTotalSize() + "px",
      width: "100%",
      position: "relative"
    }
  }, rowVirtualizer.getVirtualItems().map(function (virtualRow) {
    return /*#__PURE__*/React.createElement("div", {
      key: virtualRow.key,
      style: {
        position: "absolute",
        top: 0,
        left: 0,
        width: "100%",
        transform: "translateY(" + virtualRow.start + "px)",
        padding: gap * 2,
        overflow: "hidden"
      },
      "data-index": virtualRow.index,
      ref: rowVirtualizer.measureElement
    }, /*#__PURE__*/React.createElement(Item, {
      item: filteredItems[virtualRow.index],
      index: virtualRow.index,
      itemProps: itemProps,
      value: value,
      onItemClick: onItemClick,
      close: close
    }));
  }))), Footer && /*#__PURE__*/React.createElement(Footer, {
    close: close
  }));
});
export default Dropdown;