"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.publishTransitionSummary = publishTransitionSummary;
const runner_1 = require("../../event-hooks/runner");
const log_text_1 = require("../log-text");
const effects_1 = require("./effects");
const presentation_1 = require("./presentation");
const results_1 = require("./results");
const terminal_followup_1 = require("./terminal-followup");
async function publishTransitionSummary(input) {
    await input.logger.appendSection((0, log_text_1.issueLogSection)("resolvedTransition"), (0, presentation_1.renderOutcomeTransitionSection)({
        workflowLabels: input.workflowLabels,
        from: input.fromStatus,
        to: input.transitionState.nextSnapshot.status,
        requestedOutcome: input.transitionState.requestedOutcome,
        finalOutcome: input.transitionState.finalOutcome,
        facts: input.resolvedFacts,
        locale: input.logger.locale,
    }));
    if ((0, results_1.shouldCloseTerminalWorkflowSuccess)(input.transitionState)) {
        await (0, terminal_followup_1.publishFollowupIssueIfNeeded)({
            context: {
                cwd: input.repoRoot,
                config: input.config,
                ticketTracker: input.ticketTracker,
                ...(input.currentUserLogin !== undefined
                    ? { currentUserLogin: input.currentUserLogin }
                    : {}),
                ...(input.worker !== undefined ? { worker: input.worker } : {}),
            },
            workflowLabels: input.workflowLabels,
            transitionState: input.transitionState,
        });
    }
    const effectResult = await (0, effects_1.executeEffects)(input.repoRoot, input.config, input.currentUserLogin, input.repositoryHost, input.ticketTracker, input.issueId, input.transitionState, input.logger, input.workflowLabels, input.initialResult, input.publicationOrigin);
    await (0, runner_1.runEventHooksForPublications)({
        repoRoot: input.repoRoot,
        config: input.config,
        publications: effectResult.publications,
        logger: input.logger,
        ...(input.worker !== undefined ? { worker: input.worker } : {}),
        ...(input.homeDirectory !== undefined
            ? { homeDirectory: input.homeDirectory }
            : {}),
    });
    const summary = (0, results_1.buildCliSummary)(input.transitionState, effectResult);
    await input.logger.appendSection((0, log_text_1.issueLogSection)("finalSummary"), (0, presentation_1.renderCliSummary)(summary, input.logger.locale));
    return summary;
}
