"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-overlay/overlay.tsx
var overlay_exports = {};
__export(overlay_exports, {
  Overlay: () => Overlay
});
module.exports = __toCommonJS(overlay_exports);
var import_compose = require("@wordpress/compose");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_avatar = __toESM(require("../collaborators-presence/avatar/index.cjs"));
var import_avatar_iframe_styles = require("./avatar-iframe-styles.cjs");
var import_overlay_iframe_styles = require("./overlay-iframe-styles.cjs");
var import_timing_utils = require("./timing-utils.cjs");
var import_use_block_highlighting = require("./use-block-highlighting.cjs");
var import_use_render_cursors = require("./use-render-cursors.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var RERENDER_DELAY_MS = 500;
var CURSOR_REDRAW_INTERVAL_MS = 1e4;
function Overlay({
  blockEditorDocument,
  postId,
  postType,
  cursorRegistry
}) {
  const [overlayElement, setOverlayElement] = (0, import_element.useState)(null);
  const { cursors, rerenderCursorsAfterDelay } = (0, import_use_render_cursors.useRenderCursors)(
    overlayElement,
    blockEditorDocument ?? null,
    postId ?? null,
    postType ?? null,
    RERENDER_DELAY_MS
  );
  const { highlights, rerenderHighlightsAfterDelay } = (0, import_use_block_highlighting.useBlockHighlighting)(
    overlayElement,
    blockEditorDocument ?? null,
    postId ?? null,
    postType ?? null,
    RERENDER_DELAY_MS
  );
  const onResize = (0, import_element.useCallback)(() => {
    rerenderCursorsAfterDelay();
    rerenderHighlightsAfterDelay();
  }, [rerenderCursorsAfterDelay, rerenderHighlightsAfterDelay]);
  const resizeObserverRef = (0, import_compose.useResizeObserver)(onResize);
  (0, import_element.useEffect)(() => {
    const cleanupCursors = rerenderCursorsAfterDelay();
    const cleanupHighlights = rerenderHighlightsAfterDelay();
    return () => {
      cleanupCursors();
      cleanupHighlights();
    };
  }, [rerenderCursorsAfterDelay, rerenderHighlightsAfterDelay]);
  (0, import_element.useEffect)(() => {
    if (cursors.length === 0) {
      return;
    }
    return (0, import_timing_utils.setDelayedInterval)(
      rerenderCursorsAfterDelay,
      CURSOR_REDRAW_INTERVAL_MS
    );
  }, [cursors.length, rerenderCursorsAfterDelay]);
  const mergedRef = (0, import_compose.useMergeRefs)([
    setOverlayElement,
    resizeObserverRef
  ]);
  const cursorRefsMap = (0, import_element.useRef)(/* @__PURE__ */ new Map());
  (0, import_element.useEffect)(() => {
    if (!cursorRegistry) {
      return;
    }
    const refs = cursorRefsMap.current;
    const currentIds = new Set(cursors.map((c) => c.clientId));
    for (const id of refs.keys()) {
      if (!currentIds.has(id)) {
        cursorRegistry.unregisterCursor(id);
        refs.delete(id);
      }
    }
    for (const [id, el] of refs.entries()) {
      cursorRegistry.registerCursor(id, el);
    }
    return () => cursorRegistry.removeAll();
  }, [cursors, cursorRegistry]);
  const setCursorRef = (0, import_element.useCallback)(
    (clientId) => (el) => {
      if (el) {
        cursorRefsMap.current.set(clientId, el);
      } else {
        cursorRefsMap.current.delete(clientId);
      }
    },
    []
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "collaborators-overlay-full", ref: mergedRef, children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)("style", { children: import_avatar_iframe_styles.AVATAR_IFRAME_STYLES + import_overlay_iframe_styles.OVERLAY_IFRAME_STYLES }),
    cursors.map((cursor) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
      !cursor.isMe && cursor.selectionRects?.map((rect, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        "div",
        {
          className: "collaborators-overlay-selection-rect",
          style: {
            left: `${rect.x}px`,
            top: `${rect.y}px`,
            width: `${rect.width}px`,
            height: `${rect.height}px`,
            backgroundColor: cursor.color
          }
        },
        `${cursor.clientId}-sel-${index}`
      )),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
        "div",
        {
          ref: setCursorRef(cursor.clientId),
          className: "collaborators-overlay-user",
          style: {
            left: `${cursor.x}px`,
            top: `${cursor.y}px`
          },
          children: [
            !cursor.isMe && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              "div",
              {
                className: "collaborators-overlay-user-cursor",
                style: {
                  backgroundColor: cursor.color,
                  height: `${cursor.height}px`
                }
              }
            ),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_avatar.default,
              {
                className: "collaborators-overlay-user-label",
                variant: "badge",
                size: "small",
                src: cursor.avatarUrl,
                name: cursor.userName,
                label: cursor.isMe ? (0, import_i18n.__)("You") : void 0,
                borderColor: cursor.color
              }
            )
          ]
        }
      )
    ] }, cursor.clientId)),
    highlights.map((highlight) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_avatar.default,
      {
        className: "collaborators-overlay-block-label",
        variant: "badge",
        size: "small",
        src: highlight.avatarUrl,
        name: highlight.userName,
        borderColor: highlight.color,
        style: {
          left: `${highlight.x}px`,
          top: `${highlight.y}px`
        }
      },
      highlight.blockId
    ))
  ] });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Overlay
});
//# sourceMappingURL=overlay.cjs.map
