"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");
const chan_1 = require("../../../shared/types/chan");
exports.default = (function (irc, network) {
    const client = this;
    irc.on("whois", handleWhois);
    irc.on("whowas", (data) => {
        data.whowas = true;
        handleWhois(data);
    });
    function handleWhois(data) {
        let chan = network.getChannel(data.nick);
        if (typeof chan === "undefined") {
            // Do not create new windows for errors as they may contain illegal characters
            if (data.error) {
                chan = network.getLobby();
            }
            else {
                chan = client.createChannel({
                    type: chan_1.ChanType.QUERY,
                    name: data.nick,
                });
                client.emit("join", {
                    network: network.uuid,
                    chan: chan.getFilteredClone(true),
                    shouldOpen: true,
                    index: network.addChannel(chan),
                });
                chan.loadMessages(client, network);
                client.save();
            }
        }
        let msg;
        if (data.error) {
            msg = new msg_1.default({
                type: msg_2.MessageType.ERROR,
                text: "No such nick: " + data.nick,
            });
        }
        else {
            // Absolute datetime in milliseconds since nick is idle
            data.idleTime = Date.now() - data.idle * 1000;
            // Absolute datetime in milliseconds when nick logged on.
            data.logonTime = data.logon * 1000;
            msg = new msg_1.default({
                type: msg_2.MessageType.WHOIS,
                whois: data,
            });
        }
        chan.pushMessage(client, msg);
    }
});
