"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.installGlobalDialogEventBroadcaster = installGlobalDialogEventBroadcaster;
exports.installRecordingGlobalDialogEventBroadcaster = installRecordingGlobalDialogEventBroadcaster;
exports.clearInstalledGlobalDialogEventBroadcaster = clearInstalledGlobalDialogEventBroadcaster;
exports.assertGlobalDialogEventBroadcasterInstalled = assertGlobalDialogEventBroadcasterInstalled;
exports.getRecordingGlobalDialogEventRecorder = getRecordingGlobalDialogEventRecorder;
exports.requireRecordingGlobalDialogEventRecorder = requireRecordingGlobalDialogEventRecorder;
const evt_registry_1 = require("../evt-registry");
const log_1 = require("../log");
const log = (0, log_1.createLogger)('global-dialog-event-broadcaster');
let installedBroadcasterState = { kind: 'uninitialized' };
function buildRecorder(label, events) {
    return {
        label,
        snapshot: () => [...events],
        clear: () => {
            events.length = 0;
        },
    };
}
function installGlobalDialogEventBroadcaster(args) {
    installedBroadcasterState = {
        kind: 'custom',
        label: args.label,
        publish: args.publish,
    };
    (0, evt_registry_1.setGlobalDialogEventBroadcaster)(args.publish);
    log.debug('Installed global dialog event broadcaster', undefined, {
        label: args.label,
        kind: 'custom',
    });
}
function installRecordingGlobalDialogEventBroadcaster(args) {
    const label = args?.label?.trim() || 'recording-broadcaster';
    const events = [];
    const publish = (evt) => {
        events.push(evt);
        args?.publish?.(evt);
    };
    installedBroadcasterState = {
        kind: 'recording',
        label,
        events,
        publish: args?.publish,
    };
    (0, evt_registry_1.setGlobalDialogEventBroadcaster)(publish);
    log.debug('Installed recording global dialog event broadcaster', undefined, {
        label,
        kind: 'recording',
    });
    return buildRecorder(label, events);
}
function clearInstalledGlobalDialogEventBroadcaster() {
    const previousKind = installedBroadcasterState.kind;
    installedBroadcasterState = { kind: 'uninitialized' };
    (0, evt_registry_1.setGlobalDialogEventBroadcaster)(null);
    log.debug('Cleared global dialog event broadcaster', undefined, {
        previousKind,
    });
}
function assertGlobalDialogEventBroadcasterInstalled(where) {
    if (installedBroadcasterState.kind !== 'uninitialized') {
        return;
    }
    throw new Error(`Global dialog event broadcaster runtime missing: ${where} must install the broadcaster during runtime bootstrap before dialog logic runs`);
}
function getRecordingGlobalDialogEventRecorder() {
    if (installedBroadcasterState.kind !== 'recording') {
        return null;
    }
    return buildRecorder(installedBroadcasterState.label, installedBroadcasterState.events);
}
function requireRecordingGlobalDialogEventRecorder(where) {
    const recorder = getRecordingGlobalDialogEventRecorder();
    if (recorder) {
        return recorder;
    }
    const currentKind = installedBroadcasterState.kind;
    throw new Error(`Global dialog event recorder missing: ${where} requires a recording broadcaster, but runtime installed '${currentKind}'`);
}
