'use strict';

const fs = require('fs').promises;
const linkify = require('linkify-it')();

const questions = require('./jeopardy_raw.json');

async function init() {
	const curatedQuestions = await Promise.all(questions.map(async (question) => {
		const links = linkify.match(question.question);

		if (links?.length > 0 || /\[jpe?g\]|seen here/i.test(question.question)) {
			return null;
		}

		return {
			...question,
			question: question.question.replace(/^'|'$/g, ''),
			answer: question.answer
				.replace(/"|\\/g, '') // strip off quotes and backslashes first, to make sure they don't get in the way of stripping off articles from the beginning of a string
				.replace(/^(the|an|a)\b(?!-)\s*|\(.*?\)\s*/gi, '')
				.trim(),
			fullAnswer: question.answer.replace(/\\/g, ''),
			value: Number('$3,800'.match(/\d+/g).join('')),
		};
	}));

	const filteredQuestions = curatedQuestions.filter(Boolean);

	await fs.writeFile('assets/jeopardy.json', JSON.stringify(filteredQuestions, null, 4));

	console.log(curatedQuestions);
	console.log(`Saved ${filteredQuestions.length} questions, discarded ${questions.length - filteredQuestions.length}`);
}

init();
