// packages/editor/src/components/collab-sidebar/index.js
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 { 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 PostTypeSupportCheck from "../post-type-support-check";
import { unlock } from "../../lock-unlock";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function NotesSidebarContent({
  newNoteFormState,
  setNewNoteFormState,
  styles,
  comments,
  commentSidebarRef,
  reflowComments,
  commentLastUpdated,
  isFloating = false
}) {
  const { onCreate, onEdit, onDelete } = useBlockCommentsActions(reflowComments);
  return /* @__PURE__ */ jsx(
    VStack,
    {
      className: "editor-collab-sidebar-panel",
      style: styles,
      role: "tree",
      spacing: "3",
      justify: "flex-start",
      ref: (node) => {
        if (node) {
          commentSidebarRef.current = node;
        }
      },
      "aria-label": isFloating ? __("Unresolved notes") : __("All notes"),
      children: /* @__PURE__ */ jsx(
        Comments,
        {
          threads: comments,
          onEditComment: onEdit,
          onAddReply: onCreate,
          onCommentDelete: onDelete,
          newNoteFormState,
          setNewNoteFormState,
          commentSidebarRef,
          reflowComments,
          commentLastUpdated,
          isFloating
        }
      )
    }
  );
}
function NotesSidebar({ postId, mode }) {
  const [newNoteFormState, setNewNoteFormState] = useState("closed");
  const { getActiveComplementaryArea } = useSelect(interfaceStore);
  const { enableComplementaryArea } = useDispatch(interfaceStore);
  const { toggleBlockSpotlight } = unlock(useDispatch(blockEditorStore));
  const isLargeViewport = useViewportMatch("medium");
  const commentSidebarRef = useRef(null);
  const showFloatingSidebar = isLargeViewport && mode === "post-only";
  const { clientId, blockCommentId, isDistractionFree } = useSelect(
    (select) => {
      const {
        getBlockAttributes,
        getSelectedBlockClientId,
        getSettings
      } = select(blockEditorStore);
      const _clientId = getSelectedBlockClientId();
      return {
        clientId: _clientId,
        blockCommentId: _clientId ? getBlockAttributes(_clientId)?.metadata?.noteId : null,
        isDistractionFree: getSettings().isDistractionFree
      };
    },
    []
  );
  const {
    resultComments,
    unresolvedSortedThreads,
    reflowComments,
    commentLastUpdated
  } = useBlockComments(postId);
  useEnableFloatingSidebar(
    showFloatingSidebar && (unresolvedSortedThreads.length > 0 || newNoteFormState !== "closed")
  );
  const { merged: GlobalStyles } = useGlobalStylesContext();
  const backgroundColor = GlobalStyles?.styles?.color?.background;
  const currentThread = blockCommentId ? resultComments.find((thread) => thread.id === blockCommentId) : null;
  const showAllNotesSidebar = resultComments.length > 0 || !showFloatingSidebar;
  async function openTheSidebar() {
    const prevArea = await getActiveComplementaryArea("core");
    const activeNotesArea = SIDEBARS.find((name) => name === prevArea);
    if (currentThread?.status === "approved") {
      enableComplementaryArea("core", collabHistorySidebarName);
    } else if (!activeNotesArea || !showAllNotesSidebar) {
      enableComplementaryArea(
        "core",
        showFloatingSidebar ? collabSidebarName : collabHistorySidebarName
      );
    }
    const currentArea = await getActiveComplementaryArea("core");
    if (!SIDEBARS.includes(currentArea)) {
      return;
    }
    setNewNoteFormState(!currentThread ? "open" : "closed");
    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__ */ jsx(AddCommentMenuItem, { isDistractionFree: true });
  }
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    !!currentThread && /* @__PURE__ */ jsx(
      CommentAvatarIndicator,
      {
        thread: currentThread,
        onClick: openTheSidebar
      }
    ),
    /* @__PURE__ */ jsx(AddCommentMenuItem, { onClick: openTheSidebar }),
    showAllNotesSidebar && /* @__PURE__ */ jsx(
      PluginSidebar,
      {
        identifier: collabHistorySidebarName,
        name: collabHistorySidebarName,
        title: __("All notes"),
        header: /* @__PURE__ */ jsx("h2", { className: "interface-complementary-area-header__title", children: __("All notes") }),
        icon: commentIcon,
        closeLabel: __("Close Notes"),
        children: /* @__PURE__ */ jsx(
          NotesSidebarContent,
          {
            comments: resultComments,
            newNoteFormState,
            setNewNoteFormState,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated
          }
        )
      }
    ),
    isLargeViewport && /* @__PURE__ */ jsx(
      PluginSidebar,
      {
        isPinnable: false,
        header: false,
        identifier: collabSidebarName,
        className: "editor-collab-sidebar",
        headerClassName: "editor-collab-sidebar__header",
        backgroundColor,
        children: /* @__PURE__ */ jsx(
          NotesSidebarContent,
          {
            comments: unresolvedSortedThreads,
            newNoteFormState,
            setNewNoteFormState,
            commentSidebarRef,
            reflowComments,
            commentLastUpdated,
            styles: {
              backgroundColor
            },
            isFloating: true
          }
        )
      }
    )
  ] });
}
function NotesSidebarContainer() {
  const { postId, mode, editorMode } = useSelect((select) => {
    const { getCurrentPostId, getRenderingMode, getEditorMode } = select(editorStore);
    return {
      postId: getCurrentPostId(),
      mode: getRenderingMode(),
      editorMode: getEditorMode()
    };
  }, []);
  if (!postId || typeof postId !== "number") {
    return null;
  }
  if (editorMode === "text") {
    return null;
  }
  return /* @__PURE__ */ jsx(PostTypeSupportCheck, { supportKeys: "editor.notes", children: /* @__PURE__ */ jsx(NotesSidebar, { postId, mode }) });
}
export {
  NotesSidebarContainer as default
};
//# sourceMappingURL=index.js.map
