import { api } from "index";
import { redis } from "lib/redis";
import { streamerId } from "main";

type badgeObject = {
	[key: string]: {
		[key: string]: string;
	};
};

type emoteObject = {
	[key: string]: string;
};

export async function getBadges() {
	const redisdata = await redis.get("chatwidget:badges");
	if (redisdata) return new Response(redisdata);

	const globalBadges = api.chat.getGlobalBadges();
	const channelBadges = api.chat.getChannelBadges(streamerId);
	const rawBadges = await Promise.all([globalBadges, channelBadges]);

	const newObj: badgeObject = {};
	parseRawBadges(newObj, rawBadges[0]);
	parseRawBadges(newObj, rawBadges[1]);

	await redis.set("chatwidget:badges", JSON.stringify(newObj));
	await redis.expire("chatwidget:badges", 60 * 15);

	return Response.json(newObj);
}

export async function getExternalEmotes() {
	const redisdata = await redis.get("chatwidget:emotes");
	if (redisdata) return new Response(redisdata);

	const [bttvglobal, bttvuser, ffzglobal, ffzuser, seventvglobal, seventvuser] =
		await Promise.all([
			fetch("https://api.betterttv.net/3/cached/emotes/global").then(
				(a) => a.json() as any,
			),
			fetch(
				`https://api.betterttv.net/3/cached/users/twitch/${streamerId}`,
			).then((a) => a.json() as any),
			fetch("https://api.frankerfacez.com/v1/set/global").then(
				(a) => a.json() as any,
			),
			fetch(`https://api.frankerfacez.com/v1/room/id/${streamerId}`).then(
				(a) => a.json() as any,
			),
			fetch("https://7tv.io/v3/emote-sets/global").then((a) => a.json() as any),
			fetch(`https://7tv.io/v3/users/twitch/${streamerId}`).then(
				(a) => a.json() as any,
			),
		]);
	const emotes: emoteObject = {};
	for (const a of bttvglobal) {
		emotes[a.code] =
			`https://cdn.betterttv.net/emote/${a.id}/3x.${a.imageType}`;
	}
	for (const a of bttvuser.sharedEmotes) {
		emotes[a.code] =
			`https://cdn.betterttv.net/emote/${a.id}/3x.${a.imageType}`;
	}
	for (const a of ffzglobal.default_sets) {
		for (const b of ffzglobal.sets[a].emoticons) {
			emotes[b.name] = `https://cdn.frankerfacez.com/emote/${b.id}/4`;
		}
	}
	for (const a of ffzuser.sets[ffzuser.room.set].emoticons) {
		emotes[a.name] = `https://cdn.frankerfacez.com/emote/${a.id}/4`;
	}
	for (const a of seventvglobal.emotes) {
		emotes[a.name] = `https://cdn.7tv.app/emote/${a.id}/4x.avif`;
	}
	for (const a of seventvuser.emote_set.emotes) {
		emotes[a.name] = `https://cdn.7tv.app/emote/${a.id}/4x.avif`;
	}

	await redis.set("chatwidget:emotes", JSON.stringify(emotes));
	await redis.expire("chatwidget:emotes", 60 * 15);

	return Response.json(emotes);
}

import type { HelixChatBadgeSet } from "@twurple/api";

function parseRawBadges(returnobj: badgeObject, data: HelixChatBadgeSet[]) {
	for (const badge of data) {
		if (!returnobj[badge.id]) returnobj[badge.id] = {};
		for (const version of badge.versions) {
			returnobj[badge.id]![version.id] = version.getImageUrl(4);
		}
	}
}

import server from "web";
import type { twitchEventData } from "web/chatWidget/websockettypes";

export async function sendTwitchChatEvent(event: twitchEventData) {
	server.publish("twitch.chat", JSON.stringify(event));
}
