"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CHILD_ISSUE_VISIBLE_UPDATE_KINDS = void 0;
exports.buildChildParentPublicationRequest = buildChildParentPublicationRequest;
exports.buildChildIssueVisibleUpdate = buildChildIssueVisibleUpdate;
const catalog_1 = require("../../i18n/catalog");
const workflow_labels_1 = require("../../platform/ticket-tracker/workflow-labels");
const publish_request_builders_1 = require("../../workflows/publish-request-builders");
const publisher_events_1 = require("../../workflows/publisher-events");
const visible_update_builders_1 = require("../../workflows/visible-update-builders");
const publisher_state_support_1 = require("../../workflows/publisher-state-support");
const recurring_1 = require("../../workflows/recurring");
const CHILD_ISSUE_VISIBLE_UPDATE_KINDS = {
    recurringChildCreated: "recurring_child_created",
    recurringChildSkipped: "recurring_child_skipped",
    unplannedChildCreated: "unplanned_child_created",
    unplannedChildReused: "unplanned_child_reused",
    controllerChildrenCreated: "controller_children_created",
    controllerChildrenExisting: "controller_children_existing",
    controllerChildrenProgressed: "controller_children_progressed",
    controllerChildrenUpdated: "controller_children_updated",
    controllerStartedConflict: "controller_started_conflict",
};
exports.CHILD_ISSUE_VISIBLE_UPDATE_KINDS = CHILD_ISSUE_VISIBLE_UPDATE_KINDS;
function buildChildParentPublicationRequest(input) {
    const workflowProjection = (0, publisher_state_support_1.buildWorkflowProjectionChange)({
        previous: input.projection.previous,
        next: input.projection.next,
        labelProjection: input.projection.labelProjection,
        auxiliaryManagedLabels: {
            previous: (0, workflow_labels_1.pickAuxiliaryManagedLabelNames)(input.workflowLabels, input.issue.labelNames),
            next: (0, workflow_labels_1.resolveAuxiliaryManagedLabelNames)({
                workflowLabels: input.workflowLabels,
                snapshot: input.projection.next,
                needsHumanReason: input.projection.nextNeedsHumanReason,
                recurringLabelNames: (0, recurring_1.resolveRecurringLabelNames)(input.workflowLabels.aiRecurring, input.workflowState),
            }),
        },
    });
    const trackerFields = input.trackerTitle !== undefined && input.trackerTitle !== null
        ? (0, publisher_state_support_1.buildWorkflowTrackerFields)({
            title: input.trackerTitle,
        })
        : undefined;
    return (0, publish_request_builders_1.buildExistingIssueWorkflowPublishRequest)({
        issueId: input.issue.id,
        preloadedIssue: input.issue,
        workflowState: input.workflowState,
        workflowProjection,
        trackerFields,
        dependencyChanges: input.dependencyChanges,
        visibleUpdate: input.visibleUpdate,
        commentCursorPolicy: input.commentCursorPolicy,
        visibleUpdateDedupeCursor: input.visibleUpdateDedupeCursor,
        terminalAction: input.terminalAction,
        responseLanguage: input.responseLanguage,
    });
}
function buildChildIssueVisibleUpdate(input) {
    const event = resolveChildIssueVisibleEvent(input.kind);
    const visibleUpdate = (0, visible_update_builders_1.buildWorkflowVisibleUpdate)({
        event,
        summary: input.summary,
        next: (0, catalog_1.translate)(input.responseLanguage, resolveChildIssueVisibleNextKey(input.kind)),
    });
    if (input.workerOutcome !== undefined) {
        visibleUpdate.workerOutcome = input.workerOutcome;
    }
    if (input.needsHumanReason !== undefined) {
        visibleUpdate.needsHumanReason = input.needsHumanReason;
    }
    return visibleUpdate;
}
function resolveChildIssueVisibleEvent(kind) {
    switch (kind) {
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.recurringChildCreated:
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.unplannedChildCreated:
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerChildrenCreated:
            return publisher_events_1.WORKFLOW_VISIBLE_EVENTS.childIssueCreated;
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.recurringChildSkipped:
            return publisher_events_1.WORKFLOW_VISIBLE_EVENTS.childIssueSkipped;
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.unplannedChildReused:
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerChildrenExisting:
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerChildrenProgressed:
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerChildrenUpdated:
            return publisher_events_1.WORKFLOW_VISIBLE_EVENTS.childIssueReused;
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerStartedConflict:
            return publisher_events_1.WORKFLOW_VISIBLE_EVENTS.humanAttentionRequested;
    }
}
function resolveChildIssueVisibleNextKey(kind) {
    switch (kind) {
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.recurringChildCreated:
            return "run.recurring.child.createdNext";
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.recurringChildSkipped:
            return "run.recurring.child.existsNext";
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.unplannedChildCreated:
            return "run.spawnChild.createdChildNext";
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.unplannedChildReused:
            return "run.spawnChild.reuseNext";
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerChildrenCreated:
            return "run.controllerChildren.createdNext";
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerChildrenExisting:
            return "run.controllerChildren.existingNext";
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerChildrenProgressed:
            return "run.controllerChildren.progressedNext";
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerChildrenUpdated:
            return "run.controllerChildren.updatedNext";
        case CHILD_ISSUE_VISIBLE_UPDATE_KINDS.controllerStartedConflict:
            return "run.controllerChildren.startedConflictNext";
    }
}
