// deno-lint-ignore-file no-explicit-any
import * as dntShim from "../../../_dnt.shims.js";
import { colors, Command, log, SEP, Table } from "../../../deps.js";
import * as wmill from "../../../gen/services.gen.js";
import { requireLogin } from "../../core/auth.js";
import { resolveWorkspace, validatePath } from "../../core/context.js";
import { isSuperset, parseFromFile } from "../../types.js";
async function list(opts) {
    const workspace = await resolveWorkspace(opts);
    await requireLogin(opts);
    const folders = await wmill.listFolders({
        workspace: workspace.workspaceId,
    });
    new Table()
        .header(["Name", "Owners", "Extra Perms"])
        .padding(2)
        .border(true)
        .body(folders.map((x) => [
        x.name,
        x.owners?.join(",") ?? "-",
        JSON.stringify(x.extra_perms ?? {}),
    ]))
        .render();
}
export async function pushFolder(workspace, name, folder, localFolder) {
    if (name.startsWith(SEP)) {
        name = name.substring(1);
    }
    if (name.startsWith("f" + SEP)) {
        name = name.substring(2);
    }
    name = name.split(SEP)[0];
    log.debug(`Processing local folder ${name}`);
    // deleting old app if it exists in raw mode
    try {
        folder = await wmill.getFolder({ workspace, name });
        log.debug(`Folder ${name} exists on remote`);
    }
    catch {
        log.debug(`Folder ${name} does not exist on remote`);
        //ignore
    }
    if (folder) {
        if (isSuperset(localFolder, folder)) {
            log.debug(`Folder ${name} is up to date`);
            return;
        }
        log.debug(`Folder ${name} is not up-to-date, updating...`);
        try {
            await wmill.updateFolder({
                workspace: workspace,
                name: name,
                requestBody: {
                    ...localFolder,
                },
            });
        }
        catch (e) {
            //@ts-ignore
            console.error(e.body);
            throw e;
        }
    }
    else {
        console.log(colors.bold.yellow("Creating new folder: " + name));
        try {
            await wmill.createFolder({
                workspace: workspace,
                requestBody: {
                    name: name,
                    ...localFolder,
                },
            });
        }
        catch (e) {
            //@ts-ignore
            throw Error(`Failed to create folder ${name}: ${e.body ?? e.message}`);
        }
    }
}
async function push(opts, filePath, remotePath) {
    const workspace = await resolveWorkspace(opts);
    await requireLogin(opts);
    if (!validatePath(remotePath)) {
        return;
    }
    const fstat = await dntShim.Deno.stat(filePath);
    if (!fstat.isFile) {
        throw new Error("file path must refer to a file.");
    }
    console.log(colors.bold.yellow("Pushing folder..."));
    await pushFolder(workspace.workspaceId, remotePath, undefined, parseFromFile(filePath));
    console.log(colors.bold.underline.green("Folder pushed"));
}
const command = new Command()
    .description("folder related commands")
    .action(list)
    .command("push", "push a local folder spec. This overrides any remote versions.")
    .arguments("<file_path:string> <remote_path:string>")
    .action(push);
export default command;
