"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.shouldAppendAcceptedWorkflowCommandState = shouldAppendAcceptedWorkflowCommandState;
exports.applyAcceptedWorkflowCommandOptions = applyAcceptedWorkflowCommandOptions;
exports.applyAcceptedWorkflowProjectionAuxiliaryLabels = applyAcceptedWorkflowProjectionAuxiliaryLabels;
const workflow_update_1 = require("../../comments/workflow-update");
const workflow_labels_1 = require("../../platform/ticket-tracker/workflow-labels");
const comment_command_1 = require("../../workflows/comment-command");
const publisher_events_1 = require("../../workflows/publisher-events");
const recurring_auto_stop_1 = require("../../workflows/recurring-auto-stop");
const scheduled_run_1 = require("../../workflows/scheduled-run");
const types_1 = require("../../workflows/types");
const config_decision_support_1 = require("./config-decision-support");
function shouldAppendAcceptedWorkflowCommandState(input) {
    const { currentWorkflowStateCommentId, scannedInputCursor } = input;
    if (input.workflowState.pendingPlanningIntent !== "replan") {
        return false;
    }
    if (currentWorkflowStateCommentId == null || scannedInputCursor === null) {
        return false;
    }
    const canonicalIndex = input.comments.findIndex((comment) => comment.id === currentWorkflowStateCommentId);
    const scannedInputIndex = input.comments.findIndex((comment) => comment.id === scannedInputCursor.commentId);
    if (canonicalIndex === -1 || scannedInputIndex <= canonicalIndex) {
        return false;
    }
    return input.comments
        .slice(canonicalIndex + 1, scannedInputIndex)
        .some(isWorkflowStartBoundaryComment);
}
function isWorkflowStartBoundaryComment(comment) {
    if ((0, comment_command_1.parseStartCommandComment)(comment.body)) {
        return true;
    }
    return ((0, workflow_update_1.parseWorkflowUpdate)(comment.body)?.event ===
        publisher_events_1.WORKFLOW_VISIBLE_EVENTS.workflowStarted);
}
function applyAcceptedWorkflowCommandOptions(input) {
    let workflowState = input.workflowState;
    const scheduleResolution = (0, config_decision_support_1.applyAcceptedSchedule)(workflowState, input.acceptedAt, input.command.schedule, input.command.firstRun, input.displayTimeZone);
    if (scheduleResolution.kind === "invalid") {
        return { kind: "invalid" };
    }
    workflowState = scheduleResolution.workflowState;
    const scheduledRunResolution = applyAcceptedScheduledRunCommand({
        workflowState,
        scheduledRun: input.command.scheduledRun,
        acceptedAt: input.acceptedAt,
        displayTimeZone: input.displayTimeZone,
    });
    if (scheduledRunResolution.kind === "invalid") {
        return { kind: "invalid" };
    }
    workflowState = scheduledRunResolution.workflowState;
    const autoStopResolution = (0, recurring_auto_stop_1.applyRecurringAutoStopCommand)({
        workflowState,
        command: input.command.recurringAutoStop,
        acceptedAt: input.acceptedAt,
        displayTimeZone: input.displayTimeZone,
    });
    if (autoStopResolution.kind === "invalid") {
        return { kind: "invalid" };
    }
    return {
        kind: "ok",
        workflowState: autoStopResolution.workflowState,
    };
}
function applyAcceptedScheduledRunCommand(input) {
    if (input.scheduledRun === undefined) {
        return {
            kind: "ok",
            workflowState: input.workflowState,
        };
    }
    if (input.scheduledRun.mode === "off") {
        const { scheduledRun: _scheduledRun, recurring: _recurring, ...workflowState } = input.workflowState;
        return {
            kind: "ok",
            workflowState: {
                ...workflowState,
                schedule: (0, types_1.createOffWorkflowScheduleState)(),
                nextRunAt: null,
            },
        };
    }
    const scheduledRun = (0, scheduled_run_1.materializeScheduledRunCommand)({
        command: input.scheduledRun,
        acceptedAt: input.acceptedAt,
        displayTimeZone: input.displayTimeZone,
    });
    if (scheduledRun === null) {
        return { kind: "invalid" };
    }
    const { recurring: _recurring, ...workflowState } = input.workflowState;
    return {
        kind: "ok",
        workflowState: {
            ...workflowState,
            schedule: (0, types_1.createOffWorkflowScheduleState)(),
            scheduledRun,
            nextRunAt: null,
        },
    };
}
function applyAcceptedWorkflowProjectionAuxiliaryLabels(input) {
    if (input.followupProposalPolicy !== "off") {
        return;
    }
    const followupLabel = (0, workflow_labels_1.getFollowupRequestLabelName)(input.workflowLabels);
    input.workflowProjection.auxiliaryManagedLabels = {
        previous: dedupeLabels([
            ...(input.workflowProjection.auxiliaryManagedLabels?.previous ?? []),
            followupLabel,
        ]),
        next: (input.workflowProjection.auxiliaryManagedLabels?.next ?? []).filter((label) => label !== followupLabel),
    };
}
function dedupeLabels(labels) {
    return [...new Set(labels)];
}
