#!/usr/bin/env node
import "../_dnt.polyfills.js";
import "../_dnt.polyfills.js";
import * as dntShim from "../_dnt.shims.js";
import { Command, CompletionsCommand, UpgradeCommand, esMain, log, } from "../deps.js";
import flow from "./commands/flow/flow.js";
import app from "./commands/app/app.js";
import script from "./commands/script/script.js";
import workspace, { getActiveWorkspace, } from "./commands/workspace/workspace.js";
import resource from "./commands/resource/resource.js";
import resourceType from "./commands/resource-type/resource-type.js";
import user from "./commands/user/user.js";
import variable from "./commands/variable/variable.js";
import hub from "./commands/hub/hub.js";
import folder from "./commands/folder/folder.js";
import schedule from "./commands/schedule/schedule.js";
import trigger from "./commands/trigger/trigger.js";
import sync from "./commands/sync/sync.js";
import gitsyncSettings from "./commands/gitsync-settings/gitsync-settings.js";
import instance from "./commands/instance/instance.js";
import workerGroups from "./commands/worker-groups/worker-groups.js";
import dev from "./commands/dev/dev.js";
import { OpenAPI } from "../gen/index.js";
import { getHeaders, getIsWin } from "./utils/utils.js";
import { setShowDiffs } from "./core/conf.js";
import { NpmProvider } from "./utils/upgrade.js";
import { pull as hubPull } from "./commands/hub/hub.js";
import { pull, push } from "./commands/sync/sync.js";
import { add as workspaceAdd } from "./commands/workspace/workspace.js";
import workers from "./commands/workers/workers.js";
import queues from "./commands/queues/queues.js";
import dependencies from "./commands/dependencies/dependencies.js";
import init from "./commands/init/init.js";
import jobs from "./commands/jobs/jobs.js";
import { fetchVersion } from "./core/context.js";
export { flow, app, script, workspace, resource, resourceType, user, variable, hub, folder, schedule, trigger, sync, gitsyncSettings, instance, dev, hubPull, pull, push, workspaceAdd, };
// addEventListener("error", (event) => {
//   if (event.error) {
//     console.error("Error details of: " + event.error.message);
//     console.error(JSON.stringify(event.error, null, 4));
//   }
// });
export const VERSION = "1.612.0";
// Re-exported from constants.ts to maintain backwards compatibility
export { WM_FORK_PREFIX } from "./core/constants.js";
const command = new Command()
    .name("wmill")
    .action(() => log.info(`Welcome to Windmill CLI ${VERSION}. Use -h for help.`))
    .description("Windmill CLI")
    .globalOption("--workspace <workspace:string>", "Specify the target workspace. This overrides the default workspace.")
    .globalOption("--debug --verbose", "Show debug/verbose logs")
    .globalOption("--show-diffs", "Show diff informations when syncing (may show sensitive informations)")
    .globalOption("--token <token:string>", "Specify an API token. This will override any stored token.")
    .globalOption("--base-url <baseUrl:string>", "Specify the base URL of the API. If used, --token and --workspace are required and no local remote/workspace already set will be used.")
    .globalOption("--config-dir <configDir:string>", "Specify a custom config directory. Overrides WMILL_CONFIG_DIR environment variable and default ~/.config location.")
    .env("HEADERS <headers:string>", "Specify headers to use for all requests. e.g: \"HEADERS='h1: v1, h2: v2'\"")
    .version(VERSION)
    .versionOption(false)
    .command("init", init)
    .command("app", app)
    .command("flow", flow)
    .command("script", script)
    .command("workspace", workspace)
    .command("resource", resource)
    .command("resource-type", resourceType)
    .command("user", user)
    .command("variable", variable)
    .command("hub", hub)
    .command("folder", folder)
    .command("schedule", schedule)
    .command("trigger", trigger)
    .command("dev", dev)
    .command("sync", sync)
    .command("gitsync-settings", gitsyncSettings)
    .command("instance", instance)
    .command("worker-groups", workerGroups)
    .command("workers", workers)
    .command("queues", queues)
    .command("dependencies", dependencies)
    .command("jobs", jobs)
    .command("version --version", "Show version information")
    .action(async (opts) => {
    console.log("CLI version: " + VERSION);
    try {
        const provider = new NpmProvider({ package: "windmill-cli" });
        const versions = await provider.getVersions("windmill-cli");
        if (versions.latest !== VERSION) {
            console.log(`CLI is outdated. Latest version ${versions.latest} is available. Run \`wmill upgrade\` to update.`);
        }
        else {
            console.log("CLI is up to date");
        }
    }
    catch (e) {
        console.warn(`Cannot fetch latest CLI version on npmjs to check if up-to-date: ${e}`);
    }
    const workspace = await getActiveWorkspace(opts);
    if (workspace) {
        try {
            const backendVersion = await fetchVersion(workspace.remote);
            console.log("Backend Version: " + backendVersion);
        }
        catch (e) {
            console.warn("Cannot fetch backend version: " + e);
        }
    }
    else {
        console.warn("Cannot fetch backend version: no active workspace selected, choose one to pick a remote to fetch version of");
    }
})
    .command("upgrade", new UpgradeCommand({
    provider: new NpmProvider({ package: "windmill-cli" }),
}).error((e) => {
    log.error(e);
    log.info("Try running with sudo and otherwise check the result of the command: npm uninstall windmill-cli && npm install -g windmill-cli");
}))
    .command("completions", new CompletionsCommand());
async function main() {
    try {
        if (dntShim.Deno.args.length === 0) {
            command.showHelp();
        }
        const LOG_LEVEL = dntShim.Deno.args.includes("--verbose") || dntShim.Deno.args.includes("--debug")
            ? "DEBUG"
            : "INFO";
        // const NO_COLORS = Deno.args.includes("--no-colors");
        setShowDiffs(dntShim.Deno.args.includes("--show-diffs"));
        const isWin = await getIsWin();
        log.setup({
            handlers: {
                console: new log.ConsoleHandler(LOG_LEVEL, {
                    formatter: ({ msg }) => msg,
                    useColors: isWin ? false : true,
                }),
            },
            loggers: {
                default: {
                    level: LOG_LEVEL,
                    handlers: ["console"],
                },
            },
        });
        log.debug("Debug logging enabled. CLI build against " + VERSION);
        const extraHeaders = getHeaders();
        if (extraHeaders) {
            OpenAPI.HEADERS = extraHeaders;
        }
        await command.parse(dntShim.Deno.args);
    }
    catch (e) {
        if (e && typeof e === "object" && "name" in e && e.name === "ApiError") {
            console.log("Server failed. " + e.statusText + ": " + e.body);
        }
        throw e;
    }
}
function isMain() {
    // dnt-shim-ignore
    const { Deno } = globalThis;
    const isDeno = Deno != undefined;
    if (isDeno) {
        const isMain = (import.meta.url === ("file:///" + process.argv[1].replace(/\\/g, "/")).replace(/\/{3,}/, "///"));
        if (isMain) {
            if (!Deno.args.includes("completions")) {
                if (Deno.env.get("SKIP_DENO_DEPRECATION_WARNING") !== "true") {
                    log.warn("Using the deno runtime for the Windmill CLI is deprecated, you can now use node: deno uninstall wmill && npm install -g windmill-cli. To skip this warning set SKIP_DENO_DEPRECATION_WARNING=true");
                }
            }
        }
        return isMain;
    }
    else {
        //@ts-ignore
        return esMain.default(import.meta);
    }
}
if (isMain()) {
    main();
}
export default command;
