import { authenticate } from "../middleware/authentication.js";
import { configure } from "../middleware/user-configuration.js";
import { identify } from "../middleware/user-identification.js";
import setBlocking from "../output.js";
import { YargsChecker } from "../validators/lib.js";
import { ProjectIsSetValidator } from "../validators/project-name-validator.js";
export class AuthenticatedCommandBuilder {
    options;
    constructor(options = {}) {
        this.options = options;
    }
    build(yargs, customBuilder) {
        let builder;
        if (customBuilder) {
            builder = customBuilder(yargs);
        }
        builder = this.addStandardOptions(yargs);
        builder = builder.middleware([
            setBlocking,
            authenticate,
            configure,
            identify,
        ]);
        if (this.options.additionalMiddleware) {
            builder = builder.middleware(this.options.additionalMiddleware);
        }
        return this.addValidationChecks(builder);
    }
    static create(options) {
        return new AuthenticatedCommandBuilder(options);
    }
    static withSelfHosted(options) {
        return new AuthenticatedCommandBuilder({
            ...options,
            requireSelfHostedEndpoint: true,
        });
    }
    static withMiddleware(middleware, options) {
        return new AuthenticatedCommandBuilder({
            ...options,
            additionalMiddleware: middleware,
        });
    }
    addStandardOptions(yargs) {
        let builder = yargs.option("api-key", {
            type: "string",
            describe: "The API Key from Zuplo",
            envVar: "API_KEY",
        });
        builder = builder.option("project", {
            type: "string",
            describe: "The project name",
        });
        if (this.options.requireSelfHostedEndpoint) {
            builder = builder.option("self-hosted-endpoint", {
                type: "string",
                describe: "The endpoint of your self-hosted service to deploy to",
            });
        }
        return builder;
    }
    addValidationChecks(yargs) {
        const validators = [];
        validators.push(new ProjectIsSetValidator());
        if (this.options.additionalValidators) {
            validators.push(...this.options.additionalValidators);
        }
        if (validators.length === 1) {
            return yargs.check(async (argv) => {
                return await new YargsChecker(validators[0]).check(argv);
            });
        }
        else if (validators.length > 1) {
            return yargs.check(async (argv) => {
                for (const validator of validators) {
                    const result = await new YargsChecker(validator).check(argv);
                    if (result !== true) {
                        return result;
                    }
                }
                return true;
            });
        }
        return yargs;
    }
}
//# sourceMappingURL=authenticated-command-builder.js.map