"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/editor/src/components/collaborators-presence/index.tsx
var collaborators_presence_exports = {};
__export(collaborators_presence_exports, {
  CollaboratorsPresence: () => CollaboratorsPresence
});
module.exports = __toCommonJS(collaborators_presence_exports);
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_core_data = require("@wordpress/core-data");
var import_i18n = require("@wordpress/i18n");
var import_avatar = __toESM(require("./avatar/index.cjs"));
var import_avatar_group = __toESM(require("./avatar-group/index.cjs"));
var import_list = require("./list.cjs");
var import_lock_unlock = require("../../lock-unlock.cjs");
var import_get_avatar_url = require("../collaborators-overlay/get-avatar-url.cjs");
var import_utils = require("../collab-sidebar/utils.cjs");
var import_cursor_registry = require("../collaborators-overlay/cursor-registry.cjs");
var import_collaborators_overlay = require("../collaborators-overlay/index.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var { useActiveCollaborators } = (0, import_lock_unlock.unlock)(import_core_data.privateApis);
function CollaboratorsPresence({
  postId,
  postType
}) {
  const activeCollaborators = useActiveCollaborators(
    postId,
    postType
  );
  const otherActiveCollaborators = activeCollaborators.filter(
    (c) => !c.isMe
  );
  const collaboratorsForList = (0, import_element.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] = (0, import_element.useState)(import_cursor_registry.createCursorRegistry);
  const [isPopoverVisible, setIsPopoverVisible] = (0, import_element.useState)(false);
  const [popoverAnchor, setPopoverAnchor] = (0, import_element.useState)(
    null
  );
  if (otherActiveCollaborators.length === 0) {
    return null;
  }
  const me = activeCollaborators.find((c) => c.isMe);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "editor-collaborators-presence", children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_components.Button,
        {
          __next40pxDefaultSize: true,
          className: "editor-collaborators-presence__button",
          onClick: () => setIsPopoverVisible(!isPopoverVisible),
          isPressed: isPopoverVisible,
          ref: setPopoverAnchor,
          "aria-label": (0, import_i18n.sprintf)(
            // translators: %d: number of online collaborators.
            (0, import_i18n.__)("Collaborators list, %d online"),
            collaboratorsForList.length
          ),
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_avatar_group.default, { max: 4, children: [
            me && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_avatar.default,
              {
                src: (0, import_get_avatar_url.getAvatarUrl)(
                  me.collaboratorInfo.avatar_urls
                ),
                name: me.collaboratorInfo.name,
                borderColor: "var(--wp-admin-theme-color)",
                size: "small"
              },
              me.clientId
            ),
            otherActiveCollaborators.map(
              (collaboratorState) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_avatar.default,
                {
                  src: (0, import_get_avatar_url.getAvatarUrl)(
                    collaboratorState.collaboratorInfo.avatar_urls
                  ),
                  name: collaboratorState.collaboratorInfo.name,
                  borderColor: (0, import_utils.getAvatarBorderColor)(
                    collaboratorState.collaboratorInfo.id
                  ),
                  size: "small"
                },
                collaboratorState.clientId
              )
            )
          ] })
        }
      ),
      isPopoverVisible && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_list.CollaboratorsList,
        {
          activeCollaborators: collaboratorsForList,
          popoverAnchor,
          setIsPopoverVisible,
          cursorRegistry
        }
      )
    ] }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_collaborators_overlay.CollaboratorsOverlay,
      {
        postId,
        postType,
        cursorRegistry
      }
    )
  ] });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CollaboratorsPresence
});
//# sourceMappingURL=index.cjs.map
