/*
 * 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 { ListogramInput } from "../base/inputs/ListogramInput.js";
import { useFilterPropertyAggregation } from "../hooks/useFilterPropertyAggregation.js";
import { EMPTY_LINKED_FILTERS } from "../types/LinkedFilterTypes.js";
import { coerceToStringArray } from "../utils/coerceFilterValue.js";
function ListogramFilterInputInner({
  objectType,
  objectSet,
  propertyKey,
  filterState,
  onFilterStateChanged,
  whereClause,
  linkedFilters = EMPTY_LINKED_FILTERS,
  showFilteredOutValues,
  colorMap,
  displayMode,
  showCount,
  maxVisibleItems,
  searchQuery,
  excludeRowOpen,
  renderValue
}) {
  const selectedValues = useMemo(() => filterState?.type === "EXACT_MATCH" ? coerceToStringArray(filterState.values) : [], [filterState]);
  const isExcluding = filterState?.isExcluding ?? false;
  const handleClearAll = useCallback(() => {
    onFilterStateChanged({
      type: "EXACT_MATCH",
      values: [],
      isExcluding
    });
  }, [onFilterStateChanged, isExcluding]);
  const handleChange = useCallback(values => {
    onFilterStateChanged({
      type: "EXACT_MATCH",
      values,
      isExcluding
    });
  }, [onFilterStateChanged, isExcluding]);
  const sortBy = displayMode === "minimal" ? "value" : "count";
  const {
    data,
    maxCount,
    isLoading,
    error
  } = useFilterPropertyAggregation(objectType, propertyKey, objectSet, whereClause, linkedFilters, {
    sortBy,
    selectedValues,
    showFilteredOutValues
  });
  return /*#__PURE__*/React.createElement(FilterInputExcludeRow, {
    excludeRowOpen: excludeRowOpen,
    filterState: filterState,
    onFilterStateChanged: onFilterStateChanged,
    totalValueCount: data.length,
    onClearAll: handleClearAll
  }, /*#__PURE__*/React.createElement(ListogramInput, {
    values: data,
    maxCount: maxCount,
    isLoading: isLoading,
    error: error,
    selectedValues: selectedValues,
    onChange: handleChange,
    colorMap: colorMap,
    displayMode: displayMode,
    showCount: showCount,
    isExcluding: isExcluding,
    maxVisibleItems: maxVisibleItems,
    searchQuery: searchQuery,
    renderValue: renderValue
  }));
}
export const ListogramFilterInput = /*#__PURE__*/memo(ListogramFilterInputInner);
//# sourceMappingURL=ListogramFilterInput.js.map