import { parse } from "./swc.ts";
import { transform } from "./transformers.ts";
import { join, basename, dirname } from "https://deno.land/std/path/mod.ts";

const inPath = Deno.args[0];
const input = Deno.readTextFileSync(inPath);

const ast = parse(input, {
  syntax: "typescript",
  comments: true,
  target: "es2020",
});

const template = Deno.readTextFileSync("./assets/template.fprg");

const output = transform(input, ast, {
  indent: 3,
  showSourceStatements: false
}, template);

if (Deno.args.length > 1) {
  Deno.writeTextFileSync(Deno.args[1], output);
} else {
  const inPathBasename = basename(inPath);
  const dot = inPathBasename.lastIndexOf(".");
  const outPath = join(
    dirname(inPath),
    dot > -1
      ? inPathBasename.slice(0, dot) + ".fprg"
      : inPathBasename + ".fprg"
    );
  console.log(outPath);
  Deno.writeTextFileSync(outPath, output);
}

console.log(output);
