import { n as a } from "./utils-CIKuP89W.js";
import { t as c } from "./theme-BarViDe0.js";
const r = [
  "plaintext",
  "json",
  "yaml",
  "css"
], l = {
  plaintext: () => import("./plaintext-t5CGmSc3.js"),
  json: () => import("./json-Bh-SPdZH.js"),
  yaml: () => import("./yaml-CGptevkk.js"),
  css: () => import("./css-CrqZ87Qb.js")
}, d = async (t) => {
  if (!r.includes(t))
    return (await l.plaintext()).default;
  const e = await l[t]();
  return e ? e.default : (await l.plaintext()).default;
};
var h = class extends HTMLElement {
  static observedAttributes = [
    "content",
    "lang",
    "readonly"
  ];
  constructor() {
    super();
  }
  value = "";
  codeEl = null;
  styleEl = null;
  applyHighlights = () => {
  };
  async connectedCallback() {
    await this.prepare(), setTimeout(async () => {
      await this.refresh();
    }, 1);
  }
  async attributeChangedCallback(t, e) {
    t !== e && await this.refresh();
  }
  async prepare() {
    const t = this.shadowRoot || this.attachShadow({ mode: "open" });
    t.innerHTML = "";
    const e = document.createElement("style");
    e.textContent = c, t.appendChild(e);
    const n = document.createElement("pre"), s = document.createElement("code"), o = this.hasAttribute("readonly") ? "false" : "plaintext-only";
    s.setAttribute("contenteditable", o), s.setAttribute("spellcheck", "false"), t.appendChild(n), n.appendChild(s), s.addEventListener("input", () => {
      this.codeEl.normalize(), this.value = this.codeEl.textContent ?? "", this.applyHighlights();
    }), this.codeEl = s, this.styleEl = e;
  }
  async refresh() {
    if (!this.codeEl) return;
    const t = await d(this.getAttribute("lang") ?? "plaintext"), e = this.getAttribute("content") ?? "";
    if (this.value = e, this.codeEl.textContent = t.format ? t.format(e) : e, !this.codeEl.childNodes?.[0]) return;
    const n = window.crypto.randomUUID().substring(0, 6), s = a(t.definitions, this.codeEl, n), o = s.map((i) => i.css).join(" ");
    this.styleEl.textContent += `${o}`, this.applyHighlights = () => {
      s.forEach((i) => {
        i.apply();
      });
    }, this.applyHighlights();
  }
};
customElements.get("monogon-code") || customElements.define("monogon-code", h);
