/// A very tiny Visual Novel-like engine for the site

import { getRelativeLocaleUrl } from "astro:i18n";

type BaseChoice = { text: string };

export type Choice = BaseChoice &
    (
        | { action: "link"; data: string }
        | { action: "continue"; data?: never }
        | { action: "jump"; data: number }
    );

export interface Dialog {
    title: string;
    content: string;
    choices?: Choice[];
}

export function mapStaticPaths(dialogs: Dialog[]) {
    return dialogs.map((v, i) => {
        return {
            params: {
                index: i.toString(),
            },
            props: {
                title: v.title,
                content: v.content,
                choices: v.choices,
            },
        };
    });
}

const defaultChoice: Choice = {
    action: "continue",
    text: "Continue",
};

export function getChoiceElement(choice: Choice, sectionName: string, index: string, lang: string | undefined) {
    switch (choice.action) {
        case "link":
            return `<a href="${choice.data}" class="button flex flex-col justify-center"
                ${choice.data.startsWith("http") ? ' target="_blank" rel="noreferrer"' : ""}>
                ${choice.text}
            </a>`;
        case "continue":
            return `<a class="button flex flex-col justify-center" 
            href="${getRelativeLocaleUrl(lang ?? "en", `${sectionName}/${parseInt(index) + 1}`)}">
						${choice.text}
					</a>`;
        case "jump":
            return `<a class="button flex flex-col justify-center" 
            href="${getRelativeLocaleUrl(lang ?? "en", `${sectionName}/${choice.data}`)}">
						${choice.text}
					</a>`;
    }
}

export function getChoices(choices: Choice[] | undefined, sectionName: string, index: string, lang: string | undefined) {
    if (choices === undefined) {
        return getChoiceElement(defaultChoice, sectionName, index, lang);
    }

    return choices.map((v) => getChoiceElement(v, sectionName, index, lang));
}
