"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dispatchSharedReminderUpdateImpact = dispatchSharedReminderUpdateImpact;
exports.dispatchSharedReminderMigrationImpact = dispatchSharedReminderMigrationImpact;
const id_1 = require("@longrun-ai/kernel/utils/id");
const dialog_global_registry_1 = require("../dialog-global-registry");
const driver_messages_1 = require("./driver-messages");
function sharedReminderUpdateImpactsDialog(args) {
    if (args.candidate.id.equals(args.updater.id)) {
        return false;
    }
    if (args.candidate.agentId !== args.updater.agentId) {
        return false;
    }
    return args.scope === 'task' ? args.candidate.taskDocPath === args.updater.taskDocPath : true;
}
function collectSharedReminderUpdatePeerDialogs(args) {
    const peers = [];
    for (const candidate of dialog_global_registry_1.globalDialogRegistry.getLoadedInFlightDialogsForSharedReminderImpact()) {
        if (sharedReminderUpdateImpactsDialog({
            scope: args.scope,
            updater: args.updater,
            candidate,
        })) {
            peers.push(candidate);
        }
    }
    return peers;
}
async function queueSharedReminderUpdateImpactPrompt(args) {
    await args.targetDialog.queueRuntimeGuidePrompt({
        prompt: (0, driver_messages_1.formatSharedReminderUpdateImpactNotice)(args.language, {
            reminderId: args.reminderId,
            scope: args.scope,
            audience: 'peer',
        }),
        msgId: (0, id_1.generateShortId)(),
        grammar: 'markdown',
        userLanguageCode: args.language,
        skipTaskdoc: true,
    });
    dialog_global_registry_1.globalDialogRegistry.queueRootDrive(args.targetDialog.id.rootId, {
        source: 'shared_reminder_update_impact',
        reason: `reminder_id:${args.reminderId}`,
    });
}
async function queueSharedReminderMigrationImpactPrompt(args) {
    await args.targetDialog.queueRuntimeGuidePrompt({
        prompt: (0, driver_messages_1.formatSharedReminderMigrationImpactNotice)(args.language, {
            reminderId: args.reminderId,
            scope: args.scope,
        }),
        msgId: (0, id_1.generateShortId)(),
        grammar: 'markdown',
        userLanguageCode: args.language,
        skipTaskdoc: true,
    });
    dialog_global_registry_1.globalDialogRegistry.queueRootDrive(args.targetDialog.id.rootId, {
        source: 'shared_reminder_migration_impact',
        reason: `migrated_reminder_id:${args.reminderId}`,
    });
}
async function dispatchSharedReminderUpdateImpact(args) {
    const peerDialogs = collectSharedReminderUpdatePeerDialogs({
        updater: args.updater,
        scope: args.scope,
    });
    if (peerDialogs.length === 0) {
        return undefined;
    }
    let dispatchedDialogCount = 0;
    for (const peerDialog of peerDialogs) {
        await queueSharedReminderUpdateImpactPrompt({
            targetDialog: peerDialog,
            reminderId: args.reminderId,
            scope: args.scope,
            language: args.language,
        });
        dispatchedDialogCount += 1;
    }
    return {
        scope: args.scope,
        peerDialogCount: peerDialogs.length,
        dispatchedDialogCount,
    };
}
async function dispatchSharedReminderMigrationImpact(args) {
    const peerDialogs = collectSharedReminderUpdatePeerDialogs({
        updater: args.updater,
        scope: args.scope,
    });
    if (peerDialogs.length === 0) {
        return undefined;
    }
    let dispatchedDialogCount = 0;
    for (const peerDialog of peerDialogs) {
        await queueSharedReminderMigrationImpactPrompt({
            targetDialog: peerDialog,
            reminderId: args.reminderId,
            scope: args.scope,
            language: args.language,
        });
        dispatchedDialogCount += 1;
    }
    return {
        scope: args.scope,
        peerDialogCount: peerDialogs.length,
        dispatchedDialogCount,
    };
}
