function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
/*
 * Copyright 2026 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, { useMemo } from "react";
import { SkeletonBar } from "../../base-components/skeleton/SkeletonBar.js";
import { useInfiniteScroll } from "../../shared/hooks/useInfiniteScroll.js";
import { typedReactMemo } from "../../shared/typedMemo.js";
import { DropdownField } from "./DropdownField.js";
import styles from "./AsyncDropdownField.module.css";
export const AsyncDropdownField = typedReactMemo(function ({
  isLoading,
  hasMore,
  isSearching,
  onFetchMore,
  fetchError,
  ...dropdownProps
}) {
  const itemCount = dropdownProps.items.length;
  const popupStatus = useMemo(() => {
    if (fetchError != null) {
      return /*#__PURE__*/React.createElement("div", {
        className: styles.osdkAsyncDropdownError,
        role: "alert"
      }, fetchError.message);
    }
    if (isSearching) {
      return /*#__PURE__*/React.createElement("div", {
        className: styles.osdkAsyncDropdownStatus
      }, "Searching\u2026");
    }
    // Show "Loading…" during the initial fetch before any data arrives,
    // so the user doesn't see a misleading "No results" message.
    if (isLoading && itemCount === 0) {
      return /*#__PURE__*/React.createElement("div", {
        className: styles.osdkAsyncDropdownStatus
      }, "Loading\u2026");
    }
    // "No results" is handled by Combobox.Empty inside DropdownField
    return null;
  }, [fetchError, isSearching, isLoading, itemCount]);
  const infiniteScrollRef = useInfiniteScroll({
    callback: onFetchMore,
    loadedCount: itemCount
  });
  return /*#__PURE__*/React.createElement(DropdownField, _extends({}, dropdownProps, {
    isSearchable: true,
    popupStatus: popupStatus,
    trailingItem: hasMore ? /*#__PURE__*/React.createElement(InfiniteScrollSentinel, {
      key: "sentinel",
      infiniteScrollRef: infiniteScrollRef
    }) : null,
    disableClientSideFiltering: dropdownProps.onQueryChange != null
  }));
});

/** Skeleton placeholder that triggers the next page fetch when it scrolls into view. */
function InfiniteScrollSentinel({
  infiniteScrollRef
}) {
  return /*#__PURE__*/React.createElement("div", {
    ref: infiniteScrollRef,
    role: "presentation"
  }, /*#__PURE__*/React.createElement(SkeletonBar, {
    className: styles.osdkAsyncDropdownSkeleton
  }));
}
//# sourceMappingURL=AsyncDropdownField.js.map