// retoor <retoor@molodetz.nl>

import { Component } from "./Component.js";
import { CodeBlock } from "../CodeBlock.js";

export class AppCode extends Component {
    connectedCallback() {
        if (this._enhanced) {
            return;
        }
        this._enhanced = true;
        let pre = this.querySelector("pre");
        if (!pre) {
            const source = this.textContent.replace(/^\n+|\s+$/g, "");
            this.textContent = "";
            pre = document.createElement("pre");
            const code = document.createElement("code");
            code.textContent = source;
            pre.appendChild(code);
            this.appendChild(pre);
        }
        const code = pre.querySelector("code");
        const language = this.attr("language");
        if (code && language) {
            code.className = `language-${language}`;
        }
        CodeBlock.enhance(pre, { lineNumbers: this.boolAttr("line-numbers") });
    }
}

customElements.define("dp-code", AppCode);
