"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mdBlockStr = exports.mdLineStr = void 0;
const marked_1 = require("marked");
const chalk_1 = __importDefault(require("chalk"));
const renderer = new marked_1.marked.Renderer();
renderer.heading = function (text, level) {
    return chalk_1.default.redBright.bold.underline(`\n${"#".repeat(level)} ${text}\n`);
};
renderer.strong = function (text) {
    return chalk_1.default.redBright.bold(`${text}`);
};
renderer.em = function (text) {
    return chalk_1.default.redBright.italic(`${text}`);
};
renderer.codespan = function (text) {
    const retainFences = `\`${text}\``;
    return chalk_1.default.yellow(retainFences);
};
renderer.paragraph = function (text) {
    return chalk_1.default.cyan(`${text}\n`);
};
renderer.hr = function () {
    return chalk_1.default.bold.gray("" + "-".repeat(3) + "\n");
};
renderer.link = function (href, title, text) {
    const link = chalk_1.default.blue.underline(`${href}`);
    return `${chalk_1.default.cyan(text)} (${link})`;
};
renderer.blockquote = function (quote) {
    return chalk_1.default.dim.italic(`> ${quote}`);
};
renderer.del = function (text) {
    return chalk_1.default.dim.strikethrough(`${text}`);
};
function postProcess(input) {
    input = input.replace(/<br>/g, "\n");
    const htmlDecode = (input) => {
        for (const [key, value] of Object.entries({
            "&amp;": "&",
            "&lt;": "<",
            "&gt;": ">",
            "&quot;": '"',
            "&#39;": "'",
            // add more if needed
        })) {
            input = input.replace(new RegExp(key, "g"), value);
        }
        return input;
    };
    return htmlDecode(input);
}
const mdLineStr = (input) => {
    renderer.list = function (body, ordered, start) {
        const lines = body.trim().split("\n");
        return lines
            .map((line, index) => {
            const prefix = ordered ? `${start + index}.` : "-";
            return `${chalk_1.default.gray(prefix)} ${line}\n`;
        })
            .join("");
    };
    renderer.listitem = function (text, task, checked) {
        if (task) {
            let check = checked ? "[x]" : "[ ]";
            return chalk_1.default.cyan(check + " " + text.trim());
        }
        return chalk_1.default.cyan(text.trim());
    };
    // this is ONLY for line by line makdown rendering
    renderer.code = function (code, language, isEscaped) {
        return chalk_1.default.gray("  " + code + "\n");
    };
    marked_1.marked.setOptions({
        renderer,
        mangle: false,
        headerIds: false, // prevent markdown from adding id's to headers
    });
    return postProcess((0, marked_1.marked)(input));
};
exports.mdLineStr = mdLineStr;
const mdBlockStr = (input) => {
    const UNLIKELY_SEQ = "😔😔😔";
    const PREFIX_CHAR = chalk_1.default.gray("-");
    const PREFIX_INDENT = "  ";
    renderer.list = function (body, ordered, start) {
        let resultArry = body.split(UNLIKELY_SEQ).filter((str) => str.length > 0);
        let result = "";
        let newResultArry = resultArry.map((line) => {
            // this MUST be a list item with sub items
            let subLine = line;
            if (!line.startsWith(PREFIX_CHAR) && line.includes("\n")) {
                // so, split by `\n` and join by `\n` + PREFIX_INDENT
                subLine = line.split("\n").join("\n" + PREFIX_INDENT);
            }
            return subLine;
        });
        result = newResultArry.map((line) => `${PREFIX_CHAR} ${line}`).join("\n");
        return "\n" + result + "\n";
    };
    renderer.listitem = function (text, task, checked) {
        return UNLIKELY_SEQ + `${chalk_1.default.cyan(text.trim())}` + UNLIKELY_SEQ;
    };
    // special code
    renderer.code = function (code, language, isEscaped) {
        const spaces = `\n`;
        const trimmedCode = code.replace(/^\\n+|\\n+$/g, "");
        const retainFences = `\`\`\`${chalk_1.default.yellow(language)}\n${trimmedCode}\n\`\`\`${"\n".repeat(2)}`;
        return chalk_1.default.gray(retainFences);
    };
    marked_1.marked.setOptions({
        renderer,
        mangle: false,
        headerIds: false, // prevent markdown from adding id's to headers
    });
    return postProcess((0, marked_1.marked)(input));
};
exports.mdBlockStr = mdBlockStr;
