/*
 * 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 React, { memo, useCallback, useMemo } from "react";
import { FilterInputExcludeRow } from "../base/FilterInputExcludeRow.js";
import { MultiSelectInput } from "../base/inputs/MultiSelectInput.js";
import { useFilterPropertyAggregation } from "../hooks/useFilterPropertyAggregation.js";
import { EMPTY_LINKED_FILTERS } from "../types/LinkedFilterTypes.js";
import { coerceToStringArray } from "../utils/coerceFilterValue.js";
function MultiSelectFilterInputInner({
  objectType,
  objectSet,
  propertyKey,
  filterState,
  onFilterStateChanged,
  whereClause,
  linkedFilters = EMPTY_LINKED_FILTERS,
  showFilteredOutValues,
  excludeRowOpen,
  renderValue,
  showCount,
  layout
}) {
  const selectedValues = useMemo(() => filterState?.type === "SELECT" ? coerceToStringArray(filterState.selectedValues) : [], [filterState]);
  const isExcluding = filterState?.isExcluding ?? false;
  const handleClearAll = useCallback(() => {
    onFilterStateChanged({
      type: "SELECT",
      selectedValues: [],
      isExcluding
    });
  }, [onFilterStateChanged, isExcluding]);
  const handleChange = useCallback(selectedValues => {
    onFilterStateChanged({
      type: "SELECT",
      selectedValues,
      isExcluding
    });
  }, [onFilterStateChanged, isExcluding]);
  const {
    data,
    isLoading,
    error
  } = useFilterPropertyAggregation(objectType, propertyKey, objectSet, whereClause, linkedFilters, {
    selectedValues,
    showFilteredOutValues
  });
  return /*#__PURE__*/React.createElement(FilterInputExcludeRow, {
    excludeRowOpen: excludeRowOpen,
    filterState: filterState,
    onFilterStateChanged: onFilterStateChanged,
    totalValueCount: data.length,
    onClearAll: handleClearAll
  }, /*#__PURE__*/React.createElement(MultiSelectInput, {
    values: data,
    isLoading: isLoading,
    error: error,
    selectedValues: selectedValues,
    onChange: handleChange,
    showCounts: showCount,
    ariaLabel: `Search ${propertyKey} values`,
    renderValue: renderValue,
    layout: layout
  }));
}
export const MultiSelectFilterInput = /*#__PURE__*/memo(MultiSelectFilterInputInner);
//# sourceMappingURL=MultiSelectFilterInput.js.map