/*
 * 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 classnames from "classnames";
import React, { memo, useCallback, useMemo } from "react";
import { Combobox } from "../../../base-components/combobox/Combobox.js";
import { useFilterListBoundary } from "../FilterListBoundaryContext.js";
import { createRenderValueFilter } from "./comboboxFilter.js";
import { OptionLabel } from "./OptionLabel.js";
import { SelectInputSkeleton } from "./SelectInputSkeleton.js";
import { useStableData } from "./useStableData.js";
import sharedStyles from "./shared.module.css";
import styles from "./SingleSelectInput.module.css";
function SingleSelectInputInner({
  values,
  isLoading,
  error,
  selectedValue,
  onChange,
  className,
  style,
  placeholder = "Select a value...",
  showClearButton = true,
  showCounts = false,
  ariaLabel = "Select value",
  renderValue
}) {
  const collisionBoundary = useFilterListBoundary();
  const handleValueChange = useCallback(value => {
    onChange(value ?? undefined);
  }, [onChange]);
  const stableValues = useStableData(values, isLoading);
  const items = useMemo(() => stableValues.map(({
    value
  }) => value), [stableValues]);
  const countByValue = useMemo(() => new Map(stableValues.map(({
    value,
    count
  }) => [value, count])), [stableValues]);
  const comboboxFilter = useMemo(() => renderValue ? createRenderValueFilter(renderValue) : undefined, [renderValue]);
  const renderItem = useCallback(value => {
    return /*#__PURE__*/React.createElement(Combobox.Item, {
      key: value,
      value: value
    }, /*#__PURE__*/React.createElement(Combobox.ItemIndicator, null), /*#__PURE__*/React.createElement("span", {
      className: styles.itemLabel
    }, /*#__PURE__*/React.createElement(OptionLabel, {
      value: value,
      renderValue: renderValue
    })), showCounts && /*#__PURE__*/React.createElement("span", {
      className: styles.itemCount
    }, "(", (countByValue.get(value) ?? 0).toLocaleString(), ")"));
  }, [countByValue, showCounts, renderValue]);
  const isNoData = !error && stableValues.length === 0;
  const isReloading = isLoading && stableValues.length > 0;
  return /*#__PURE__*/React.createElement("div", {
    className: classnames(styles.singleSelect, className),
    style: style,
    "data-loading": isReloading
  }, /*#__PURE__*/React.createElement("span", {
    className: sharedStyles.srOnly,
    role: "status"
  }, isLoading ? "Loading options" : ""), error && /*#__PURE__*/React.createElement("div", {
    className: sharedStyles.errorMessage
  }, "Error loading options: ", error.message), isNoData && isLoading && /*#__PURE__*/React.createElement(SelectInputSkeleton, null), isNoData && !isLoading && /*#__PURE__*/React.createElement("div", {
    className: sharedStyles.emptyMessage
  }, "No options available"), stableValues.length > 0 && /*#__PURE__*/React.createElement("div", {
    className: styles.selectContainer
  }, /*#__PURE__*/React.createElement(Combobox.Root, {
    value: selectedValue ?? null,
    onValueChange: handleValueChange,
    items: items,
    filter: comboboxFilter
  }, /*#__PURE__*/React.createElement(Combobox.SearchInput, {
    placeholder: placeholder,
    "aria-label": ariaLabel
  }), showClearButton && selectedValue !== undefined && /*#__PURE__*/React.createElement(Combobox.Clear, {
    className: styles.clearButton
  }), /*#__PURE__*/React.createElement(Combobox.Portal, null, /*#__PURE__*/React.createElement(Combobox.Positioner, {
    collisionBoundary: collisionBoundary
  }, /*#__PURE__*/React.createElement(Combobox.Popup, null, /*#__PURE__*/React.createElement(Combobox.Empty, null, "No matching options"), /*#__PURE__*/React.createElement(Combobox.List, null, renderItem)))))));
}
export const SingleSelectInput = /*#__PURE__*/memo(SingleSelectInputInner);
//# sourceMappingURL=SingleSelectInput.js.map