import { cacheCampaigns } from './campaigns.js';

import { cacheEntityIds } from './entities.js';
import redis from './redis.js';
import { cacheTagIds } from './tags.js';

export async function getIdsBySlug(slugs, domain, toMap) {
	if (!slugs) {
		return [];
	}

	const ids = await Promise.all(slugs.map(async (slug) => {
		if (!slug) {
			return null;
		}

		if (typeof slug === 'number') {
			return slug; // already an ID, tags like 69 should be a string at this stage
		}

		const id = await redis.hGet(`traxxx:${domain}:id_by_slug`, slug);

		return Number(id);
	}));

	if (toMap) {
		return Object.fromEntries(slugs.map((slug, index) => [slug, ids[index]]));
	}

	return ids.filter(Boolean);
}

export async function initCaches() {
	await Promise.all([
		cacheTagIds(),
		cacheEntityIds(),
		cacheCampaigns(),
	]);
}
