"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");
const commands = ["mute", "unmute"];
const allowDisconnected = true;
function args_to_channels(network, args) {
    const targets = [];
    for (const arg of args) {
        const argLower = arg.toLowerCase();
        const target = network.channels.find((c) => c.name.toLowerCase() === argLower);
        if (target) {
            targets.push(target);
        }
    }
    return targets;
}
function change_mute_state(client, target, valueToSet) {
    if (target.type === chan_1.ChanType.SPECIAL) {
        return;
    }
    target.setMuteStatus(valueToSet);
    client.emit("mute:changed", {
        target: target.id,
        status: valueToSet,
    });
}
const input = function (network, chan, cmd, args) {
    const valueToSet = cmd === "mute" ? true : false;
    const client = this;
    if (args.length === 0) {
        change_mute_state(client, chan, valueToSet);
        client.save();
        return;
    }
    const targets = args_to_channels(network, args);
    if (targets.length !== args.length) {
        const targetNames = targets.map((ch) => ch.name);
        const missing = args.filter((x) => !targetNames.includes(x));
        chan.pushMessage(client, new msg_1.default({
            type: msg_2.MessageType.ERROR,
            text: `No open ${missing.length === 1 ? "channel or user" : "channels or users"} found for ${missing.join(",")}`,
        }));
        return;
    }
    for (const target of targets) {
        change_mute_state(client, target, valueToSet);
    }
    client.save();
};
exports.default = {
    commands,
    input,
    allowDisconnected,
};
