import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { __ } from "@wordpress/i18n";
import { useSelect, useDispatch } from "@wordpress/data";
import { __experimentalVStack as VStack } from "@wordpress/components";
import { useState, useRef } from "@wordpress/element";
import { useViewportMatch } from "@wordpress/compose";
import { comment as commentIcon } from "@wordpress/icons";
import { store as blockEditorStore } from "@wordpress/block-editor";
import { store as interfaceStore } from "@wordpress/interface";
import PluginSidebar from "../plugin-sidebar";
import {
  collabHistorySidebarName,
  collabSidebarName,
  SIDEBARS
} from "./constants";
import { Comments } from "./comments";
import { AddComment } from "./add-comment";
import { store as editorStore } from "../../store";
import AddCommentMenuItem from "./comment-menu-item";
import CommentAvatarIndicator from "./comment-indicator-toolbar";
import { useGlobalStylesContext } from "../global-styles-provider";
import {
  useBlockComments,
  useBlockCommentsActions,
  useEnableFloatingSidebar
} from "./hooks";
import { focusCommentThread } from "./utils";
import PluginMoreMenuItem from "../plugin-more-menu-item";
function CollabSidebarContent({
  showCommentBoard,
  setShowCommentBoard,
  styles,
  comments,
  commentSidebarRef,
  reflowComments,
  commentLastUpdated,
  isFloating = false
}) {
  const { onCreate, onEdit, onDelete } = useBlockCommentsActions(reflowComments);
  return /* @__PURE__ */ jsxs(
    VStack,
    {
      className: "editor-collab-sidebar-panel",
      style: styles,
      role: "list",
      spacing: "3",
      justify: "flex-start",
      ref: (node) => {
        commentSidebarRef.current = node;
      },
      children: [
        /* @__PURE__ */ jsx(
          AddComment,
          {
            onSubmit: onCreate,
            showCommentBoard,
            setShowCommentBoard,
            commentSidebarRef
          }
        ),
        /* @__PURE__ */ jsx(
          Comments,
          {
            threads: comments,
            onEditComment: onEdit,
            onAddReply: onCreate,
            onCommentDelete: onDelete,
            showCommentBoard,
            setShowCommentBoard,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated,
            isFloating
          }
        )
      ]
    }
  );
}
function CollabSidebar() {
  const [showCommentBoard, setShowCommentBoard] = useState(false);
  const { getActiveComplementaryArea } = useSelect(interfaceStore);
  const { enableComplementaryArea } = useDispatch(interfaceStore);
  const isLargeViewport = useViewportMatch("medium");
  const commentSidebarRef = useRef(null);
  const { postId } = useSelect((select) => {
    const { getCurrentPostId } = select(editorStore);
    return {
      postId: getCurrentPostId()
    };
  }, []);
  const blockCommentId = useSelect((select) => {
    const { getBlockAttributes, getSelectedBlockClientId } = select(blockEditorStore);
    const clientId = getSelectedBlockClientId();
    return clientId ? getBlockAttributes(clientId)?.metadata?.noteId : null;
  }, []);
  const {
    resultComments,
    unresolvedSortedThreads,
    totalPages,
    reflowComments,
    commentLastUpdated
  } = useBlockComments(postId);
  useEnableFloatingSidebar(resultComments.length > 0);
  const hasMoreComments = totalPages && totalPages > 1;
  const { merged: GlobalStyles } = 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 = SIDEBARS.find((name) => name === prevArea);
    if (!activeNotesArea) {
      enableComplementaryArea("core", collabSidebarName);
    }
    const currentArea = await getActiveComplementaryArea("core");
    if (!SIDEBARS.includes(currentArea)) {
      return;
    }
    setShowCommentBoard(!blockCommentId);
    focusCommentThread(
      blockCommentId,
      commentSidebarRef.current,
      // Focus a comment thread when there's a selected block with a comment.
      !blockCommentId ? "textarea" : void 0
    );
  }
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    blockCommentId && /* @__PURE__ */ jsx(
      CommentAvatarIndicator,
      {
        thread: currentThread,
        hasMoreComments,
        onClick: openTheSidebar
      }
    ),
    /* @__PURE__ */ jsx(AddCommentMenuItem, { onClick: openTheSidebar }),
    /* @__PURE__ */ jsx(
      PluginSidebar,
      {
        identifier: collabHistorySidebarName,
        title: __("Notes"),
        icon: commentIcon,
        closeLabel: __("Close Notes"),
        children: /* @__PURE__ */ jsx(
          CollabSidebarContent,
          {
            comments: resultComments,
            showCommentBoard,
            setShowCommentBoard,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated
          }
        )
      }
    ),
    isLargeViewport && (unresolvedSortedThreads.length > 0 || showCommentBoard) && /* @__PURE__ */ jsx(
      PluginSidebar,
      {
        isPinnable: false,
        header: false,
        identifier: collabSidebarName,
        className: "editor-collab-sidebar",
        headerClassName: "editor-collab-sidebar__header",
        backgroundColor,
        children: /* @__PURE__ */ jsx(
          CollabSidebarContent,
          {
            comments: unresolvedSortedThreads,
            showCommentBoard,
            setShowCommentBoard,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated,
            styles: {
              backgroundColor
            },
            isFloating: true
          }
        )
      }
    ),
    /* @__PURE__ */ jsx(
      PluginMoreMenuItem,
      {
        icon: commentIcon,
        onClick: () => enableComplementaryArea("core", collabHistorySidebarName),
        children: __("Notes")
      }
    )
  ] });
}
export {
  CollabSidebar as default
};
//# sourceMappingURL=index.js.map
