import { repositories } from '../../../database/dataSource.js';
import { timeoutMap } from '../../../globals.js';
import { toUuid } from '../../../methods/device.js';
import { signalingQueueManager } from '../../../methods/signaling/signalingQueueManager.js';
import { connectedDevices } from '../../devices/websocket/handling/index.js';
/**
 * This function handles an incoming "device-changed" event callback.
 * @param callback The incoming "device-changed" callback to be handled.
 * @returns The status code for the response to the incoming callback.
 */
export async function handleDeviceChangedEventCallback(callback) {
    switch (callback.device.type) {
        case 'device':
            return await handleConcreteDevice(callback.device);
        default:
            return 410;
    }
}
async function handleConcreteDevice(concreteDevice) {
    const pendingConnections = (await repositories.peerconnection.find({
        where: {
            status: 'new',
        },
    })).filter(peerconnection => peerconnection.deviceA.url === concreteDevice.url ||
        peerconnection.deviceB.url === concreteDevice.url);
    for (const pendingConnection of pendingConnections) {
        if (!connectedDevices.has(toUuid(pendingConnection.deviceA)) ||
            !connectedDevices.has(toUuid(pendingConnection.deviceB))) {
            continue;
        }
        clearTimeout(timeoutMap.get(pendingConnection.uuid));
        signalingQueueManager.startSignalingQueues(pendingConnection.uuid);
        timeoutMap.delete(pendingConnection.uuid);
    }
    return pendingConnections.length === 0 ? 410 : 200;
}
//# sourceMappingURL=deviceChanged.js.map