// @ts-check

const Ty = require("../../types")
const DiscordTypes = require("discord-api-types/v10")

const passthrough = require("../../passthrough")
const {discord, sync, db, from, select} = passthrough
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
/** @type {import("../converters/emoji-to-key")} */
const emojiToKey = sync.require("../converters/emoji-to-key")
/** @type {import("../../m2d/converters/emoji")} */
const emoji = sync.require("../../m2d/converters/emoji")
/** @type {import("../converters/remove-reaction")} */
const converter = sync.require("../converters/remove-reaction")

/**
 * @param {DiscordTypes.GatewayMessageReactionRemoveDispatchData | DiscordTypes.GatewayMessageReactionRemoveEmojiDispatchData | DiscordTypes.GatewayMessageReactionRemoveAllDispatchData} data
 */
async function removeSomeReactions(data) {
	const row = select("channel_room", "room_id", {channel_id: data.channel_id}).get()
	if (!row) return

	const eventReactedTo = from("event_message").join("message_room", "message_id").join("historical_channel_room", "historical_room_index")
		.where({message_id: data.message_id}).and("ORDER BY reaction_part").select("event_id", "room_id").get()
	if (!eventReactedTo) return

	// Due to server restrictions, all relations (i.e. reactions) have to be in the same room as the original event.
	const reactions = await api.getFullRelations(eventReactedTo.room_id, eventReactedTo.event_id, "m.annotation")

	// Run the proper strategy and any strategy-specific database changes
	const removals = await
		( "user_id" in data ? removeReaction(data, reactions)
		: "emoji" in data ? removeEmojiReaction(data, reactions)
		: removeAllReactions(data, reactions))

	// Redact the events and delete individual stored events in the database
	for (const removal of removals) {
		await api.redactEvent(eventReactedTo.room_id, removal.eventID, removal.mxid)
		if (removal.hash) db.prepare("DELETE FROM reaction WHERE hashed_event_id = ?").run(removal.hash)
	}
}

/**
 * @param {DiscordTypes.GatewayMessageReactionRemoveDispatchData} data
 * @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} reactions
 */
async function removeReaction(data, reactions) {
	const key = await emojiToKey.emojiToKey(data.emoji, data.message_id)
	return converter.removeReaction(data, reactions, key)
}

/**
 * @param {DiscordTypes.GatewayMessageReactionRemoveEmojiDispatchData} data
 * @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} reactions
 */
async function removeEmojiReaction(data, reactions) {
	const key = await emojiToKey.emojiToKey(data.emoji, data.message_id)
	const discordPreferredEncoding = await emoji.encodeEmoji(key, undefined)
	db.prepare("DELETE FROM reaction WHERE message_id = ? AND encoded_emoji = ?").run(data.message_id, discordPreferredEncoding)

	return converter.removeEmojiReaction(data, reactions, key)
}

/**
 * @param {DiscordTypes.GatewayMessageReactionRemoveAllDispatchData} data
 * @param {Ty.Event.Outer<Ty.Event.M_Reaction>[]} reactions
 */
async function removeAllReactions(data, reactions) {
	db.prepare("DELETE FROM reaction WHERE message_id = ?").run(data.message_id)

	return converter.removeAllReactions(data, reactions)
}

module.exports.removeSomeReactions = removeSomeReactions
