"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var WsGateway_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WsGateway = void 0;
const common_1 = require("@nestjs/common");
const websockets_1 = require("@nestjs/websockets");
const uuid = require("uuid");
const ws_socket_1 = require("./ws-socket");
let WsGateway = WsGateway_1 = class WsGateway {
    constructor() {
        this.connectedClients = [];
    }
    async broadcast(payload) {
        await Promise.all(this.connectedClients.map((ws) => new Promise((resolve) => ws.send(payload, () => resolve()))));
    }
    async handleConnection(client, req) {
        client.id = uuid.v4();
        this.connectedClients.push(client);
        common_1.Logger.verbose(`Client connected ${client.id}. Total clients: ${this.connectedClients.length}`, WsGateway_1.name);
    }
    async handleDisconnect(client) {
        this.connectedClients.splice(this.connectedClients.indexOf(client));
        common_1.Logger.verbose(`Client disconnected ${client.id}. Total clients: ${this.connectedClients.length}`, WsGateway_1.name);
    }
    onEvent({ type, payload } = {}, ws) {
        common_1.Logger.verbose(`WS -> Type:'${type}' | Payload:'${JSON.stringify(payload)}'`, WsGateway_1.name);
    }
};
__decorate([
    websockets_1.SubscribeMessage('events'),
    __param(0, websockets_1.MessageBody()), __param(1, websockets_1.ConnectedSocket()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, ws_socket_1.WsSocket]),
    __metadata("design:returntype", void 0)
], WsGateway.prototype, "onEvent", null);
WsGateway = WsGateway_1 = __decorate([
    websockets_1.WebSocketGateway()
], WsGateway);
exports.WsGateway = WsGateway;
//# sourceMappingURL=ws.gateway.js.map