"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/collab-sidebar/index.js
var collab_sidebar_exports = {};
__export(collab_sidebar_exports, {
  default: () => NotesSidebarContainer
});
module.exports = __toCommonJS(collab_sidebar_exports);
var import_i18n = require("@wordpress/i18n");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_keyboard_shortcuts = require("@wordpress/keyboard-shortcuts");
var import_icons = require("@wordpress/icons");
var import_block_editor = require("@wordpress/block-editor");
var import_interface = require("@wordpress/interface");
var import_preferences = require("@wordpress/preferences");
var import_plugin_sidebar = __toESM(require("../plugin-sidebar/index.cjs"));
var import_constants = require("./constants.cjs");
var import_notes = require("./notes.cjs");
var import_store = require("../../store/index.cjs");
var import_add_note_menu_item = require("./add-note-menu-item.cjs");
var import_note_indicator_toolbar = require("./note-indicator-toolbar.cjs");
var import_global_styles_provider = require("../global-styles-provider/index.cjs");
var import_hooks = require("./hooks.cjs");
var import_post_type_support_check = __toESM(require("../post-type-support-check/index.cjs"));
var import_lock_unlock = require("../../lock-unlock.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
function NotesSidebar({ postId }) {
  const { getActiveComplementaryArea } = (0, import_data.useSelect)(import_interface.store);
  const { enableComplementaryArea } = (0, import_data.useDispatch)(import_interface.store);
  const { toggleBlockSpotlight, selectBlock } = (0, import_lock_unlock.unlock)(
    (0, import_data.useDispatch)(import_block_editor.store)
  );
  const { selectNote } = (0, import_lock_unlock.unlock)((0, import_data.useDispatch)(import_store.store));
  const isLargeViewport = (0, import_compose.useViewportMatch)("medium");
  const sidebarRef = (0, import_element.useRef)(null);
  const { clientId, noteId, isClassicBlock } = (0, import_data.useSelect)((select) => {
    const { getBlockAttributes, getSelectedBlockClientId, getBlockName } = select(import_block_editor.store);
    const _clientId = getSelectedBlockClientId();
    return {
      clientId: _clientId,
      noteId: _clientId ? getBlockAttributes(_clientId)?.metadata?.noteId : null,
      isClassicBlock: _clientId ? getBlockName(_clientId) === "core/freeform" : false
    };
  }, []);
  const { isDistractionFree } = (0, import_data.useSelect)((select) => {
    const { get } = select(import_preferences.store);
    return {
      isDistractionFree: get("core", "distractionFree")
    };
  }, []);
  const selectedNote = (0, import_data.useSelect)(
    (select) => (0, import_lock_unlock.unlock)(select(import_store.store)).getSelectedNote(),
    []
  );
  const { notes, unresolvedNotes } = (0, import_hooks.useNoteThreads)(postId);
  const showFloatingSidebar = isLargeViewport;
  const showAllNotesSidebar = notes.length > 0 || !showFloatingSidebar;
  (0, import_hooks.useEnableFloatingSidebar)(
    showFloatingSidebar && (unresolvedNotes.length > 0 || selectedNote !== void 0)
  );
  async function focusNote({
    targetClientId,
    noteId: targetNoteId,
    isApproved
  }) {
    if (!targetClientId) {
      return;
    }
    const prevArea = await getActiveComplementaryArea("core");
    if (isApproved) {
      enableComplementaryArea("core", import_constants.ALL_NOTES_SIDEBAR);
    } else if (!import_constants.SIDEBARS.includes(prevArea) || !showAllNotesSidebar) {
      enableComplementaryArea(
        "core",
        showFloatingSidebar ? import_constants.FLOATING_NOTES_SIDEBAR : import_constants.ALL_NOTES_SIDEBAR
      );
    }
    const currentArea = await getActiveComplementaryArea("core");
    if (!import_constants.SIDEBARS.includes(currentArea)) {
      return;
    }
    selectBlock(targetClientId, null);
    toggleBlockSpotlight(targetClientId, true);
    selectNote(targetNoteId, { focus: true });
  }
  function openNoteForBlock(targetClientId) {
    const target = notes.find(
      (note) => note.blockClientId === targetClientId
    );
    return focusNote({
      targetClientId,
      noteId: target?.id ?? "new",
      isApproved: target?.status === "approved"
    });
  }
  (0, import_keyboard_shortcuts.useShortcut)(
    "core/editor/new-note",
    (event) => {
      event.preventDefault();
      openNoteForBlock(clientId);
    },
    {
      // When multiple notes per block are supported. Remove note ID check.
      // See: https://github.com/WordPress/gutenberg/pull/75147.
      isDisabled: isDistractionFree || isClassicBlock || !clientId || !!noteId
    }
  );
  const { merged: GlobalStyles } = (0, import_global_styles_provider.useGlobalStylesContext)();
  const backgroundColor = GlobalStyles?.styles?.color?.background;
  const currentThread = noteId ? notes.find((thread) => thread.id === noteId) : null;
  if (isDistractionFree) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_add_note_menu_item.AddNoteMenuItem, { isDistractionFree: true });
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    !!currentThread && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_note_indicator_toolbar.NoteAvatarIndicator,
      {
        note: currentThread,
        onClick: () => openNoteForBlock(clientId)
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_add_note_menu_item.AddNoteMenuItem,
      {
        onClick: (menuClientId) => openNoteForBlock(menuClientId)
      }
    ),
    showAllNotesSidebar && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_plugin_sidebar.default,
      {
        identifier: import_constants.ALL_NOTES_SIDEBAR,
        name: import_constants.ALL_NOTES_SIDEBAR,
        title: (0, import_i18n.__)("All notes"),
        header: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h2", { className: "interface-complementary-area-header__title", children: (0, import_i18n.__)("All notes") }),
        icon: import_icons.comment,
        closeLabel: (0, import_i18n.__)("Close Notes"),
        children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_notes.Notes, { notes, sidebarRef })
      }
    ),
    isLargeViewport && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_plugin_sidebar.default,
      {
        isPinnable: false,
        header: false,
        identifier: import_constants.FLOATING_NOTES_SIDEBAR,
        className: "editor-collab-sidebar",
        headerClassName: "editor-collab-sidebar__header",
        backgroundColor,
        children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_notes.Notes,
          {
            notes: unresolvedNotes,
            sidebarRef,
            styles: { backgroundColor },
            isFloating: true
          }
        )
      }
    )
  ] });
}
function NotesSidebarContainer() {
  const { postId, editorMode, revisionsMode } = (0, import_data.useSelect)((select) => {
    const { getCurrentPostId, getEditorMode, isRevisionsMode } = (0, import_lock_unlock.unlock)(
      select(import_store.store)
    );
    return {
      postId: getCurrentPostId(),
      editorMode: getEditorMode(),
      revisionsMode: isRevisionsMode()
    };
  }, []);
  if (!postId || typeof postId !== "number") {
    return null;
  }
  if (editorMode === "text" || revisionsMode) {
    return null;
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_post_type_support_check.default, { supportKeys: "editor.notes", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(NotesSidebar, { postId }) });
}
//# sourceMappingURL=index.cjs.map
