import { Node as d, textblockTypeInputRule as c, mergeAttributes as p } from "../../../../../@tiptap_core@2.14.0_@tiptap_pm@2.14.0/node_modules/@tiptap/core/dist/index.js";
import { Plugin as f, PluginKey as g, TextSelection as m, Selection as h } from "../../../../../prosemirror-state@1.4.3/node_modules/prosemirror-state/dist/index.js";
const x = /^```([a-z]+)?[\s\n]$/, y = /^~~~([a-z]+)?[\s\n]$/, C = d.create({
  name: "codeBlock",
  addOptions() {
    return {
      languageClassPrefix: "language-",
      exitOnTripleEnter: !0,
      exitOnArrowDown: !0,
      defaultLanguage: null,
      HTMLAttributes: {}
    };
  },
  content: "text*",
  marks: "",
  group: "block",
  code: !0,
  defining: !0,
  addAttributes() {
    return {
      language: {
        default: this.options.defaultLanguage,
        parseHTML: (e) => {
          var t;
          const { languageClassPrefix: s } = this.options, o = [...((t = e.firstElementChild) === null || t === void 0 ? void 0 : t.classList) || []].filter((n) => n.startsWith(s)).map((n) => n.replace(s, ""))[0];
          return o || null;
        },
        rendered: !1
      }
    };
  },
  parseHTML() {
    return [
      {
        tag: "pre",
        preserveWhitespace: "full"
      }
    ];
  },
  renderHTML({ node: e, HTMLAttributes: t }) {
    return [
      "pre",
      p(this.options.HTMLAttributes, t),
      [
        "code",
        {
          class: e.attrs.language ? this.options.languageClassPrefix + e.attrs.language : null
        },
        0
      ]
    ];
  },
  addCommands() {
    return {
      setCodeBlock: (e) => ({ commands: t }) => t.setNode(this.name, e),
      toggleCodeBlock: (e) => ({ commands: t }) => t.toggleNode(this.name, "paragraph", e)
    };
  },
  addKeyboardShortcuts() {
    return {
      "Mod-Alt-c": () => this.editor.commands.toggleCodeBlock(),
      // remove code block when at start of document or code block is empty
      Backspace: () => {
        const { empty: e, $anchor: t } = this.editor.state.selection, s = t.pos === 1;
        return !e || t.parent.type.name !== this.name ? !1 : s || !t.parent.textContent.length ? this.editor.commands.clearNodes() : !1;
      },
      // exit node on triple enter
      Enter: ({ editor: e }) => {
        if (!this.options.exitOnTripleEnter)
          return !1;
        const { state: t } = e, { selection: s } = t, { $from: r, empty: a } = s;
        if (!a || r.parent.type !== this.type)
          return !1;
        const o = r.parentOffset === r.parent.nodeSize - 2, n = r.parent.textContent.endsWith(`

`);
        return !o || !n ? !1 : e.chain().command(({ tr: i }) => (i.delete(r.pos - 2, r.pos), !0)).exitCode().run();
      },
      // exit node on arrow down
      ArrowDown: ({ editor: e }) => {
        if (!this.options.exitOnArrowDown)
          return !1;
        const { state: t } = e, { selection: s, doc: r } = t, { $from: a, empty: o } = s;
        if (!o || a.parent.type !== this.type || !(a.parentOffset === a.parent.nodeSize - 2))
          return !1;
        const i = a.after();
        return i === void 0 ? !1 : r.nodeAt(i) ? e.commands.command(({ tr: u }) => (u.setSelection(h.near(r.resolve(i))), !0)) : e.commands.exitCode();
      }
    };
  },
  addInputRules() {
    return [
      c({
        find: x,
        type: this.type,
        getAttributes: (e) => ({
          language: e[1]
        })
      }),
      c({
        find: y,
        type: this.type,
        getAttributes: (e) => ({
          language: e[1]
        })
      })
    ];
  },
  addProseMirrorPlugins() {
    return [
      // this plugin creates a code block for pasted content from VS Code
      // we can also detect the copied code language
      new f({
        key: new g("codeBlockVSCodeHandler"),
        props: {
          handlePaste: (e, t) => {
            if (!t.clipboardData || this.editor.isActive(this.type.name))
              return !1;
            const s = t.clipboardData.getData("text/plain"), r = t.clipboardData.getData("vscode-editor-data"), a = r ? JSON.parse(r) : void 0, o = a?.mode;
            if (!s || !o)
              return !1;
            const { tr: n, schema: i } = e.state, l = i.text(s.replace(/\r\n?/g, `
`));
            return n.replaceSelectionWith(this.type.create({ language: o }, l)), n.selection.$from.parent.type !== this.type && n.setSelection(m.near(n.doc.resolve(Math.max(0, n.selection.from - 2)))), n.setMeta("paste", !0), e.dispatch(n), !0;
          }
        }
      })
    ];
  }
});
export {
  C as CodeBlock,
  x as backtickInputRegex,
  C as default,
  y as tildeInputRegex
};
//# sourceMappingURL=index.js.map
