// packages/editor/src/components/collaborators-presence/index.tsx
import { Button } from "@wordpress/components";
import { useMemo, useState } from "@wordpress/element";
import {
  privateApis
} from "@wordpress/core-data";
import { __, sprintf } from "@wordpress/i18n";
import Avatar from "./avatar/index.mjs";
import AvatarGroup from "./avatar-group/index.mjs";
import { CollaboratorsList } from "./list.mjs";
import { unlock } from "../../lock-unlock.mjs";
import { getAvatarUrl } from "../collaborators-overlay/get-avatar-url.mjs";
import { getAvatarBorderColor } from "../collab-sidebar/utils.mjs";
import { createCursorRegistry } from "../collaborators-overlay/cursor-registry.mjs";
import { CollaboratorsOverlay } from "../collaborators-overlay/index.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var { useActiveCollaborators } = unlock(privateApis);
function CollaboratorsPresence({
  postId,
  postType
}) {
  const activeCollaborators = useActiveCollaborators(
    postId,
    postType
  );
  const otherActiveCollaborators = activeCollaborators.filter(
    (c) => !c.isMe
  );
  const collaboratorsForList = useMemo(() => {
    return [...activeCollaborators].sort((a, b) => {
      if (a.isMe && !b.isMe) {
        return -1;
      }
      if (!a.isMe && b.isMe) {
        return 1;
      }
      return 0;
    });
  }, [activeCollaborators]);
  const [cursorRegistry] = useState(createCursorRegistry);
  const [isPopoverVisible, setIsPopoverVisible] = useState(false);
  const [popoverAnchor, setPopoverAnchor] = useState(
    null
  );
  if (otherActiveCollaborators.length === 0) {
    return null;
  }
  const me = activeCollaborators.find((c) => c.isMe);
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsxs("div", { className: "editor-collaborators-presence", children: [
      /* @__PURE__ */ jsx(
        Button,
        {
          __next40pxDefaultSize: true,
          className: "editor-collaborators-presence__button",
          onClick: () => setIsPopoverVisible(!isPopoverVisible),
          isPressed: isPopoverVisible,
          ref: setPopoverAnchor,
          "aria-label": sprintf(
            // translators: %d: number of online collaborators.
            __("Collaborators list, %d online"),
            collaboratorsForList.length
          ),
          children: /* @__PURE__ */ jsxs(AvatarGroup, { max: 4, children: [
            me && /* @__PURE__ */ jsx(
              Avatar,
              {
                src: getAvatarUrl(
                  me.collaboratorInfo.avatar_urls
                ),
                name: me.collaboratorInfo.name,
                borderColor: "var(--wp-admin-theme-color)",
                size: "small"
              },
              me.clientId
            ),
            otherActiveCollaborators.map(
              (collaboratorState) => /* @__PURE__ */ jsx(
                Avatar,
                {
                  src: getAvatarUrl(
                    collaboratorState.collaboratorInfo.avatar_urls
                  ),
                  name: collaboratorState.collaboratorInfo.name,
                  borderColor: getAvatarBorderColor(
                    collaboratorState.collaboratorInfo.id
                  ),
                  size: "small"
                },
                collaboratorState.clientId
              )
            )
          ] })
        }
      ),
      isPopoverVisible && /* @__PURE__ */ jsx(
        CollaboratorsList,
        {
          activeCollaborators: collaboratorsForList,
          popoverAnchor,
          setIsPopoverVisible,
          cursorRegistry
        }
      )
    ] }),
    /* @__PURE__ */ jsx(
      CollaboratorsOverlay,
      {
        postId,
        postType,
        cursorRegistry
      }
    )
  ] });
}
export {
  CollaboratorsPresence
};
//# sourceMappingURL=index.mjs.map
