/*
 * 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 } from "react";
import { FilterInputExcludeRow } from "./base/FilterInputExcludeRow.js";
import { ContainsTextInput } from "./base/inputs/ContainsTextInput.js";
import { ToggleInput } from "./base/inputs/ToggleInput.js";
import { LinkedPropertyInput } from "./inputs/LinkedPropertyInput.js";
import { PropertyFilterInput } from "./inputs/PropertyFilterInput.js";
import { StaticValuesFilterInput } from "./inputs/StaticValuesFilterInput.js";
function FilterInputInner({
  objectType,
  objectSet,
  definition,
  filterState,
  onFilterStateChanged,
  whereClause,
  linkedFilters,
  showFilteredOutValues,
  searchQuery,
  excludeRowOpen,
  layout
}) {
  switch (definition.type) {
    case "HAS_LINK":
      return /*#__PURE__*/React.createElement(HasLinkInput, {
        filterState: filterState,
        onFilterStateChanged: onFilterStateChanged,
        excludeRowOpen: excludeRowOpen
      });
    case "LINKED_PROPERTY":
      {
        if (objectSet == null) {
          return /*#__PURE__*/React.createElement(React.Fragment, null);
        }
        return /*#__PURE__*/React.createElement(LinkedPropertyInput, {
          objectSet: objectSet,
          definition: definition,
          filterState: filterState,
          onFilterStateChanged: onFilterStateChanged,
          whereClause: whereClause,
          linkedFilters: linkedFilters,
          showFilteredOutValues: showFilteredOutValues,
          searchQuery: searchQuery,
          excludeRowOpen: excludeRowOpen,
          layout: layout
        });
      }
    case "KEYWORD_SEARCH":
      return /*#__PURE__*/React.createElement(KeywordSearchInput, {
        filterState: filterState,
        onFilterStateChanged: onFilterStateChanged,
        placeholder: definition.label ?? "Search..."
      });
    case "CUSTOM":
      {
        if (!definition.renderInput) {
          return /*#__PURE__*/React.createElement("div", {
            "data-unsupported": "true"
          }, "Custom filter missing renderInput");
        }
        const customFilterState = filterState?.type === "custom" ? filterState : definition.filterState;
        return /*#__PURE__*/React.createElement(React.Fragment, null, definition.renderInput({
          objectType,
          objectSet,
          filterState: customFilterState,
          onFilterStateChanged: state => onFilterStateChanged(state)
        }));
      }
    case "PROPERTY":
      return /*#__PURE__*/React.createElement(PropertyFilterInput, {
        objectType: objectType,
        objectSet: objectSet,
        definition: definition,
        filterState: filterState,
        onFilterStateChanged: onFilterStateChanged,
        whereClause: whereClause,
        linkedFilters: linkedFilters,
        showFilteredOutValues: showFilteredOutValues,
        searchQuery: searchQuery,
        excludeRowOpen: excludeRowOpen,
        layout: layout
      });
    case "STATIC_VALUES":
      return /*#__PURE__*/React.createElement(StaticValuesFilterInput, {
        definition: definition,
        filterState: filterState,
        onFilterStateChanged: onFilterStateChanged,
        searchQuery: searchQuery,
        excludeRowOpen: excludeRowOpen,
        layout: layout
      });
    default:
      return /*#__PURE__*/React.createElement("div", {
        "data-unsupported": "true"
      }, "Unsupported filter type");
  }
}
export const FilterInput = /*#__PURE__*/memo(FilterInputInner);
const HasLinkInput = /*#__PURE__*/memo(function ({
  filterState,
  onFilterStateChanged,
  excludeRowOpen
}) {
  const hasLink = filterState?.type === "hasLink" ? filterState.hasLink : false;
  const isExcluding = filterState?.type === "hasLink" ? filterState.isExcluding ?? false : false;
  const handleChange = useCallback(hasLink => {
    onFilterStateChanged({
      type: "hasLink",
      hasLink,
      isExcluding
    });
  }, [onFilterStateChanged, isExcluding]);
  const handleClearAll = useCallback(() => {
    onFilterStateChanged({
      type: "hasLink",
      hasLink: false,
      isExcluding
    });
  }, [onFilterStateChanged, isExcluding]);
  return /*#__PURE__*/React.createElement(FilterInputExcludeRow, {
    excludeRowOpen: excludeRowOpen,
    filterState: filterState,
    onFilterStateChanged: onFilterStateChanged,
    onClearAll: handleClearAll
  }, /*#__PURE__*/React.createElement(ToggleInput, {
    enabled: hasLink,
    onChange: handleChange
  }));
});
const KeywordSearchInput = /*#__PURE__*/memo(function ({
  filterState,
  onFilterStateChanged,
  placeholder
}) {
  const searchTerm = filterState?.type === "keywordSearch" ? filterState.searchTerm : undefined;
  const operator = filterState?.type === "keywordSearch" ? filterState.operator : "AND";
  const handleChange = useCallback(newSearchTerm => {
    onFilterStateChanged({
      type: "keywordSearch",
      searchTerm: newSearchTerm ?? "",
      operator
    });
  }, [onFilterStateChanged, operator]);
  return /*#__PURE__*/React.createElement(ContainsTextInput, {
    value: searchTerm,
    onChange: handleChange,
    placeholder: placeholder
  });
});
//# sourceMappingURL=FilterInput.js.map