const A = () => {
  const c = window.getSelection();
  if (c && c.rangeCount > 0) {
    let e = c.getRangeAt(0).startContainer;
    for (; e && e !== document.body; ) {
      if (e.nodeType === Node.ELEMENT_NODE) {
        const n = e;
        if (n.getAttribute("contenteditable") === "true")
          return n;
      }
      e = e.parentNode;
    }
  }
  const t = document.activeElement;
  if (t) {
    if (t.getAttribute("contenteditable") === "true")
      return t;
    const e = t.closest('[contenteditable="true"]');
    if (e) return e;
  }
  return document.querySelector('[contenteditable="true"]');
}, w = () => ({
  name: "checklist",
  // Initialize click handler when plugin is loaded
  init: () => {
    if (typeof document != "undefined" && typeof window != "undefined") {
      const c = (t) => {
        const n = t.target.closest('li[data-type="checklist-item"]');
        if (n) {
          const a = n.getBoundingClientRect();
          if (t.clientX - a.left < 32) {
            t.preventDefault(), t.stopPropagation();
            const k = n.getAttribute("data-checked") === "true";
            n.setAttribute("data-checked", (!k).toString());
          }
        }
      };
      document.addEventListener("click", c);
    }
  },
  // Schema definition for checklist nodes
  nodes: {
    checklist: {
      content: "checklistItem+",
      group: "block",
      parseDOM: [{ tag: 'ul[data-type="checklist"]' }],
      toDOM: () => ["ul", { "data-type": "checklist" }, 0]
    },
    checklistItem: {
      content: "paragraph",
      attrs: {
        checked: { default: !1 }
      },
      parseDOM: [
        {
          tag: 'li[data-type="checklist-item"]',
          getAttrs: (c) => ({
            checked: c.getAttribute("data-checked") === "true"
          })
        }
      ],
      toDOM: (c) => {
        var t;
        return [
          "li",
          {
            "data-type": "checklist-item",
            "data-checked": (t = c == null ? void 0 : c.attrs) != null && t.checked ? "true" : "false"
          },
          0
        ];
      }
    }
  },
  toolbar: [
    {
      label: "Checklist",
      command: "toggleChecklist",
      icon: '<svg width="24px" height="24px" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" fill="#000000"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"><path fill-rule="evenodd" clip-rule="evenodd" d="M3.75 4.48h-.71L2 3.43l.71-.7.69.68L4.81 2l.71.71-1.77 1.77zM6.99 3h8v1h-8V3zm0 3h8v1h-8V6zm8 3h-8v1h8V9zm-8 3h8v1h-8v-1zM3.04 7.48h.71l1.77-1.77-.71-.7L3.4 6.42l-.69-.69-.71.71 1.04 1.04zm.71 3.01h-.71L2 9.45l.71-.71.69.69 1.41-1.42.71.71-1.77 1.77zm-.71 3.01h.71l1.77-1.77-.71-.71-1.41 1.42-.69-.69-.71.7 1.04 1.05z"></path></g></svg>',
      shortcut: "Mod-Shift-9"
    }
  ],
  commands: {
    toggleChecklist: () => {
      var c, t;
      try {
        const e = A();
        if (!e) return !1;
        const n = window.getSelection();
        if (!n || n.rangeCount === 0) return !1;
        const a = n.getRangeAt(0);
        if (!e.contains(a.commonAncestorContainer)) return !1;
        const i = a.commonAncestorContainer.nodeType === Node.TEXT_NODE ? a.commonAncestorContainer.parentElement : a.commonAncestorContainer, o = (c = i == null ? void 0 : i.closest("li")) == null ? void 0 : c.closest("ul, ol");
        if (o) {
          const C = o.hasAttribute("data-type") && o.getAttribute("data-type") === "checklist", f = o.tagName.toLowerCase() === "ol", m = o.tagName.toLowerCase() === "ul" && !o.hasAttribute("data-type");
          if (C) {
            const l = o.querySelectorAll('li[data-type="checklist-item"]'), u = Array.from(l).map((r) => `<p>${r.innerHTML}</p>`).join("");
            return o.outerHTML = u, setTimeout(() => {
              const r = window.getSelection();
              if (r && e)
                try {
                  const s = e.querySelectorAll("p"), d = i == null ? void 0 : i.closest('li[data-type="checklist-item"]'), h = d ? Array.from(l).indexOf(d) : -1;
                  if (s[h] && h >= 0) {
                    const p = s[h], y = document.createRange();
                    y.setStart(p, p.childNodes.length), y.setEnd(p, p.childNodes.length), r.removeAllRanges(), r.addRange(y);
                  }
                } catch (s) {
                  console.warn("Could not restore selection after checklist removal");
                }
            }, 10), !0;
          } else if (f || m)
            return o.setAttribute("data-type", "checklist"), o.querySelectorAll("li").forEach((l) => {
              l.setAttribute("data-type", "checklist-item"), l.setAttribute("data-checked", "false");
            }), !0;
        }
        const k = a.toString().trim(), g = i == null ? void 0 : i.closest("p, div, h1, h2, h3, h4, h5, h6");
        if (g) {
          const f = `<ul data-type="checklist"><li data-type="checklist-item" data-checked="false"><p>${k || g.innerHTML}</p></li></ul>`, m = document.createElement("div");
          m.innerHTML = f;
          const l = m.firstElementChild;
          return (t = g.parentNode) == null || t.replaceChild(l, g), setTimeout(() => {
            var r;
            const u = window.getSelection();
            if (u)
              try {
                const s = l.querySelector('li[data-type="checklist-item"]');
                if (s) {
                  const d = s.querySelector("p");
                  if (d && d.firstChild) {
                    const h = document.createRange();
                    h.setStart(d.firstChild, ((r = d.textContent) == null ? void 0 : r.length) || 0), h.collapse(!0), u.removeAllRanges(), u.addRange(h);
                  }
                }
              } catch (s) {
                console.warn("Could not restore selection after checklist insertion:", s);
              }
          }, 10), !0;
        } else
          return console.warn("[ChecklistPlugin] No block element found"), !1;
        return !0;
      } catch (e) {
        return console.error("Failed to toggle checklist:", e), !1;
      }
    }
  },
  keymap: {
    "Mod-Shift-9": "toggleChecklist"
  }
});
export {
  w as ChecklistPlugin
};
//# sourceMappingURL=ChecklistPlugin.native-Dccs3nLe.mjs.map
