/*
 * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Menu } from "@base-ui/react/menu";
import classnames from "classnames";
import React, { memo, useCallback, useMemo, useState } from "react";
import { usePortalContainer } from "../../shared/PortalContainerContext.js";
import { SearchBar } from "../search-bar/SearchBar.js";
import styles from "./SearchableMenu.module.css";
function SearchableMenuInner({
  items,
  onItemSelected,
  trigger,
  triggerClassName,
  disabled,
  searchPlaceholder = "Search",
  emptyMessage = "No matching items",
  className,
  collisionBoundary
}) {
  const [searchQuery, setSearchQuery] = useState("");
  const portalContainer = usePortalContainer();
  const filteredItems = useMemo(() => {
    const query = searchQuery.toLowerCase().trim();
    if (!query) return items;
    return items.filter(item => item.label.toLowerCase().includes(query));
  }, [items, searchQuery]);
  const handleSearchChange = useCallback(event => {
    setSearchQuery(event.target.value);
  }, []);
  const handleMenuOpenChange = useCallback(open => {
    if (open) {
      setSearchQuery("");
    }
  }, []);
  const handleKeyDown = useCallback(event => {
    event.stopPropagation();
  }, []);
  return /*#__PURE__*/React.createElement(Menu.Root, {
    onOpenChange: handleMenuOpenChange
  }, /*#__PURE__*/React.createElement(Menu.Trigger, {
    className: triggerClassName,
    disabled: disabled
  }, trigger), /*#__PURE__*/React.createElement(Menu.Portal, {
    container: portalContainer
  }, /*#__PURE__*/React.createElement(Menu.Positioner, {
    className: classnames(styles.positioner, className),
    sideOffset: 4,
    collisionBoundary: collisionBoundary
  }, /*#__PURE__*/React.createElement(Menu.Popup, {
    className: styles.popup
  }, /*#__PURE__*/React.createElement(SearchBar, {
    value: searchQuery,
    onChange: handleSearchChange,
    placeholder: searchPlaceholder,
    "aria-label": searchPlaceholder,
    className: styles.searchContainer,
    onKeyDown: handleKeyDown
  }), filteredItems.map(item => /*#__PURE__*/React.createElement(SearchableMenuItemRow, {
    key: item.key,
    item: item,
    onItemSelected: onItemSelected
  })), filteredItems.length === 0 && /*#__PURE__*/React.createElement("div", {
    className: styles.emptyState
  }, emptyMessage)))));
}
export const SearchableMenu = /*#__PURE__*/memo(SearchableMenuInner);
function SearchableMenuItemRowInner({
  item,
  onItemSelected
}) {
  const handleClick = useCallback(() => onItemSelected(item.key), [onItemSelected, item.key]);
  return /*#__PURE__*/React.createElement(Menu.Item, {
    className: styles.menuItem,
    onClick: handleClick
  }, item.label);
}
const SearchableMenuItemRow = /*#__PURE__*/memo(SearchableMenuItemRowInner);
//# sourceMappingURL=SearchableMenu.js.map