// packages/editor/src/components/collab-sidebar/note-indicator-toolbar.js
import { ToolbarButton } from "@wordpress/components";
import { Stack } from "@wordpress/ui";
import { __, sprintf } from "@wordpress/i18n";
import { useMemo } from "@wordpress/element";
import { privateApis as blockEditorPrivateApis } from "@wordpress/block-editor";
import { unlock } from "../../lock-unlock.mjs";
import { getAvatarBorderColor } from "./utils.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
var { NoteIconToolbarSlotFill } = unlock(blockEditorPrivateApis);
function NoteAvatarIndicator({ onClick, note }) {
  const threadParticipants = useMemo(() => {
    if (!note) {
      return [];
    }
    const participantsMap = /* @__PURE__ */ new Map();
    const allNotes = [note, ...note.reply];
    allNotes.sort((a, b) => new Date(a.date) - new Date(b.date));
    allNotes.forEach((entry) => {
      if (entry.author_name && entry.author_avatar_urls) {
        if (!participantsMap.has(entry.author)) {
          participantsMap.set(entry.author, {
            name: entry.author_name,
            avatar: entry.author_avatar_urls?.["48"] || entry.author_avatar_urls?.["96"],
            id: entry.author,
            date: entry.date
          });
        }
      }
    });
    return Array.from(participantsMap.values());
  }, [note]);
  if (!threadParticipants.length) {
    return null;
  }
  const maxAvatars = 3;
  const isOverflow = threadParticipants.length > maxAvatars;
  const visibleParticipants = isOverflow ? threadParticipants.slice(0, maxAvatars - 1) : threadParticipants;
  const overflowCount = Math.max(
    0,
    threadParticipants.length - visibleParticipants.length
  );
  const threadHasMoreParticipants = threadParticipants.length > 100;
  const overflowText = threadHasMoreParticipants && overflowCount > 0 ? __("100+") : sprintf(
    // translators: %s: Number of participants.
    __("+%s"),
    overflowCount
  );
  return /* @__PURE__ */ jsx(NoteIconToolbarSlotFill.Fill, { children: /* @__PURE__ */ jsx(
    ToolbarButton,
    {
      className: "editor-note-indicator",
      label: __("View notes"),
      onClick: () => onClick(),
      showTooltip: true,
      children: /* @__PURE__ */ jsxs(Stack, { direction: "row", align: "center", gap: "xs", children: [
        visibleParticipants.map((participant) => /* @__PURE__ */ jsx(
          "img",
          {
            src: participant.avatar,
            alt: participant.name,
            className: "editor-note-indicator__avatar",
            style: {
              borderColor: getAvatarBorderColor(
                participant.id
              )
            }
          },
          participant.id
        )),
        overflowCount > 0 && /* @__PURE__ */ jsx("span", { className: "editor-note-indicator__overflow", children: overflowText })
      ] })
    }
  ) });
}
export {
  NoteAvatarIndicator
};
//# sourceMappingURL=note-indicator-toolbar.mjs.map
