"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/note.js
var note_exports = {};
__export(note_exports, {
  Note: () => Note
});
module.exports = __toCommonJS(note_exports);
var import_clsx = __toESM(require("clsx"));
var import_element = require("@wordpress/element");
var import_components = require("@wordpress/components");
var import_i18n = require("@wordpress/i18n");
var import_icons = require("@wordpress/icons");
var import_note_card = require("./note-card.cjs");
var import_note_form = require("./note-form.cjs");
var import_lock_unlock = require("../../lock-unlock.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var { Menu } = (0, import_lock_unlock.unlock)(import_components.privateApis);
function NoteActionsMenu({ items, buttonRef }) {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Menu, { placement: "bottom-end", children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      Menu.TriggerButton,
      {
        render: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_components.Button,
          {
            ref: buttonRef,
            size: "small",
            icon: import_icons.moreVertical,
            label: (0, import_i18n.__)("Actions"),
            disabled: !items.length,
            accessibleWhenDisabled: true
          }
        )
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      Menu.Popover,
      {
        modal: false,
        children: items.map((item) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Menu.Item, { onClick: item.onClick, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Menu.ItemLabel, { children: item.title }) }, item.id))
      }
    )
  ] });
}
function Note({
  note,
  parentNote,
  isSelected,
  onEditNote,
  onDeleteNote,
  onResolve
}) {
  const [actionState, setActionState] = (0, import_element.useState)(null);
  const actionButtonRef = (0, import_element.useRef)(null);
  const canResolve = note.parent === 0;
  const isResolutionNote = note.type === "note" && note.meta && (note.meta._wp_note_status === "resolved" || note.meta._wp_note_status === "reopen");
  const menuItems = [
    {
      id: "edit",
      title: (0, import_i18n.__)("Edit"),
      isEligible: ({ status }) => status !== "approved",
      onClick: () => setActionState("edit")
    },
    {
      id: "reopen",
      title: (0, import_i18n._x)("Reopen", "Reopen note"),
      isEligible: ({ status }) => status === "approved",
      onClick: () => onEditNote({ id: note.id, status: "hold" })
    },
    {
      id: "delete",
      title: (0, import_i18n.__)("Delete"),
      isEligible: () => true,
      onClick: () => setActionState("delete")
    }
  ];
  const availableItems = parentNote?.status !== "approved" ? menuItems.filter((item) => item.isEligible(note)) : [];
  const deleteConfirmMessage = note.parent === 0 ? (0, import_i18n.__)(
    "Are you sure you want to delete this note? This will also delete all of this note's replies."
  ) : (0, import_i18n.__)("Are you sure you want to delete this reply?");
  const handleCancel = () => {
    setActionState(null);
    actionButtonRef.current?.focus();
  };
  let body;
  if (actionState === "edit") {
    body = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_note_form.NoteForm,
      {
        onSubmit: (value) => {
          onEditNote({ id: note.id, content: value });
          setActionState(null);
          actionButtonRef.current?.focus();
        },
        onCancel: handleCancel,
        note,
        labels: {
          submit: (0, import_i18n._x)("Update", "verb"),
          input: (0, import_i18n.sprintf)(
            // translators: %1$s: note identifier, %2$s: author name.
            (0, import_i18n.__)("Edit note %1$s by %2$s"),
            note.id,
            note.author_name
          )
        }
      }
    );
  } else if (isResolutionNote) {
    const actionText = note.meta._wp_note_status === "resolved" ? (0, import_i18n.__)("Marked as resolved") : (0, import_i18n.__)("Reopened");
    const raw = note?.content?.raw;
    const text = raw && typeof raw === "string" && raw.trim() !== "" ? (0, import_i18n.sprintf)(
      // translators: %1$s: action label ("Marked as resolved" or "Reopened"); %2$s: note text.
      (0, import_i18n.__)("%1$s: %2$s"),
      actionText,
      raw
    ) : actionText;
    body = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_element.RawHTML,
      {
        className: (0, import_clsx.default)(
          "editor-collab-sidebar-panel__note-content",
          "editor-collab-sidebar-panel__resolution-text"
        ),
        children: text
      }
    );
  } else {
    body = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_element.RawHTML, { className: "editor-collab-sidebar-panel__note-content", children: note?.content?.rendered });
  }
  const actions = isSelected ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    canResolve && onResolve && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_components.Button,
      {
        label: (0, import_i18n._x)("Resolve", "Mark note as resolved"),
        size: "small",
        icon: import_icons.published,
        disabled: note.status === "approved",
        accessibleWhenDisabled: note.status === "approved",
        onClick: onResolve
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      NoteActionsMenu,
      {
        items: availableItems,
        buttonRef: actionButtonRef
      }
    )
  ] }) : null;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    import_note_card.NoteCard,
    {
      note,
      actions,
      role: note.parent !== 0 ? "treeitem" : void 0,
      children: [
        body,
        actionState === "delete" && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_components.__experimentalConfirmDialog,
          {
            isOpen: true,
            onConfirm: () => {
              onDeleteNote(note);
              setActionState(null);
            },
            onCancel: handleCancel,
            confirmButtonText: (0, import_i18n.__)("Delete"),
            children: deleteConfirmMessage
          }
        )
      ]
    }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Note
});
//# sourceMappingURL=note.cjs.map
