"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const msg_1 = __importDefault(require("../../models/msg"));
const msg_2 = require("../../../shared/types/msg");
exports.default = (function (irc, network) {
    const client = this;
    irc.on("nick", function (data) {
        const self = data.nick === irc.user.nick;
        if (self) {
            network.setNick(data.new_nick);
            const lobby = network.getLobby();
            const msg = new msg_1.default({
                text: `You're now known as ${data.new_nick}`,
            });
            lobby.pushMessage(client, msg, true);
            client.save();
            client.emit("nick", {
                network: network.uuid,
                nick: data.new_nick,
            });
        }
        network.channels.forEach((chan) => {
            const user = chan.findUser(data.nick);
            if (typeof user === "undefined") {
                return;
            }
            const msg = new msg_1.default({
                time: data.time,
                from: user,
                type: msg_2.MessageType.NICK,
                new_nick: data.new_nick,
            });
            chan.pushMessage(client, msg);
            chan.removeUser(user);
            user.nick = data.new_nick;
            chan.setUser(user);
            client.emit("users", {
                chan: chan.id,
            });
        });
    });
});
