#!/usr/bin/env node

import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import startCompile, { Target } from "./process";

import dbg from "debug";
const log = dbg("app");

dbg.disable();

yargs(hideBin(process.argv))
   .version("1.0.0")
   .command(
      "compile <input>",
      "Compile source",
      (yargs) => {
         return yargs
            .positional("input", {
               describe: "Input file",
               type: "string",
               demandOption: true,
            })
            .option("definition", {
               type: "string",
               describe: "Emit definition json at specified location",
            })
            .option("output", {
               type: "string",
               describe: "Output lang and location 'ts:out/' 'c:/test'",
               alias: "o",
               coerce: (arg: string | string[] | undefined) => {
                  if (!arg) return [];
                  if (!Array.isArray(arg)) arg = [arg];
                  return arg.map((input) => {
                     const [type, output] = input.split(":", 2);
                     return {
                        type,
                        output,
                     } as Target;
                  });
               },
               array: true,
            });
      },
      (argv) => {
         if (argv.verbose) {dbg.enable("app");}
         log("Received compile command with args", argv);

         startCompile({
            input: argv.input,
            targets: argv.output as any,
            emitDefinitions: argv.definition
         })
      }
   )
   .option("verbose", {
      alias: "v",
      type: "boolean",
      describe: "Adds additional outputs",
   })
   .strictCommands()
   .demandCommand()
   .parse();
