'use strict';

const config = require('config');

function onCommand(args, context) {
	if (!config.operators?.includes(context.user.username)) {
		return;
	}

	if (config.platform === 'irc' && /^#+/.test(args[0])) {
		context.sendMessage(args.slice(1).join(' '), args[0], { label: false });
		return;
	}

	if (config.platform === 'irc' && context.room.id === config.user.nick) {
		// if the room ID is the bot's own nickname, it's a PM and we should reply to the sender
		context.sendMessage(args.join(' '), context.user.id, { label: false });
	}

	if (config.platform === 'schat' && !context.room) {
		const roomName = args[0].replace(/#+/, '');
		const room = Object.values(context.bot.rooms).find((botRoom) => botRoom.name === roomName);

		if (room) {
			context.sendMessage(args.slice(1).join(' '), room.id, { label: false });
			return;
		}

		if (context.message.recipient === config.user.username) {
			context.sendMessage(args.join(' '), null, { type: 'message', label: false }, context.user.username);
		}

		return;
	}

	context.sendMessage(args.join(' '), context.room.id, { label: false });
}

module.exports = {
	name: 'Say',
	onCommand,
};
