/*
 * 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 } from "@osdk/react";
import React, { memo, useCallback, useMemo } from "react";
import { DateRangeHistogramInput } from "../base/inputs/DateRangeHistogramInput.js";
import { NullValueWrapper } from "../base/inputs/NullValueWrapper.js";
import { createGroupByAggregateOptions, createNullCountAggregateOptions, createNullCountWhereClause } from "../utils/aggregationHelpers.js";
function DateRangeFilterInputInner({
  objectType,
  objectSet,
  propertyKey,
  filterState,
  onFilterStateChanged,
  whereClause,
  formatDate,
  clickToFilter
}) {
  const dateRangeState = filterState?.type === "DATE_RANGE" ? filterState : undefined;
  const includeNull = filterState?.includeNull;
  const handleNullChange = useCallback(includeNull => {
    onFilterStateChanged({
      type: "DATE_RANGE",
      minValue: dateRangeState?.minValue,
      maxValue: dateRangeState?.maxValue,
      includeNull
    });
  }, [onFilterStateChanged, dateRangeState?.minValue, dateRangeState?.maxValue]);
  const handleRangeChange = useCallback((minValue, maxValue) => {
    onFilterStateChanged({
      type: "DATE_RANGE",
      minValue,
      maxValue,
      includeNull
    });
  }, [onFilterStateChanged, includeNull]);
  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 date = new Date(String(rawValue));
        if (!isNaN(date.getTime())) {
          pairs.push({
            value: date,
            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(DateRangeHistogramInput, {
    valueCountPairs: valueCountPairs,
    isLoading: isLoading,
    minValue: dateRangeState?.minValue,
    maxValue: dateRangeState?.maxValue,
    onChange: handleRangeChange,
    formatDate: formatDate,
    clickToFilter: clickToFilter
  }));
}
export const DateRangeFilterInput = /*#__PURE__*/memo(DateRangeFilterInputInner);
//# sourceMappingURL=DateRangeFilterInput.js.map