"use strict";

const Chan = require("../../models/chan");
const Msg = require("../../models/msg");

exports.commands = ["mode", "op", "deop", "hop", "dehop", "voice", "devoice"];

exports.input = function ({irc, nick}, chan, cmd, args) {
	if (cmd !== "mode") {
		if (chan.type !== Chan.Type.CHANNEL) {
			chan.pushMessage(
				this,
				new Msg({
					type: Msg.Type.ERROR,
					text: `${cmd} command can only be used in channels.`,
				})
			);

			return;
		}

		if (args.length === 0) {
			chan.pushMessage(
				this,
				new Msg({
					type: Msg.Type.ERROR,
					text: `Usage: /${cmd} <nick> [...nick]`,
				})
			);

			return;
		}

		const mode = {
			op: "+o",
			hop: "+h",
			voice: "+v",
			deop: "-o",
			dehop: "-h",
			devoice: "-v",
		}[cmd];

		args.forEach(function (target) {
			irc.raw("MODE", chan.name, mode, target);
		});

		return;
	}

	if (args.length === 0 || args[0][0] === "+" || args[0][0] === "-") {
		args.unshift(
			chan.type === Chan.Type.CHANNEL || chan.type === Chan.Type.QUERY ? chan.name : nick
		);
	}

	irc.raw("MODE", ...args);
};

@@ -460,18 +460,72 @@
 %09%7D%0A%0A
+%09%09const target = args.filter((arg) =%3E arg !== %22%22);%0A%0A
 %09%09if (
+t
 arg
-s
+et
 .len
@@ -811,40 +811,230 @@
 %0A%0A%09%09
-args.forEach(function (target) %7B
+const limit = parseInt(irc.network.supports(%22modes%22)) %7C%7C 1;%0A%0A%09%09for (let i = 0; i %3C target.length; i += limit) %7B%0A%09%09%09const targets = target.slice(i, i + limit);%0A%09%09%09const amode = %60$%7Bmode%5B0%5D%7D$%7Bmode%5B1%5D.repeat(targets.length)%7D%60;
 %0A%09%09%09
@@ -1064,28 +1064,31 @@
 me, 
+a
 mode, 
+...
 target
+s
 );%0A%09%09%7D
-);
 %0A%0A%09%09

Optimise modes based on ISUPPORT