import bundler from "./mod.ts";
import Introspect, {Root} from "./introspect.tsx";

const Config = await Introspect();

console.log(Config.imports);

const transpiled:Map<string, string> = new Map();

Deno.serve(async(req)=>{
    const url = new URL(req.url);
    const index = url.pathname.lastIndexOf(".");
    if(index > -1)
    {
        const ext = url.pathname.substring(index+1, index+3);
        if(ext === "ts")
        {
            const results = await bundler({imports:Config.imports}, {
                entryPoints:[Root+url.pathname],
                outfile:"bundle.js",
                entryNames: `[dir][name]`,
            });
            results.outputFiles?.forEach(output=>{
                transpiled.set(output.path, output.text);
            })
            return new Response(results.outputFiles[0].text, {headers:{"content-type":"application/javascript"}});
        }
    }
    return new Response(Root+url.pathname);
});