"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveReminderContextFollowingMessage = resolveReminderContextFollowingMessage;
exports.resolveReminderContextHealth = resolveReminderContextHealth;
exports.resolveReminderContextBusinessState = resolveReminderContextBusinessState;
exports.resolveReminderContextFooterStateFromSignals = resolveReminderContextFooterStateFromSignals;
const context_health_1 = require("./context-health");
function resolveReminderContextFollowingMessage(args) {
    if (args.prompt === undefined || args.currentTurnDialogMsgsForContext.length === 0) {
        return { kind: 'none' };
    }
    if (args.prompt.origin !== 'user') {
        return { kind: 'runtime_notice' };
    }
    return typeof args.prompt.q4hAnswerCallId === 'string' &&
        args.prompt.q4hAnswerCallId.trim() !== ''
        ? { kind: 'human_answer' }
        : { kind: 'user_message' };
}
function resolveReminderContextHealth(snapshot) {
    const remediationLevel = (0, context_health_1.getContextHealthRemediationLevel)(snapshot);
    return remediationLevel === undefined ? { kind: 'normal' } : { kind: remediationLevel };
}
function resolveReminderContextBusinessState(args) {
    if (args.followingMessage.kind === 'human_answer') {
        // askHuman answers resume a waiting task. They are not fresh user follow-ups and should not
        // inherit interjection or completed-handoff wording from adjacent durable state.
        return { kind: 'none' };
    }
    if (args.followingMessage.kind === 'user_message' &&
        args.hasCompletedHandoffWithoutPendingReply) {
        // Business scenario: the requester-side work has already received its final report, and now a
        // user is speaking again inside that completed Side Dialog. Dominds can know this from durable
        // handoff state; the model should hear the simple product meaning directly instead of guessing
        // from older task context or reminder-maintenance references.
        return { kind: 'user_followup_after_completed_handoff' };
    }
    if (args.pendingUserInterjectionReply) {
        // User-visible reply gaps take priority over handoff continuation state. The completed
        // handoff case is most specific: Dominds already knows the old delegated task is done. The
        // footer should say there is no old handoff to advance and the model should handle the current
        // user message normally. Do not narrow this to "only answer" or "do not organize reminders":
        // a user may explicitly ask for reminder cleanup/correction as the current conversation topic.
        //
        // This branch may run on a later tool-followup turn where no user message immediately
        // follows the reminder block anymore. That is still the same pending user follow-up; do not
        // demote it to generic auto-continuation wording just because the prompt itself is absent
        // on this retry/continuation round.
        if (args.hasCompletedHandoffWithoutPendingReply) {
            return { kind: 'user_followup_after_completed_handoff' };
        }
        if (args.hasActiveReplyObligation) {
            return { kind: 'pending_user_interjection_with_active_reply' };
        }
        return { kind: 'pending_user_interjection' };
    }
    if (args.hasActiveReplyObligation) {
        return { kind: 'active_reply_obligation' };
    }
    return { kind: 'none' };
}
function resolveReminderContextFooterStateFromSignals(args) {
    const followingMessage = resolveReminderContextFollowingMessage({
        prompt: args.prompt,
        currentTurnDialogMsgsForContext: args.currentTurnDialogMsgsForContext,
    });
    return {
        followingMessage,
        contextHealth: resolveReminderContextHealth(args.contextHealth),
        dialogScope: args.dialogScope,
        business: resolveReminderContextBusinessState({
            followingMessage,
            pendingUserInterjectionReply: args.pendingUserInterjectionReply,
            hasCompletedHandoffWithoutPendingReply: args.hasCompletedHandoffWithoutPendingReply,
            hasActiveReplyObligation: args.hasActiveReplyObligation,
        }),
    };
}
