"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationGateway = void 0;
const websockets_1 = require("@nestjs/websockets");
const socket_io_1 = require("socket.io");
let NotificationGateway = class NotificationGateway {
    constructor() {
        this.memberSockets = new Map();
        this.ownerSockets = new Map();
    }
    handleConnection(client) {
        const memberId = client.handshake.auth.memberId;
        const ownerId = client.handshake.auth.ownerId;
        if (memberId) {
            this.memberSockets.set(memberId, client.id);
        }
        if (ownerId) {
            this.ownerSockets.set(ownerId, client.id);
        }
    }
    handleDisconnect(client) {
        for (const [memberId, socketId] of this.memberSockets.entries()) {
            if (socketId === client.id) {
                this.memberSockets.delete(memberId);
                console.log('❌ Member disconnected', {
                    memberId,
                });
                break;
            }
        }
        for (const [ownerId, socketId] of this.ownerSockets.entries()) {
            if (socketId === client.id) {
                this.ownerSockets.delete(ownerId);
                console.log('❌ Owner disconnected', {
                    ownerId,
                });
                break;
            }
        }
    }
    sendToUser(memberId, data) {
        const socketId = this.memberSockets.get(memberId);
        console.log('📤 sendToUser', {
            memberId,
            socketId,
            hasSocket: !!socketId,
        });
        if (!socketId) {
            return;
        }
        this.server.to(socketId).emit('notification', data);
    }
    sendToOwner(ownerId, data) {
        const socketId = this.ownerSockets.get(ownerId);
        console.log('📤 sendToOwner', {
            ownerId,
            socketId,
            hasSocket: !!socketId,
        });
        if (!socketId) {
            return;
        }
        this.server.to(socketId).emit('notification', data);
    }
    sendToUsers(memberIds, data) {
        memberIds.forEach((id) => {
            this.sendToUser(id, data);
        });
    }
};
exports.NotificationGateway = NotificationGateway;
__decorate([
    (0, websockets_1.WebSocketServer)(),
    __metadata("design:type", socket_io_1.Server)
], NotificationGateway.prototype, "server", void 0);
exports.NotificationGateway = NotificationGateway = __decorate([
    (0, websockets_1.WebSocketGateway)({
        cors: {
            origin: '*',
        },
    })
], NotificationGateway);
//# sourceMappingURL=notification.socket.js.map