const O = () => ({
  name: "fontSize",
  marks: {
    fontSize: {
      attrs: {
        size: { default: null }
      },
      parseDOM: [
        {
          tag: 'span[style*="font-size"]',
          getAttrs: (e) => {
            const t = e.style.fontSize;
            return t ? { size: t } : !1;
          }
        },
        {
          tag: "font[size]",
          getAttrs: (e) => {
            const t = e.getAttribute("size");
            return t ? { size: t } : !1;
          }
        }
      ],
      toDOM: (e) => ["span", { style: `font-size: ${e.attrs?.size}` }, 0]
    }
  },
  toolbar: [
    {
      label: "Font Size",
      command: "fontSize",
      type: "group",
      items: [
        {
          label: "Decrease Font Size",
          command: "decreaseFontSize",
          icon: "−",
          type: "button"
        },
        {
          label: "Font Size",
          command: "setFontSize",
          type: "input",
          placeholder: "14"
        },
        {
          label: "Increase Font Size",
          command: "increaseFontSize",
          icon: "+",
          type: "button"
        }
      ]
    }
  ],
  commands: {
    decreaseFontSize: () => {
      try {
        return y(-2), z(), !0;
      } catch (e) {
        return console.error("Failed to decrease font size:", e), !1;
      }
    },
    increaseFontSize: () => {
      try {
        return y(2), z(), !0;
      } catch (e) {
        return console.error("Failed to increase font size:", e), !1;
      }
    },
    setFontSize: (e) => {
      if (!e) return !1;
      try {
        const t = e.trim().match(/^(\d+(?:\.\d+)?)(px|em|rem)?$/i);
        if (!t) return !1;
        const n = parseFloat(t[1]), i = t[2]?.toLowerCase() || "px";
        return i === "px" && (n < 8 || n > 72) || (i === "em" || i === "rem") && (n < 0.5 || n > 5) ? !1 : (C(n, i), !0);
      } catch (o) {
        return console.error("Failed to set font size:", o), !1;
      }
    }
  },
  keymap: {}
}), A = "p,div,li,ul,ol,table,thead,tbody,tfoot,tr,td,th,h1,h2,h3,h4,h5,h6,blockquote,pre", g = {
  "xx-small": 9,
  "x-small": 10,
  small: 13,
  medium: 16,
  large: 18,
  "x-large": 24,
  "xx-large": 32,
  "xxx-large": 48,
  smaller: 13,
  larger: 18
};
function p(e) {
  const o = e.trim().toLowerCase(), t = o.match(/^(\d+(?:\.\d+)?)(px|em|rem)$/i);
  if (t)
    return {
      value: parseFloat(t[1]),
      unit: t[2].toLowerCase()
    };
  const n = g[o];
  return n ? { value: n, unit: "px" } : null;
}
function N(e) {
  return e.trim().toLowerCase() in g;
}
function b(e) {
  return !!e.cloneContents().querySelector(A);
}
function x(e) {
  return (e.nodeType === Node.ELEMENT_NODE ? e : e.parentElement)?.closest('[contenteditable="true"]') || document.querySelector('[contenteditable="true"]');
}
function m(e) {
  e && e.dispatchEvent(new Event("input", { bubbles: !0 }));
}
function w(e, o) {
  const t = document.createElement("span");
  for (const i of Array.from(e.attributes))
    i.name === "size" || i.name === "style" || t.setAttribute(i.name, i.value);
  const n = e.getAttribute("style");
  for (n && t.setAttribute("style", n), t.style.fontSize = o; e.firstChild; )
    t.appendChild(e.firstChild);
  e.parentNode?.replaceChild(t, e);
}
function E(e, o, t, n) {
  const i = Array.from(
    e.querySelectorAll("font[size], [style*='font-size']")
  ), r = (s) => {
    if (!n || !s.isConnected) return !1;
    try {
      if (typeof n.intersectsNode == "function")
        return n.intersectsNode(s);
    } catch {
    }
    const a = document.createRange();
    return a.selectNodeContents(s), n.compareBoundaryPoints(Range.END_TO_START, a) > 0 && n.compareBoundaryPoints(Range.START_TO_END, a) < 0;
  };
  let c = !1;
  return i.forEach((s) => {
    const a = !o.has(s), u = s.tagName === "FONT", l = N(s.style.fontSize || ""), f = r(s);
    if (!(!a && !f) && !(!a && !u && !l)) {
      if (c = !0, s.tagName === "FONT") {
        w(s, t);
        return;
      }
      s.style.fontSize = t;
    }
  }), c;
}
function S(e) {
  e.querySelectorAll("span").forEach((t) => {
    t.childElementCount > 0 || (t.textContent || "").trim().length > 0 || t.remove();
  });
}
function T(e, o, t) {
  if (b(e))
    return !1;
  const n = document.createElement("span");
  n.style.fontSize = t;
  try {
    e.surroundContents(n);
  } catch {
    const r = e.extractContents();
    n.appendChild(r), e.insertNode(n);
  }
  const i = document.createRange();
  return i.selectNodeContents(n), o.removeAllRanges(), o.addRange(i), !0;
}
function y(e) {
  const o = window.getSelection();
  if (!o || o.rangeCount === 0) return;
  const { value: t, unit: n } = h();
  let i = e;
  (n === "em" || n === "rem") && (i = e * 0.125);
  let r;
  n === "px" ? r = e < 0 ? Math.max(8, t + e) : Math.min(72, t + e) : r = e < 0 ? Math.max(0.5, t + i) : Math.min(5, t + i), C(r, n);
}
function h() {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0)
    return { value: 14, unit: "px" };
  const t = e.getRangeAt(0).startContainer, n = t.nodeType === Node.TEXT_NODE ? t.parentElement : t;
  if (n) {
    const i = n.style?.fontSize;
    if (i) {
      const a = p(i);
      if (a)
        return a;
    }
    const c = window.getComputedStyle(n).fontSize, s = p(c);
    if (s)
      return s;
  }
  return { value: 14, unit: "px" };
}
function C(e, o = "px") {
  const t = window.getSelection();
  if (!t || t.rangeCount === 0) return;
  const n = t.getRangeAt(0), i = n.cloneRange(), r = x(n.commonAncestorContainer);
  if (n.collapsed)
    return;
  const c = `${e}${o}`, s = n.commonAncestorContainer, a = R(s);
  if (a && v(n, a)) {
    a.style.fontSize = c, m(r);
    const d = document.createRange();
    d.selectNodeContents(a), t.removeAllRanges(), t.addRange(d);
    return;
  }
  if (!r) return;
  const u = new Set(
    Array.from(
      r.querySelectorAll("font[size], [style*='font-size']")
    )
  );
  r.focus({ preventScroll: !0 });
  try {
    document.execCommand("styleWithCSS", !1, "true");
  } catch {
  }
  let l = document.execCommand("fontSize", !1, "7");
  const f = t.rangeCount > 0 ? t.getRangeAt(0).cloneRange() : i, F = E(
    r,
    u,
    c,
    f
  );
  if (l && F) {
    S(r), m(r);
    return;
  }
  l = T(i, t, c), l && (S(r), m(r));
}
function R(e) {
  let o = e;
  for (; o && o !== document.body; ) {
    if (o.nodeType === Node.ELEMENT_NODE) {
      const t = o;
      if (t.tagName === "SPAN" && t.style.fontSize)
        return t;
    }
    o = o.parentNode;
  }
  return null;
}
function v(e, o) {
  const t = e.startContainer, n = e.endContainer;
  return o.contains(t) && o.contains(n);
}
function z() {
  setTimeout(() => {
    const { value: e, unit: o } = h(), t = window.getSelection();
    if (!t || t.rangeCount === 0) return;
    let n = null, r = t.getRangeAt(0).startContainer;
    for (; r; ) {
      if (r instanceof HTMLElement && (r.classList.contains("rte-editor") || r.classList.contains("editora-editor") || r.hasAttribute("data-editora-editor"))) {
        n = r;
        break;
      }
      r = r.parentNode;
    }
    if (!n) return;
    const c = n.querySelector(
      ".rte-toolbar-wrapper, .editora-toolbar-container"
    ) || n, s = Array.from(
      c.querySelectorAll(
        'input[data-command="setFontSize"], input.editora-toolbar-input.font-size, input.rte-toolbar-input.font-size, input[title="Font Size"]'
      )
    );
    if (s.length === 0) return;
    const u = `${e % 1 === 0 ? e.toString() : e.toFixed(2).replace(/\.?0+$/, "")}${o}`;
    s.forEach((l) => {
      l.value = u;
    });
  }, 10);
}
export {
  O as FontSizePlugin
};
