import { getBaseDirPath, getFullPath } from "../../../../utils/file.js";
import { outDirName, cacheDirName } from "../../../../types/const.js";
import { getUrlsIfNeeded, selectTemplate } from "../../../../utils/inquirer.js";
import { createMulmoScriptFromUrl, createMulmoScriptFromFile } from "../../../../tools/create_mulmo_script_from_url.js";
import { createMulmoScriptInteractively } from "../../../../tools/create_mulmo_script_interactively.js";
import { setGraphAILogger } from "../../../../cli/helpers.js";
export const handler = async (argv) => {
    const { o: outdir, b: basedir, "input-file": inputFile, v: verbose, i: interactive, c: cache, s: filename, llm, llm_model } = argv;
    let { t: template } = argv;
    const urls = argv.u || [];
    const baseDirPath = getBaseDirPath(basedir);
    const outDirPath = getFullPath(baseDirPath, outdir ?? outDirName);
    const cacheDirPath = getFullPath(outDirPath, cache ?? cacheDirName);
    if (!template) {
        if (interactive) {
            template = await selectTemplate();
        }
        else {
            template = "business";
        }
    }
    setGraphAILogger(verbose, {
        baseDirPath,
        outDirPath,
        cacheDirPath,
        template,
        urls,
        interactive,
        filename,
        inputFile,
        llm,
        llm_model,
    });
    const context = { outDirPath, templateName: template, urls, filename: filename, cacheDirPath, llm_model, llm, verbose };
    if (interactive) {
        await createMulmoScriptInteractively(context);
        return;
    }
    if (inputFile) {
        await createMulmoScriptFromFile(inputFile, context);
    }
    else {
        context.urls = await getUrlsIfNeeded(urls);
        await createMulmoScriptFromUrl(context);
    }
};
