"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtensionDebugAdapterTracker = void 0;
class ExtensionDebugAdapterTracker {
    constructor(trackers) {
        this.trackers = trackers;
    }
    static async create(session, trackerFactories) {
        const trackers = [];
        const factories = trackerFactories
            .filter((tuple) => tuple[0] === '*' || tuple[0] === session.type)
            .map((tuple) => tuple[1]);
        for (const factory of factories) {
            const tracker = await factory.createDebugAdapterTracker(session);
            if (tracker) {
                trackers.push(tracker);
            }
        }
        return new ExtensionDebugAdapterTracker(trackers);
    }
    toDebugProtocolMessage(message) {
        if (typeof message === 'string') {
            try {
                return JSON.parse(message);
            }
            catch (error) { }
        }
        return message;
    }
    onWillStartSession() {
        this.trackers.forEach((tracker) => {
            if (tracker.onWillStartSession) {
                tracker.onWillStartSession();
            }
        });
    }
    onWillReceiveMessage(message) {
        this.trackers.forEach((tracker) => {
            if (tracker.onWillReceiveMessage) {
                tracker.onWillReceiveMessage(this.toDebugProtocolMessage(message));
            }
        });
    }
    onDidSendMessage(message) {
        this.trackers.forEach((tracker) => {
            if (tracker.onDidSendMessage) {
                tracker.onDidSendMessage(this.toDebugProtocolMessage(message));
            }
        });
    }
    onWillStopSession() {
        this.trackers.forEach((tracker) => {
            if (tracker.onWillStopSession) {
                tracker.onWillStopSession();
            }
        });
    }
    onError(error) {
        this.trackers.forEach((tracker) => {
            if (tracker.onError) {
                tracker.onError(error);
            }
        });
    }
    onExit(code, signal) {
        this.trackers.forEach((tracker) => {
            if (tracker.onExit) {
                tracker.onExit(code, signal);
            }
        });
    }
}
exports.ExtensionDebugAdapterTracker = ExtensionDebugAdapterTracker;
//# sourceMappingURL=extension-debug-adapter-tracker.js.map