"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_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_icons = require("@wordpress/icons");
var import_block_editor = require("@wordpress/block-editor");
var import_interface = require("@wordpress/interface");
var import_plugin_sidebar = __toESM(require("../plugin-sidebar/index.cjs"));
var import_constants = require("./constants.cjs");
var import_comments = require("./comments.cjs");
var import_store = require("../../store/index.cjs");
var import_comment_menu_item = __toESM(require("./comment-menu-item.cjs"));
var import_comment_indicator_toolbar = __toESM(require("./comment-indicator-toolbar.cjs"));
var import_global_styles_provider = require("../global-styles-provider/index.cjs");
var import_hooks = require("./hooks.cjs");
var import_utils = require("./utils.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 NotesSidebarContent({
  newNoteFormState,
  setNewNoteFormState,
  styles,
  comments,
  commentSidebarRef,
  reflowComments,
  commentLastUpdated,
  isFloating = false
}) {
  const { onCreate, onEdit, onDelete } = (0, import_hooks.useBlockCommentsActions)(reflowComments);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.__experimentalVStack,
    {
      className: "editor-collab-sidebar-panel",
      style: styles,
      role: "tree",
      spacing: "3",
      justify: "flex-start",
      ref: (node) => {
        if (node) {
          commentSidebarRef.current = node;
        }
      },
      "aria-label": isFloating ? (0, import_i18n.__)("Unresolved notes") : (0, import_i18n.__)("All notes"),
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_comments.Comments,
        {
          threads: comments,
          onEditComment: onEdit,
          onAddReply: onCreate,
          onCommentDelete: onDelete,
          newNoteFormState,
          setNewNoteFormState,
          commentSidebarRef,
          reflowComments,
          commentLastUpdated,
          isFloating
        }
      )
    }
  );
}
function NotesSidebar({ postId }) {
  const [newNoteFormState, setNewNoteFormState] = (0, import_element.useState)("closed");
  const { getActiveComplementaryArea } = (0, import_data.useSelect)(import_interface.store);
  const { enableComplementaryArea } = (0, import_data.useDispatch)(import_interface.store);
  const { toggleBlockSpotlight } = (0, import_lock_unlock.unlock)((0, import_data.useDispatch)(import_block_editor.store));
  const isLargeViewport = (0, import_compose.useViewportMatch)("medium");
  const commentSidebarRef = (0, import_element.useRef)(null);
  const showFloatingSidebar = isLargeViewport;
  const { clientId, blockCommentId, isDistractionFree } = (0, import_data.useSelect)(
    (select) => {
      const {
        getBlockAttributes,
        getSelectedBlockClientId,
        getSettings
      } = select(import_block_editor.store);
      const _clientId = getSelectedBlockClientId();
      return {
        clientId: _clientId,
        blockCommentId: _clientId ? getBlockAttributes(_clientId)?.metadata?.noteId : null,
        isDistractionFree: getSettings().isDistractionFree
      };
    },
    []
  );
  const {
    resultComments,
    unresolvedSortedThreads,
    reflowComments,
    commentLastUpdated
  } = (0, import_hooks.useBlockComments)(postId);
  (0, import_hooks.useEnableFloatingSidebar)(
    showFloatingSidebar && (unresolvedSortedThreads.length > 0 || newNoteFormState !== "closed")
  );
  const { merged: GlobalStyles } = (0, import_global_styles_provider.useGlobalStylesContext)();
  const backgroundColor = GlobalStyles?.styles?.color?.background;
  const currentThread = blockCommentId ? resultComments.find((thread) => thread.id === blockCommentId) : null;
  const showAllNotesSidebar = resultComments.length > 0;
  async function openTheSidebar() {
    const prevArea = await getActiveComplementaryArea("core");
    const activeNotesArea = import_constants.SIDEBARS.find((name) => name === prevArea);
    if (currentThread?.status === "approved") {
      enableComplementaryArea("core", import_constants.collabHistorySidebarName);
    } else if (!activeNotesArea || !showAllNotesSidebar) {
      enableComplementaryArea(
        "core",
        showFloatingSidebar ? import_constants.collabSidebarName : import_constants.collabHistorySidebarName
      );
    }
    const currentArea = await getActiveComplementaryArea("core");
    if (!import_constants.SIDEBARS.includes(currentArea)) {
      return;
    }
    setNewNoteFormState(!currentThread ? "open" : "closed");
    (0, import_utils.focusCommentThread)(
      currentThread?.id,
      commentSidebarRef.current,
      // Focus a comment thread when there's a selected block with a comment.
      !currentThread ? "textarea" : void 0
    );
    toggleBlockSpotlight(clientId, true);
  }
  if (isDistractionFree) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_comment_menu_item.default, { isDistractionFree: true });
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    !!currentThread && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_comment_indicator_toolbar.default,
      {
        thread: currentThread,
        onClick: openTheSidebar
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_comment_menu_item.default, { onClick: openTheSidebar }),
    showAllNotesSidebar && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_plugin_sidebar.default,
      {
        identifier: import_constants.collabHistorySidebarName,
        name: import_constants.collabHistorySidebarName,
        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)(
          NotesSidebarContent,
          {
            comments: resultComments,
            newNoteFormState,
            setNewNoteFormState,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated
          }
        )
      }
    ),
    isLargeViewport && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_plugin_sidebar.default,
      {
        isPinnable: false,
        header: false,
        identifier: import_constants.collabSidebarName,
        className: "editor-collab-sidebar",
        headerClassName: "editor-collab-sidebar__header",
        backgroundColor,
        children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          NotesSidebarContent,
          {
            comments: unresolvedSortedThreads,
            newNoteFormState,
            setNewNoteFormState,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated,
            styles: {
              backgroundColor
            },
            isFloating: true
          }
        )
      }
    )
  ] });
}
function NotesSidebarContainer() {
  const { postId, editorMode } = (0, import_data.useSelect)((select) => {
    const { getCurrentPostId, getEditorMode } = select(import_store.store);
    return {
      postId: getCurrentPostId(),
      editorMode: getEditorMode()
    };
  }, []);
  if (!postId || typeof postId !== "number") {
    return null;
  }
  if (editorMode === "text") {
    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
