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); }
/*
 * Copyright 2025 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 { Button } from "@base-ui/react/button";
import { Input } from "@base-ui/react/input";
import classnames from "classnames";
import React, { memo, useCallback, useMemo, useState } from "react";
import { ErrorBoundary } from "../../shared/ErrorBoundary.js";
import { filterHasActiveState, getEffectiveFilterState, supportsExcluding, supportsSearch } from "../utils/filterValues.js";
import { DragHandleIcon } from "./DragHandleIcon.js";
import { OverflowMenuIcon, RemoveIcon, SearchIcon } from "./FilterIcons.js";
import styles from "./FilterListItem.module.css.js";
function FilterListItemInner({
  definition,
  filterKey,
  label,
  filterState,
  onFilterStateChanged,
  onFilterRemoved,
  renderInput,
  searchField,
  dragHandleAttributes,
  dragHandleListeners,
  className,
  style
}) {
  const [searchState, setSearchState] = useState({
    type: "closed"
  });
  const [excludeRowOpen, setExcludeRowOpen] = useState(false);
  const handleFilterStateChanged = useCallback(newState => {
    onFilterStateChanged(filterKey, newState);
  }, [filterKey, onFilterStateChanged]);
  const handleToggleSearch = useCallback(() => {
    setSearchState(prev => prev.type === "closed" ? {
      type: "open",
      query: ""
    } : {
      type: "closed"
    });
  }, []);
  const handleSearchChange = useCallback(e => {
    setSearchState({
      type: "open",
      query: e.target.value
    });
  }, []);
  const handleSearchClear = useCallback(() => {
    setSearchState({
      type: "open",
      query: ""
    });
  }, []);
  const handleRemove = useCallback(() => {
    onFilterRemoved?.(filterKey);
  }, [filterKey, onFilterRemoved]);
  const handleToggleExcludeRow = useCallback(() => {
    setExcludeRowOpen(prev => !prev);
  }, []);
  const searchInputRef = useCallback(element => {
    element?.focus({
      preventScroll: true
    });
  }, []);
  const effectiveState = useMemo(() => getEffectiveFilterState(filterState), [filterState]);
  const showExcludeDropdown = supportsExcluding(effectiveState);
  const showSearch = supportsSearch(effectiveState) && searchField !== false;
  const hasSelection = filterHasActiveState(filterState);
  const searchOpen = searchState.type === "open";
  const searchQuery = searchState.type === "open" ? searchState.query : "";
  const searchQueryForInput = searchState.type === "open" ? searchState.query : undefined;
  return /*#__PURE__*/React.createElement("div", {
    className: classnames(styles.filterItem, className),
    style: style,
    "data-has-selection": hasSelection || undefined
  }, /*#__PURE__*/React.createElement("div", {
    className: styles.itemHeader
  }, dragHandleAttributes && /*#__PURE__*/React.createElement(Button, _extends({
    className: styles.dragHandle,
    "aria-label": `Reorder ${label}`
  }, dragHandleAttributes, dragHandleListeners), /*#__PURE__*/React.createElement(DragHandleIcon, null)), /*#__PURE__*/React.createElement("span", {
    className: styles.itemLabel
  }, label), showSearch && /*#__PURE__*/React.createElement(Button, {
    className: styles.headerActionButton,
    onClick: handleToggleSearch,
    "aria-label": "Search values",
    "aria-pressed": searchOpen
  }, /*#__PURE__*/React.createElement(SearchIcon, null)), onFilterRemoved && /*#__PURE__*/React.createElement(Button, {
    className: styles.headerActionButton,
    onClick: handleRemove,
    "aria-label": `Remove ${label} filter`
  }, /*#__PURE__*/React.createElement(RemoveIcon, null)), showExcludeDropdown && /*#__PURE__*/React.createElement(Button, {
    className: styles.headerActionButton,
    onClick: handleToggleExcludeRow,
    "aria-label": "More actions",
    "aria-pressed": excludeRowOpen
  }, /*#__PURE__*/React.createElement(OverflowMenuIcon, null))), searchOpen && /*#__PURE__*/React.createElement("div", {
    className: styles.searchRow
  }, /*#__PURE__*/React.createElement(Input, {
    type: "text",
    className: styles.searchInput,
    value: searchQuery,
    onChange: handleSearchChange,
    placeholder: "Search property values...",
    "aria-label": "Search property values",
    ref: searchInputRef
  }), searchQuery && /*#__PURE__*/React.createElement(Button, {
    type: "button",
    className: styles.searchClearButton,
    onClick: handleSearchClear,
    "aria-label": "Clear search"
  }, /*#__PURE__*/React.createElement(RemoveIcon, null))), /*#__PURE__*/React.createElement("div", {
    className: styles.itemContent
  }, /*#__PURE__*/React.createElement(ErrorBoundary, {
    errorMessage: "Error loading filter"
  }, renderInput({
    definition,
    filterKey,
    filterState,
    onFilterStateChanged: handleFilterStateChanged,
    searchQuery: searchQueryForInput,
    excludeRowOpen
  }))));
}
export const FilterListItem = /*#__PURE__*/memo(FilterListItemInner);
//# sourceMappingURL=FilterListItem.js.map