"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runDoctorChecks = runDoctorChecks;
const node_path_1 = __importDefault(require("node:path"));
const store_1 = require("../../auth-profiles/store");
const control_plane_config_1 = require("../../config/control-plane-config");
const branding_1 = require("../../core/branding");
const paths_1 = require("../../core/paths");
const factory_1 = require("../../platform/auth/factory");
const factory_2 = require("../../platform/repository-host/factory");
const environment_1 = require("./environment");
const auth_1 = require("./auth");
const custom_workers_1 = require("./custom-workers");
const repo_1 = require("./repo");
const catalog_1 = require("../../i18n/catalog");
async function runDoctorChecks(cwd, config, options = {}) {
    const locale = options.locale ?? catalog_1.DEFAULT_APP_LOCALE;
    const authProvider = (0, factory_1.createAuthProvider)(config, {
        retryCommand: options.retryCommand ?? `${branding_1.CLI_COMMAND_NAME} doctor`,
        locale,
    });
    const repositoryHost = await (0, factory_2.createRepositoryHostClient)(config, authProvider);
    const authProfiles = await (0, store_1.loadAuthProfiles)();
    const authProfileState = await (0, store_1.loadAuthProfileStateFile)();
    const includeInstructionFiles = options.includeInstructionFiles ?? true;
    const includeConfigCheck = options.includeConfigCheck ?? true;
    const includeRepoRegistrationCheck = options.includeRepoRegistrationCheck ?? true;
    const checks = [
        ...(includeConfigCheck
            ? [
                await (0, environment_1.checkFile)(node_path_1.default.join(cwd, paths_1.CONFIG_FILENAME), "config", "fail", locale),
            ]
            : []),
    ];
    if (includeInstructionFiles) {
        checks.push(await (0, environment_1.checkFile)(node_path_1.default.join(cwd, "AGENTS.md"), "AGENTS.md", "warning", locale), await (0, environment_1.checkFile)(node_path_1.default.join(cwd, "CLAUDE.md"), "CLAUDE.md", "warning", locale));
    }
    if (includeRepoRegistrationCheck) {
        checks.push(await (0, repo_1.checkRepoRegistration)(cwd, config, locale));
    }
    checks.push(await (0, repo_1.checkWorkflowTemplates)(cwd, locale, {
        maxNestedWorkflowDepth: config.maxNestedWorkflowDepth,
        workers: (0, control_plane_config_1.getConfiguredWorkerProviders)(config),
    }), ...(await (0, custom_workers_1.collectCustomWorkerDoctorChecks)({ cwd, config, locale })), ...(await (0, auth_1.collectAuthDoctorChecks)({
        config,
        authProvider,
        authProfiles: authProfiles.profiles,
        authProfileStates: authProfileState.states,
        locale,
    })), await (0, environment_1.checkServiceManagerAvailability)(locale), await (0, repo_1.checkRepositoryHostAccess)(repositoryHost, locale), await (0, repo_1.checkRemoteBaseBranch)(cwd, config, locale, authProvider), (0, repo_1.checkIssueCompletion)(config, locale), (0, repo_1.checkDependenciesStrategy)(config, locale), await (0, repo_1.checkTrackerSchema)(config, authProvider, locale), await (0, repo_1.checkWorkspaceRoot)(config, locale));
    return checks;
}
