#!/usr/bin/env node
import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";

import { read_config } from "./config/config.js";
import { read_ignores } from "./config/ignore.js";
import { scan_for_files } from "./scan/scanner.js";
import { convert } from "./convert/setup.js";

import * as remove_tagset from "./tagset/remove.js";
import * as add_tagset from "./tagset/add.js";
import * as update_tagset from "./tagset/update.js";
import * as install_tagset from "./tagset/install.js";
import * as init from "./init.js";


yargs(hideBin(process.argv))
    .scriptName("xweb")
    .usage("$0 <cmd> [args]")
    .command(["$0", "run"], "Scan and convert xweb files", yargs => yargs, function () {
        // read config files if there are any
        const CONFIG = read_config();
        const IGNORES = read_ignores();

        // scan for files and convert them
        const FILES_TO_CONVERT = scan_for_files(CONFIG, IGNORES);
        convert(FILES_TO_CONVERT, CONFIG);
    })
    .command("remove <names>", "Remove tagsets", yargs => {
        return yargs.positional("names", {
            describe: "What tagsets to remove seperated by whitespace",
            type: "string"
        });
    }, remove_tagset.default)
    .command("add <names>", "Add new tagsets", yargs => {
        return yargs.positional("names", {
            describe: "What tagsets to add seperated by whitespace",
            type: "string"
        });
    }, add_tagset.default)
    .command("update", "Reinstall/fetch all tagsets", yargs => yargs, update_tagset.default)
    .command("install", "Detect any new tagsets from config and add them", yargs => yargs, install_tagset.default)
    .option("y", {
        alias: "yes",
        describe: "Use default config",
        type: "boolean",
        demandOption: false,
        default: false,
    })
    .command("init", "Initialize an xweb project (use --yes or -y flag for defaults)", yargs => yargs, init.default)
    .help()
    .argv;