"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hljs = require("highlight.js");
const JSON5 = require("json5");
const message_1 = require("./message");
function isErrorComponent(cmp) {
    return cmp && typeof cmp.message === 'string' && typeof cmp.stack === 'string';
}
function stringify(cmp) {
    if (isErrorComponent(cmp)) {
        return `Error: ${cmp.message}`;
    }
    if (cmp == null) {
        return `<span class="hljs-keyword">${cmp}</span>`;
    }
    switch (typeof cmp) {
        case 'string':
            return cmp;
        case 'number':
            return `<span class="hljs-number">${cmp}</span>`;
        case 'boolean':
            return `<span class="hljs-keyword">${cmp}</span>`;
        default:
            return hljs.highlight('js', JSON5.stringify(cmp)).value;
    }
}
/**
 * ErrorMessage is a special message that's used to format uncaught errors
 * from the controls.
 */
class LogMessage {
    constructor({ level, params }) {
        /**
         * @override
         */
        this.kind = message_1.Message.Log;
        /**
         * @override
         */
        this.id = String(Math.random());
        this.error = params.find(isErrorComponent);
        if (this.error && params.length > 1) {
            params = params.map(p => (p === this.error ? 'Error: ...' : p));
        }
        this.text = params.map(stringify).join(' ');
        this.level = level;
    }
    /**
     * @override
     */
    expansion() {
        let content = `<pre><code class="hljs">${this.text}`;
        if (this.error) {
            content += `\n\n${this.error.stack}`;
            if (this.error.metadata) {
                const { value } = hljs.highlight('js', JSON5.stringify(this.error.metadata));
                content += `\n\n${value}`;
            }
            content += '</code></pre>';
        }
        return content;
    }
}
exports.LogMessage = LogMessage;
