// packages/block-library/src/avatar/hooks.js
import { store as coreStore, useEntityProp } from "@wordpress/core-data";
import { __, sprintf } from "@wordpress/i18n";
import { useSelect } from "@wordpress/data";
import { useDefaultAvatar } from "../utils/hooks";
function getAvatarSizes(sizes) {
  const minSize = sizes ? sizes[0] : 24;
  const maxSize = sizes ? sizes[sizes.length - 1] : 96;
  const maxSizeBuffer = Math.floor(maxSize * 2.5);
  return {
    minSize,
    maxSize: maxSizeBuffer
  };
}
function useCommentAvatar({ commentId }) {
  const [avatars] = useEntityProp(
    "root",
    "comment",
    "author_avatar_urls",
    commentId
  );
  const [authorName] = useEntityProp(
    "root",
    "comment",
    "author_name",
    commentId
  );
  const avatarUrls = avatars ? Object.values(avatars) : null;
  const sizes = avatars ? Object.keys(avatars) : null;
  const { minSize, maxSize } = getAvatarSizes(sizes);
  const defaultAvatar = useDefaultAvatar();
  return {
    src: avatarUrls ? avatarUrls[avatarUrls.length - 1] : defaultAvatar,
    minSize,
    maxSize,
    alt: authorName ? (
      // translators: %s: Author name.
      sprintf(__("%s Avatar"), authorName)
    ) : __("Default Avatar")
  };
}
function useUserAvatar({ userId, postId, postType }) {
  const { authorDetails } = useSelect(
    (select) => {
      const { getEditedEntityRecord, getUser } = select(coreStore);
      if (userId) {
        return {
          authorDetails: getUser(userId)
        };
      }
      const _authorId = getEditedEntityRecord(
        "postType",
        postType,
        postId
      )?.author;
      return {
        authorDetails: _authorId ? getUser(_authorId) : null
      };
    },
    [postType, postId, userId]
  );
  const avatarUrls = authorDetails?.avatar_urls ? Object.values(authorDetails.avatar_urls) : null;
  const sizes = authorDetails?.avatar_urls ? Object.keys(authorDetails.avatar_urls) : null;
  const { minSize, maxSize } = getAvatarSizes(sizes);
  const defaultAvatar = useDefaultAvatar();
  return {
    src: avatarUrls ? avatarUrls[avatarUrls.length - 1] : defaultAvatar,
    minSize,
    maxSize,
    alt: authorDetails ? (
      // translators: %s: Author name.
      sprintf(__("%s Avatar"), authorDetails?.name)
    ) : __("Default Avatar")
  };
}
export {
  useCommentAvatar,
  useUserAvatar
};
//# sourceMappingURL=hooks.js.map
