"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvironmentAuthProvider = void 0;
const service_auth_definition_1 = require("./service-auth-definition");
const service_connection_session_1 = require("./service-connection-session");
const registry_1 = require("../repository-host/registry");
const registry_2 = require("../ticket-tracker/registry");
const catalog_1 = require("../../i18n/catalog");
const check_names_1 = require("../../commands/doctor/check-names");
class EnvironmentAuthProvider {
    env;
    retryCommand;
    locale;
    constructor(env = process.env, retryCommand, locale = catalog_1.DEFAULT_APP_LOCALE) {
        this.env = env;
        this.retryCommand = retryCommand;
        this.locale = locale;
    }
    async getRepositoryHostToken(target) {
        return this.resolveCredentialOrThrow((0, registry_1.requireRepositoryHostProvider)(target.kind, this.locale).auth, target);
    }
    async getTicketTrackerToken(target) {
        return this.resolveCredentialOrThrow((0, registry_2.requireTicketTrackerProvider)(target.kind, this.locale).auth, target);
    }
    async resolveCredentialOrThrow(auth, target) {
        const credential = await auth.resolveCredential(target, this.env);
        if (credential) {
            return credential;
        }
        throw new Error((0, service_auth_definition_1.renderServiceConnectionFailureMessage)(auth, target, {
            ...(this.retryCommand
                ? {
                    retryCommand: this.retryCommand,
                }
                : {}),
        }, this.locale));
    }
    async getGitCredentials(target) {
        const definition = (0, registry_1.requireRepositoryHostProvider)(target.kind, this.locale);
        if (definition.auth.resolveGitCredentials) {
            return definition.auth.resolveGitCredentials(target, this.env);
        }
        const credential = await definition.auth.resolveCredential(target, this.env);
        if (!credential) {
            return null;
        }
        return definition.auth.getGitCredentials(credential, target);
    }
    async checkRepositoryHostSession(target) {
        try {
            const definition = (0, registry_1.requireRepositoryHostProvider)(target.kind, this.locale);
            return this.checkSession((0, check_names_1.getDoctorCheckName)("repositoryHostAuth", this.locale), definition.auth, target);
        }
        catch (error) {
            return this.buildFailureCheck((0, check_names_1.getDoctorCheckName)("repositoryHostAuth", this.locale), error);
        }
    }
    async checkTicketTrackerSession(target) {
        try {
            const definition = (0, registry_2.requireTicketTrackerProvider)(target.kind, this.locale);
            return this.checkSession((0, check_names_1.getDoctorCheckName)("ticketTrackerAuth", this.locale), definition.auth, target);
        }
        catch (error) {
            return this.buildFailureCheck((0, check_names_1.getDoctorCheckName)("ticketTrackerAuth", this.locale), error);
        }
    }
    async checkSession(name, auth, target) {
        try {
            const status = await (0, service_connection_session_1.checkServiceConnectionSession)(auth, target, this.env);
            if (status.status === "ready") {
                return {
                    name,
                    status: "ok",
                    message: (0, catalog_1.translate)(this.locale, "doctor.auth.valid"),
                };
            }
            return {
                name,
                status: "fail",
                message: (0, service_auth_definition_1.renderServiceConnectionFailureMessage)(auth, target, {
                    ...(status.reason
                        ? {
                            reason: status.reason,
                        }
                        : {}),
                    ...(this.retryCommand
                        ? {
                            retryCommand: this.retryCommand,
                        }
                        : {}),
                }, this.locale),
            };
        }
        catch (error) {
            return {
                name,
                status: "fail",
                message: (0, service_auth_definition_1.renderServiceConnectionFailureMessage)(auth, target, {
                    reason: (0, service_connection_session_1.getServiceConnectionSessionFailureReason)(error),
                    ...(this.retryCommand
                        ? {
                            retryCommand: this.retryCommand,
                        }
                        : {}),
                }, this.locale),
            };
        }
    }
    buildFailureCheck(name, error) {
        return {
            name,
            status: "fail",
            message: error instanceof Error
                ? error.message
                : (0, catalog_1.translate)(this.locale, "doctor.auth.checkFailed"),
        };
    }
}
exports.EnvironmentAuthProvider = EnvironmentAuthProvider;
