import { renderMarkdownToImage } from "../html_render.js";
import { parrotingImagePath } from "./utils.js";
import { resolveCombinedStyle } from "./bg_image_util.js";
import { marked } from "marked";
export const imageType = "textSlide";
const processTextSlide = async (params) => {
    const { beat, imagePath, canvasSize } = params;
    if (!beat.image || beat.image.type !== imageType)
        return;
    const slide = beat.image.slide;
    const combinedStyle = await resolveCombinedStyle(params, beat.image.backgroundImage, beat.image.style);
    const markdown = dumpMarkdown(params) ?? "";
    const topMargin = (() => {
        if (slide.bullets?.length && slide.bullets.length > 0) {
            return "";
        }
        const marginTop = slide.subtitle ? canvasSize.height * 0.4 : canvasSize.height * 0.45;
        return `body {margin-top: ${marginTop}px;}`;
    })();
    await renderMarkdownToImage(markdown, combinedStyle + topMargin, imagePath, canvasSize.width, canvasSize.height);
    return imagePath;
};
const dumpMarkdown = (params) => {
    const { beat } = params;
    if (!beat.image || beat.image.type !== imageType)
        return;
    const slide = beat.image.slide;
    const titleString = slide.title ? `# ${slide.title}\n` : "";
    const subtitleString = slide.subtitle ? `## ${slide.subtitle}\n` : "";
    const bulletsString = (slide.bullets ?? []).map((text) => `- ${text}`).join("\n");
    return `${titleString}${subtitleString}${bulletsString}`;
};
const dumpHtml = async (params) => {
    const markdown = dumpMarkdown(params);
    return await marked.parse(markdown ?? "");
};
export const process = processTextSlide;
export const path = parrotingImagePath;
export const markdown = dumpMarkdown;
export const html = dumpHtml;
