/*
 * 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 { IconSize } from "@blueprintjs/icons";
import { useObjectSet, useOsdkMetadata } from "@osdk/react";
import classnames from "classnames";
import React from "react";
import { BlueprintIcon } from "../../base-components/icon/BlueprintIcon.js";
import { SkeletonBar } from "../../base-components/skeleton/SkeletonBar.js";
import { typedReactMemo } from "../../shared/typedMemo.js";
import styles from "./ObjectSetField.module.css";
const DEFAULT_OBJECT_ICON = {
  name: "cube",
  color: "#4C90F0"
};
const ICON_SIZE = IconSize.STANDARD;
const DEFAULT_EMPTY_MESSAGE = "Object set is not defined";
export const ObjectSetField = typedReactMemo(function ({
  value,
  emptyMessage = DEFAULT_EMPTY_MESSAGE,
  disabled
}) {
  if (value == null) {
    return /*#__PURE__*/React.createElement("div", {
      className: classnames(styles.osdkObjectSetField, styles.osdkObjectSetFieldEmpty),
      "aria-disabled": disabled === true || undefined
    }, emptyMessage);
  }
  return /*#__PURE__*/React.createElement(ObjectSetFieldContent, {
    objectSet: value,
    disabled: disabled
  });
});
const ObjectSetFieldContent = /*#__PURE__*/React.memo(function ({
  objectSet,
  disabled
}) {
  const objectTypeDef = objectSet.$objectSetInternals.def;
  const {
    metadata,
    loading: metadataLoading
  } = useOsdkMetadata(objectTypeDef);
  const {
    totalCount,
    data,
    isLoading: objectSetLoading,
    error: objectSetError
  } = useObjectSet(objectSet, {
    // Fetching a page size of one is enough to get the totalCount
    pageSize: 1
  });
  const hasMetadata = metadata != null;
  const icon = React.useMemo(() => metadata != null && "icon" in metadata && metadata.icon != null ? toComponentIcon(metadata.icon) : DEFAULT_OBJECT_ICON, [metadata]);
  const displayName = totalCount === "1" || !hasMetadata || !("pluralDisplayName" in metadata) ? metadata?.displayName : metadata.pluralDisplayName;

  // Wait for metadata even if the count is already available, so we can
  // show the correct display name and avoid a flash of "objects" → "Employees".

  return /*#__PURE__*/React.createElement("div", {
    className: styles.osdkObjectSetField,
    "aria-disabled": disabled === true || undefined
  }, metadataLoading && !hasMetadata ? /*#__PURE__*/React.createElement(React.Fragment, null, OBJECT_SET_ICON_SKELETON, OBJECT_SET_LABEL_SKELETON) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(BlueprintIcon, {
    icon: icon,
    size: ICON_SIZE
  }), /*#__PURE__*/React.createElement(ObjectSetLabel, {
    displayName: displayName,
    totalCount: totalCount,
    error: objectSetError,
    isLoading: objectSetLoading
  })));
});
const ObjectSetLabel = /*#__PURE__*/React.memo(function ({
  displayName,
  totalCount,
  isLoading,
  error
}) {
  const hasData = totalCount != null;
  const label = displayName ?? (totalCount === "1" ? "object" : "objects");
  const showSkeleton = isLoading && !hasData;
  const showError = error != null && !hasData && !isLoading;
  return /*#__PURE__*/React.createElement(React.Fragment, null, showSkeleton && OBJECT_SET_LABEL_SKELETON, showError && /*#__PURE__*/React.createElement("span", {
    className: styles.osdkObjectSetFieldError,
    role: "alert"
  }, `Failed to load: ${error.message}`), !showSkeleton && !showError && /*#__PURE__*/React.createElement("span", null, `${formatCount(totalCount)} ${label}`));
});
const OBJECT_SET_ICON_SKELETON = /*#__PURE__*/React.createElement(SkeletonBar, {
  className: styles.osdkObjectSetIconSkeleton
});
const OBJECT_SET_LABEL_SKELETON = /*#__PURE__*/React.createElement(SkeletonBar, {
  className: styles.osdkObjectSetLabelSkeleton
});
function formatCount(count) {
  if (count == null) {
    return "\u2013"; // '–' symbol
  }
  const num = Number(count);
  return Number.isNaN(num) ? count : num.toLocaleString();
}
function toComponentIcon(apiIcon) {
  return {
    name: apiIcon.name,
    color: apiIcon.color
  };
}
//# sourceMappingURL=ObjectSetField.js.map