import { h } from "@stencil/core";
import { MAX_TEXT_LENGTH, extractReplyBlock, parseRichText, stripOutReplyBlock, } from "../../utils/chat";
export class RtkMarkdownView {
    constructor() {
        /** max length of text to render as markdown */
        this.maxLength = MAX_TEXT_LENGTH;
        this.restoreEmpty = (content, tag, renderCallback) => {
            return content.trim().length === 0 ? `${tag}${content}${tag}` : renderCallback(content);
        };
        this.renderLink = (content) => {
            return (h("a", { class: "link", href: content, target: "_blank", rel: "noopener noreferrer" }, content));
        };
        this.renderBold = (content) => {
            if (typeof content === 'string') {
                return this.restoreEmpty(content, '*', (c) => h("b", null, c));
            }
            return h("b", null, this.renderTokens(content));
        };
        this.renderItalic = (content) => {
            if (typeof content === 'string') {
                return this.restoreEmpty(content, '_', (c) => h("i", null, c));
            }
            return h("i", null, this.renderTokens(content));
        };
        this.renderStrikethrough = (content) => {
            if (typeof content === 'string') {
                return this.restoreEmpty(content, '~', (c) => h("s", null, c));
            }
            return h("b", null, this.renderTokens(content));
        };
        this.renderPlainText = (content) => {
            if (typeof content === 'string') {
                return content;
            }
            return h("p", null, this.renderTokens(content));
        };
        this.renderTokens = (tokens) => {
            return tokens.map((token) => {
                switch (token.type) {
                    case 'a':
                        if (typeof token.content === 'string') {
                            return this.renderLink(token.content);
                        }
                    case 'b':
                        return this.renderBold(token.content);
                    case 'i':
                        return this.renderItalic(token.content);
                    case 's':
                        return this.renderStrikethrough(token.content);
                    case 'q':
                        return h("span", { class: "block-quote" });
                    case 'plain_text':
                    default:
                        return this.renderPlainText(token.content);
                }
            });
        };
    }
    renderMessage(text) {
        let lines = text.split('\n');
        let isCodeBlock = false;
        if (lines[0] === '```' && lines[lines.length - 1] === '```') {
            isCodeBlock = true;
            lines = lines.slice(1, -1);
        }
        const message = lines.map((line) => {
            const tokens = parseRichText(line);
            return h("p", null, this.renderTokens(tokens));
        });
        if (isCodeBlock) {
            return h("pre", { style: { whiteSpace: 'pre', overflow: 'scroll' } }, lines.join('\n'));
        }
        return message;
    }
    render() {
        const slicedMessage = this.text.slice(0, this.maxLength);
        const withReply = extractReplyBlock(slicedMessage, true);
        const withoutReply = stripOutReplyBlock(slicedMessage);
        return (h("p", { key: 'ff1a72f01b0cf274f0135874dade41d08771b6af', class: "markdown-message" }, withReply.length !== 0 && h("blockquote", { key: '35f779d8e7aa8f4ea44f16b2d35094cb9740a3f5' }, this.renderMessage(withReply)), withoutReply.length !== 0 && this.renderMessage(withoutReply)));
    }
    static get is() { return "rtk-markdown-view"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["rtk-markdown-view.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["rtk-markdown-view.css"]
        };
    }
    static get properties() {
        return {
            "text": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "raw text to render as markdown"
                },
                "getter": false,
                "setter": false,
                "attribute": "text",
                "reflect": false
            },
            "maxLength": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "max length of text to render as markdown"
                },
                "getter": false,
                "setter": false,
                "attribute": "max-length",
                "reflect": false,
                "defaultValue": "MAX_TEXT_LENGTH"
            }
        };
    }
}
