"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRevisePlanContext = isRevisePlanContext;
exports.isSemanticallyUnchangedRevisePlan = isSemanticallyUnchangedRevisePlan;
const canonical_workflow_state_1 = require("../../comments/canonical-workflow-state");
const state_1 = require("../../workflows/state");
const types_1 = require("../../workflows/types");
const workflow_timeline_scan_1 = require("../../workflows/workflow-timeline-scan");
function hasUnscannedStartCommand(comments) {
    const workflowState = resolvePersistedWorkflowState(comments);
    if (workflowState === null) {
        return false;
    }
    return (0, workflow_timeline_scan_1.scanWorkflowTimeline)({
        comments,
        workflowState,
    }).operationalCommandEvents.some((event) => event.kind === "start");
}
function isRevisePlanContext(comments) {
    const workflowState = resolvePersistedWorkflowState(comments);
    return ((0, state_1.isWorkflowAwaitingStartApproval)(workflowState) &&
        !hasUnscannedStartCommand(comments));
}
function isSemanticallyUnchangedRevisePlan(input) {
    if (input.workflowState === null) {
        return false;
    }
    const resolution = (0, canonical_workflow_state_1.resolveCanonicalWorkflowStateComment)(input.comments, input.responseLanguage);
    if (resolution.kind !== "found") {
        return false;
    }
    const previousSemantics = serializePlanningSemantics(resolution.workflowState);
    const nextSemantics = serializePlanningSemantics(input.workflowState);
    return previousSemantics === nextSemantics;
}
function serializePlanningSemantics(workflowState) {
    return JSON.stringify({
        workflowTemplates: workflowState.workflowTemplates,
        parentLink: workflowState.parentLink ?? null,
        executionContract: workflowState.executionContract,
        schedule: workflowState.schedule ?? (0, types_1.createOffWorkflowScheduleState)(),
        priorityRank: workflowState.priorityRank ?? null,
        configOverrides: normalizeConfigOverrides(workflowState.configOverrides),
        plan: normalizeWorkflowPlan(workflowState.plan),
    });
}
function normalizeConfigOverrides(configOverrides) {
    if (configOverrides === undefined) {
        return null;
    }
    return {
        entries: configOverrides.entries.map((entry) => ({
            path: entry.path,
            op: entry.op,
            value: entry.value ?? null,
        })),
    };
}
function resolvePersistedWorkflowState(comments) {
    const resolution = (0, canonical_workflow_state_1.resolveCanonicalWorkflowStateComment)(comments);
    return resolution.kind === "found" ? resolution.workflowState : null;
}
function normalizeWorkflowPlan(plan) {
    if (plan === null) {
        return null;
    }
    return {
        steps: plan.steps.map((step) => ({
            id: step.id,
            kind: step.kind,
            title: step.title,
            objective: step.objective,
            instructions: [...step.instructions],
            doneWhen: [...step.doneWhen],
            humanHandoffWhen: [...step.humanHandoffWhen],
            dependsOnStepIds: [...step.dependsOnStepIds],
            targetTemplateName: step.targetTemplateName ?? null,
            callSiteId: step.callSiteId ?? null,
            sourceTemplateName: step.sourceTemplateName ?? null,
            sourceTemplateDigest: step.sourceTemplateDigest ?? null,
            expansionGroupId: step.expansionGroupId ?? null,
            originSubtreeKey: step.originSubtreeKey ?? null,
            originWorkflowRef: step.originWorkflowRef === undefined
                ? null
                : {
                    targetTemplateName: step.originWorkflowRef.targetTemplateName,
                    callSiteId: step.originWorkflowRef.callSiteId,
                    sourceTemplateName: step.originWorkflowRef.sourceTemplateName,
                    sourceTemplateDigest: step.originWorkflowRef.sourceTemplateDigest,
                    expansionGroupId: step.originWorkflowRef.expansionGroupId,
                },
            childKey: step.childKey ?? null,
            childIssueRef: step.childIssueRef ?? null,
            suggestedTemplate: step.suggestedTemplate ?? null,
            bindingHints: step.bindingHints === undefined || step.bindingHints === null
                ? null
                : {
                    milestoneRef: step.bindingHints.milestoneRef ?? null,
                    projectRef: step.bindingHints.projectRef ?? null,
                },
            inputs: step.inputs === undefined ? null : [...step.inputs],
            outputs: step.outputs === undefined ? null : [...step.outputs],
            result: normalizeWorkflowResult(step.result),
        })),
    };
}
function normalizeWorkflowResult(result) {
    if (result === null) {
        return null;
    }
    return (0, types_1.buildWorkflowResultWithDefaults)(result);
}
