import { Command } from '@oclif/core';
import { AxiosError } from 'axios';
import DockerManager from './docker/dockerManager.js';
import LoginManager from './loginManager.js';
import OutputManager from './outputManager.js';
import StatusManager from './statusManager.js';
import TempDirManager from './tempDirManager.js';
export default class BaseCommand extends Command {
    static descriptionGerund;
    parsedArgs = {}; // eslint-disable-line @typescript-eslint/no-explicit-any
    parsedFlags = {}; // eslint-disable-line @typescript-eslint/no-explicit-any
    async assertPrerequisites() {
        await new LoginManager().login();
    }
    async catch(error) {
        let msg = "An unknown error occurred.";
        if (error instanceof AxiosError) {
            const responseMsg = error.response?.data?.message;
            if (Array.isArray(responseMsg)) {
                msg = responseMsg.join(', ');
            }
            else if (typeof responseMsg === 'string') {
                msg = responseMsg;
            }
            else {
                msg = error.message;
            }
        }
        else if (error instanceof Error) {
            msg = error.message;
        }
        this.error(msg, { exit: 1 });
    }
    async run() {
        OutputManager.newline();
        await this.parseParams();
        await this.assertPrerequisites();
        try {
            const { descriptionGerund } = this.constructor;
            if (descriptionGerund) {
                StatusManager.startCommand(descriptionGerund);
            }
            await this.runImpl();
            if (descriptionGerund) {
                StatusManager.finishCommand();
            }
        }
        finally {
            await DockerManager.dispose();
            TempDirManager.dispose();
            OutputManager.newline();
        }
    }
    async parseParams() {
        const { args, flags } = await this.parse(this.constructor);
        this.parsedArgs = args;
        this.parsedFlags = flags;
    }
}
