/*
 * 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 { useOsdkAggregation, useOsdkMetadata } from "@osdk/react";
import React, { memo, useCallback, useMemo } from "react";
import { NullValueWrapper } from "../base/inputs/NullValueWrapper.js";
import { NumberRangeInput } from "../base/inputs/NumberRangeInput.js";
import { createGroupByAggregateOptions, createNullCountAggregateOptions, createNullCountWhereClause } from "../utils/aggregationHelpers.js";
function NumberRangeFilterInputInner({
  objectType,
  objectSet,
  propertyKey,
  filterState,
  onFilterStateChanged,
  whereClause,
  clickToFilter
}) {
  const numberRangeState = filterState?.type === "NUMBER_RANGE" ? filterState : undefined;
  const includeNull = filterState?.includeNull;
  const {
    metadata
  } = useOsdkMetadata(objectType);
  const propertyType = metadata?.properties?.[propertyKey]?.type;
  const isInteger = propertyType === "integer" || propertyType === "long" || propertyType === "short" || propertyType === "byte";
  const handleNullChange = useCallback(includeNull => {
    onFilterStateChanged({
      type: "NUMBER_RANGE",
      minValue: numberRangeState?.minValue,
      maxValue: numberRangeState?.maxValue,
      includeNull
    });
  }, [onFilterStateChanged, numberRangeState?.minValue, numberRangeState?.maxValue]);
  const handleRangeChange = useCallback((minValue, maxValue) => {
    onFilterStateChanged({
      type: "NUMBER_RANGE",
      minValue: (v => isInteger && v !== undefined ? Math.ceil(v) : v)(minValue),
      maxValue: (v => isInteger && v !== undefined ? Math.floor(v) : v)(maxValue),
      includeNull
    });
  }, [onFilterStateChanged, includeNull, isInteger]);
  const aggregateOptions = useMemo(() => createGroupByAggregateOptions(propertyKey), [propertyKey]);
  const histogramArgs = useMemo(() => ({
    aggregate: aggregateOptions,
    objectSet,
    where: whereClause
  }), [aggregateOptions, objectSet, whereClause]);
  const {
    data: aggregateData,
    isLoading: histLoading
  } = useOsdkAggregation(objectType, histogramArgs);
  const valueCountPairs = useMemo(() => {
    if (!aggregateData) return [];
    const pairs = [];
    for (const item of aggregateData) {
      const rawValue = item.$group[propertyKey];
      if (rawValue != null) {
        const parsed = parseFloat(String(rawValue));
        if (!isNaN(parsed)) {
          pairs.push({
            value: parsed,
            count: item.$count ?? 0
          });
        }
      }
    }
    return pairs;
  }, [aggregateData, propertyKey]);
  const nullCountAggregateOptions = useMemo(() => createNullCountAggregateOptions(), []);
  const nullCountWhereClause = useMemo(() => createNullCountWhereClause(propertyKey, whereClause), [propertyKey, whereClause]);
  const nullCountArgs = useMemo(() => ({
    where: nullCountWhereClause,
    aggregate: nullCountAggregateOptions,
    objectSet
  }), [nullCountWhereClause, nullCountAggregateOptions, objectSet]);
  const {
    data: nullCountData,
    isLoading: nullLoading,
    error: nullError
  } = useOsdkAggregation(objectType, nullCountArgs);
  const nullCount = useMemo(() => {
    if (!nullCountData) return 0;
    const result = nullCountData;
    if ("$count" in result && typeof result.$count === "number") {
      return result.$count;
    }
    return 0;
  }, [nullCountData]);
  const isLoading = histLoading || nullLoading;
  return /*#__PURE__*/React.createElement(NullValueWrapper, {
    nullCount: nullCount,
    isLoading: isLoading,
    error: nullError,
    includeNull: includeNull,
    onIncludeNullChange: handleNullChange
  }, /*#__PURE__*/React.createElement(NumberRangeInput, {
    valueCountPairs: valueCountPairs,
    isLoading: isLoading,
    minValue: numberRangeState?.minValue,
    maxValue: numberRangeState?.maxValue,
    onChange: handleRangeChange,
    clickToFilter: clickToFilter && metadata != null
  }));
}
export const NumberRangeFilterInput = /*#__PURE__*/memo(NumberRangeFilterInputInner);
//# sourceMappingURL=NumberRangeFilterInput.js.map