"use client";
import { jsx } from "react/jsx-runtime";
import uniqBy from "lodash/uniqBy.js";
import { useState, Children, isValidElement } from "react";
import { DropdownMenu } from "./DropdownMenu.js";
import { MenuGroup } from "./MenuGroup.js";
import { MenuSearchEmptyState } from "./MenuSearchEmptyState.js";
import { MenuSearchInput } from "./MenuSearchInput.js";
const isDefined = (x) => x !== null;
function UncontrolledSearchMenu({
  placeholder,
  children,
  emptyStateText,
  ...rest
}) {
  const [searchPattern, setSearchPattern] = useState("");
  const groups = Array.isArray(children) ? children : children.props.children;
  const lowercasedSearchPattern = searchPattern.toLowerCase();
  const searchResults = uniqBy(
    Children.map(groups, (child) => {
      return Children.map(child.props.children || [], (grandchild) => {
        if (!isValidElement(grandchild)) return null;
        const props = grandchild.props;
        const value = props.textValue || props.value;
        if (typeof value !== "string") return null;
        return value.toLowerCase().includes(lowercasedSearchPattern) ? grandchild : null;
      });
    }).flat().filter(isDefined),
    (x) => x.props.textValue || x.props.value
  );
  return /* @__PURE__ */ jsx(
    DropdownMenu,
    {
      header: /* @__PURE__ */ jsx(MenuSearchInput, { placeholder, value: searchPattern, onChange: setSearchPattern }),
      ...rest,
      children: searchPattern ? searchResults.length ? /* @__PURE__ */ jsx(MenuGroup, { label: "Results", children: searchResults }) : /* @__PURE__ */ jsx(MenuSearchEmptyState, { text: emptyStateText, onResetClick: () => setSearchPattern("") }) : children
    }
  );
}
export {
  UncontrolledSearchMenu
};
