"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRunCycleContextBundle = createRunCycleContextBundle;
const global_settings_1 = require("../core/global-settings");
const factory_1 = require("../git/factory");
const factory_2 = require("../platform/auth/factory");
const factory_3 = require("../platform/repository-host/factory");
const linked_branch_service_1 = require("../platform/repository-host/linked-branch-service");
const scoped_repository_host_client_1 = require("../platform/repository-host/scoped-repository-host-client");
const factory_4 = require("../platform/ticket-tracker/factory");
const scoped_ticket_tracker_client_1 = require("../platform/ticket-tracker/scoped-ticket-tracker-client");
const runtime_1 = require("../worker/runtime");
const factory_5 = require("../workspaces/factory");
const base_workspace_1 = require("../workspaces/base-workspace");
const NON_WORKER_RUNTIME = {
    async run() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
    async runContinuationIssueSelection() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
    async runIssueDraft() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
    async runAiWorkflowOptionAutofill() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
    async runWorkflowTemplateFitReview() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
    async runWorkflowPlanReview() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
    async runPullRequestReviewNecessity() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
    async runEventHook() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
    async runTemplateDraft() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
    async runTemplateUpdate() {
        throw new Error("Worker runtime is not available for a non-worker service task.");
    },
};
async function createRunCycleContextBundle(cwd, config, options = {}) {
    const sharedClients = options.sharedClients;
    let repositoryHost;
    let linkedBranchService;
    let freshTicketTracker;
    let ticketTracker;
    let gitRepository;
    let issueWorkspaceManager;
    let currentUserLogin;
    const authProvider = (0, factory_2.createAuthProvider)(config);
    if (sharedClients) {
        repositoryHost = sharedClients.repositoryHost;
        linkedBranchService = sharedClients.linkedBranchService;
        freshTicketTracker = sharedClients.freshTicketTracker;
        ticketTracker = sharedClients.ticketTracker;
        gitRepository = sharedClients.gitRepository;
        issueWorkspaceManager = sharedClients.issueWorkspaceManager;
        currentUserLogin = sharedClients.currentUserLogin;
    }
    else {
        const freshRepositoryHost = await (0, factory_3.createRepositoryHostClient)(config, authProvider);
        linkedBranchService = await (0, linked_branch_service_1.createLinkedBranchService)(config, authProvider);
        freshTicketTracker = await (0, factory_4.createTicketTrackerClient)(config, authProvider);
        repositoryHost = (0, scoped_repository_host_client_1.createScopedRepositoryHostClient)(freshRepositoryHost);
        ticketTracker = (0, scoped_ticket_tracker_client_1.createScopedTicketTrackerClient)(freshTicketTracker);
        currentUserLogin = await ticketTracker.getAuthenticatedUserLogin();
        gitRepository = (0, factory_1.createGitRepositoryClient)(config, authProvider);
        issueWorkspaceManager = (0, factory_5.createIssueWorkspaceManager)(config, gitRepository);
    }
    const worker = options.skipWorkerRuntime
        ? NON_WORKER_RUNTIME
        : await (0, runtime_1.createConfiguredWorkerRuntime)(config);
    const globalSettings = (0, global_settings_1.loadControlPlaneGlobalSettings)(options.homeDirectory);
    return {
        cwd,
        config,
        repositoryHost,
        linkedBranchService,
        ticketTracker,
        freshTicketTracker,
        issueWorkspaceManager,
        gitRepository,
        worker,
        workerIdentity: {
            workerId: globalSettings.workerId,
        },
        ...(currentUserLogin !== undefined ? { currentUserLogin } : {}),
        ...(options.homeDirectory
            ? { logHomeDirectory: options.homeDirectory }
            : {}),
        ...(options.issueIds ? { issueIds: options.issueIds } : {}),
        ...(options.runSource ? { runSource: options.runSource } : {}),
        ...(options.workerSlotBudget !== undefined
            ? { workerSlotBudget: options.workerSlotBudget }
            : {}),
        ...(options.workflowTemplateRepoRoot
            ? { workflowTemplateRepoRoot: options.workflowTemplateRepoRoot }
            : {}),
        refreshWorkflowTemplateRepoRoot: async () => {
            const workspace = await (0, base_workspace_1.ensureBaseWorkspace)({
                config,
                gitClient: gitRepository,
                authProvider,
                repositoryRoot: cwd,
                ...(options.homeDirectory
                    ? { homeDirectory: options.homeDirectory }
                    : {}),
            });
            return workspace.path;
        },
    };
}
