"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 user_1 = __importDefault(require("../../models/user"));
const msg_2 = require("../../../shared/types/msg");
const chan_1 = require("../../../shared/types/chan");
exports.default = (function (irc, network) {
    const client = this;
    irc.on("join", function (data) {
        let chan = network.getChannel(data.channel);
        if (typeof chan === "undefined") {
            chan = client.createChannel({
                name: data.channel,
                state: chan_1.ChanState.JOINED,
            });
            client.emit("join", {
                network: network.uuid,
                chan: chan.getFilteredClone(true),
                shouldOpen: false,
                index: network.addChannel(chan),
            });
            client.save();
            chan.loadMessages(client, network);
            // Request channels' modes
            network.irc.raw("MODE", chan.name);
        }
        else if (data.nick === irc.user.nick) {
            chan.state = chan_1.ChanState.JOINED;
            client.emit("channel:state", {
                chan: chan.id,
                state: chan.state,
            });
        }
        const user = new user_1.default({ nick: data.nick });
        const msg = new msg_1.default({
            time: data.time,
            from: user,
            hostmask: data.ident + "@" + data.hostname,
            gecos: data.gecos,
            account: data.account,
            type: msg_2.MessageType.JOIN,
            self: data.nick === irc.user.nick,
        });
        chan.pushMessage(client, msg);
        chan.setUser(new user_1.default({ nick: data.nick }));
        client.emit("users", {
            chan: chan.id,
        });
    });
});
