"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);
var collab_sidebar_exports = {};
__export(collab_sidebar_exports, {
  default: () => CollabSidebar
});
module.exports = __toCommonJS(collab_sidebar_exports);
var import_jsx_runtime = require("react/jsx-runtime");
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"));
var import_constants = require("./constants");
var import_comments = require("./comments");
var import_add_comment = require("./add-comment");
var import_store = require("../../store");
var import_comment_menu_item = __toESM(require("./comment-menu-item"));
var import_comment_indicator_toolbar = __toESM(require("./comment-indicator-toolbar"));
var import_global_styles_provider = require("../global-styles-provider");
var import_hooks = require("./hooks");
var import_utils = require("./utils");
var import_plugin_more_menu_item = __toESM(require("../plugin-more-menu-item"));
function CollabSidebarContent({
  showCommentBoard,
  setShowCommentBoard,
  styles,
  comments,
  commentSidebarRef,
  reflowComments,
  commentLastUpdated,
  isFloating = false
}) {
  const { onCreate, onEdit, onDelete } = (0, import_hooks.useBlockCommentsActions)(reflowComments);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    import_components.__experimentalVStack,
    {
      className: "editor-collab-sidebar-panel",
      style: styles,
      role: "list",
      spacing: "3",
      justify: "flex-start",
      ref: (node) => {
        commentSidebarRef.current = node;
      },
      children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_add_comment.AddComment,
          {
            onSubmit: onCreate,
            showCommentBoard,
            setShowCommentBoard,
            commentSidebarRef
          }
        ),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_comments.Comments,
          {
            threads: comments,
            onEditComment: onEdit,
            onAddReply: onCreate,
            onCommentDelete: onDelete,
            showCommentBoard,
            setShowCommentBoard,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated,
            isFloating
          }
        )
      ]
    }
  );
}
function CollabSidebar() {
  const [showCommentBoard, setShowCommentBoard] = (0, import_element.useState)(false);
  const { getActiveComplementaryArea } = (0, import_data.useSelect)(import_interface.store);
  const { enableComplementaryArea } = (0, import_data.useDispatch)(import_interface.store);
  const isLargeViewport = (0, import_compose.useViewportMatch)("medium");
  const commentSidebarRef = (0, import_element.useRef)(null);
  const { postId } = (0, import_data.useSelect)((select) => {
    const { getCurrentPostId } = select(import_store.store);
    return {
      postId: getCurrentPostId()
    };
  }, []);
  const blockCommentId = (0, import_data.useSelect)((select) => {
    const { getBlockAttributes, getSelectedBlockClientId } = select(import_block_editor.store);
    const clientId = getSelectedBlockClientId();
    return clientId ? getBlockAttributes(clientId)?.metadata?.noteId : null;
  }, []);
  const {
    resultComments,
    unresolvedSortedThreads,
    totalPages,
    reflowComments,
    commentLastUpdated
  } = (0, import_hooks.useBlockComments)(postId);
  (0, import_hooks.useEnableFloatingSidebar)(resultComments.length > 0);
  const hasMoreComments = totalPages && totalPages > 1;
  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;
  if (!(!!postId && typeof postId === "number")) {
    return null;
  }
  async function openTheSidebar() {
    const prevArea = await getActiveComplementaryArea("core");
    const activeNotesArea = import_constants.SIDEBARS.find((name) => name === prevArea);
    if (!activeNotesArea) {
      enableComplementaryArea("core", import_constants.collabSidebarName);
    }
    const currentArea = await getActiveComplementaryArea("core");
    if (!import_constants.SIDEBARS.includes(currentArea)) {
      return;
    }
    setShowCommentBoard(!blockCommentId);
    (0, import_utils.focusCommentThread)(
      blockCommentId,
      commentSidebarRef.current,
      // Focus a comment thread when there's a selected block with a comment.
      !blockCommentId ? "textarea" : void 0
    );
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    blockCommentId && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_comment_indicator_toolbar.default,
      {
        thread: currentThread,
        hasMoreComments,
        onClick: openTheSidebar
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_comment_menu_item.default, { onClick: openTheSidebar }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_plugin_sidebar.default,
      {
        identifier: import_constants.collabHistorySidebarName,
        title: (0, import_i18n.__)("Notes"),
        icon: import_icons.comment,
        closeLabel: (0, import_i18n.__)("Close Notes"),
        children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          CollabSidebarContent,
          {
            comments: resultComments,
            showCommentBoard,
            setShowCommentBoard,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated
          }
        )
      }
    ),
    isLargeViewport && (unresolvedSortedThreads.length > 0 || showCommentBoard) && /* @__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)(
          CollabSidebarContent,
          {
            comments: unresolvedSortedThreads,
            showCommentBoard,
            setShowCommentBoard,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated,
            styles: {
              backgroundColor
            },
            isFloating: true
          }
        )
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_plugin_more_menu_item.default,
      {
        icon: import_icons.comment,
        onClick: () => enableComplementaryArea("core", import_constants.collabHistorySidebarName),
        children: (0, import_i18n.__)("Notes")
      }
    )
  ] });
}
//# sourceMappingURL=index.js.map
