#!/usr/bin/env node
import arg from "arg";
import { execute, parse } from "./index.mjs";
import { readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
import { renderBlock } from "./render.mjs";
const css = readFileSync(new URL("../public/index.css", import.meta.url));
const js = readFileSync(new URL("../public/index.js", import.meta.url), "utf8");
if (/<\/\s*script\s*>/.exec(js))
    throw new Error("JS includes </script>");
async function main(argv) {
    const args = arg({}, { argv });
    for (const file of args._) {
        const resolved = resolve(file);
        console.log(`Processing ${resolved}`);
        const content = readFileSync(resolved, "utf-8");
        const blocks = await parse(content);
        const results = await execute(blocks);
        const html = ["<!DOCTYPE html>"];
        html.push(`
       <script src="https://cdn.jsdelivr.net/npm/@hpcc-js/wasm@2/dist/graphviz.umd.js"></script>
       <script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
       <script src="https://cdn.jsdelivr.net/npm/@observablehq/plot@0.6"></script>
      `);
        html.push(`<link rel="stylesheet" href="data:text/css;base64,${css.toString("base64")}"/>`);
        html.push(`<script>${js}</script>`);
        html.push(`<script>Object.assign(globalThis, framework)</script>`);
        html.push(`<div class="notebook">`);
        for (const r of results) {
            const raw = await renderBlock(r);
            html.push(raw);
        }
        html.push(`</div>`);
        html.push(`<script>framework.initTabs()</script>`);
        writeFileSync(file + ".html", html.join("\n"));
    }
}
main(process.argv.slice(2)).catch((err) => {
    console.error(err);
    process.exitCode = 1;
});
//# sourceMappingURL=bin.mjs.map