import fs from "fs/promises";
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";

const items = Array.from({ length: 25 }).fill("image");

/**
 * @param {string} text text in svg code
 * @returns {string} generated code
 */
function generateSVG(text) {
	return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 600">
	<title>${text}</title>
	<path fill="#FFF" d="M300 .1L565 150v299.9L300 599.8 35 449.9V150z"/>
	<path fill="#8ED6FB" d="M517.7 439.5L308.8 557.8v-92L439 394.1l78.7 45.4zm14.3-12.9V179.4l-76.4 44.1v159l76.4 44.1zM81.5 439.5l208.9 118.2v-92l-130.2-71.6-78.7 45.4zm-14.3-12.9V179.4l76.4 44.1v159l-76.4 44.1zm8.9-263.2L290.4 42.2v89l-137.3 75.5-1.1.6-75.9-43.9zm446.9 0L308.8 42.2v89L446 206.8l1.1.6 75.9-44z"/>
	<path fill="#1C78C0" d="M290.4 444.8L162 374.1V234.2l128.4 74.1v136.5zm18.4 0l128.4-70.6v-140l-128.4 74.1v136.5zM299.6 303zm-129-85l129-70.9L428.5 218l-128.9 74.4-129-74.4z"/>
	</svg>`;
}

export async function setup() {
	const __dirname = dirname(fileURLToPath(import.meta.url));
	const generated = resolve(__dirname, "./generated");

	await fs.rm(generated, { recursive: true, force: true });

	const filesDir = resolve(generated, "./files");

	await fs.mkdir(filesDir, { recursive: true });

	let code = "";

	for (const [i, item] of items.entries()) {
		const request = `./files/new-url-${item}-${i}.svg`;
		const filename = resolve(generated, request);

		await fs.writeFile(filename, generateSVG(`new URL, Number - ${i}`));

		const name = `urlImage${i}`;

		code += `const ${name} = new URL(${JSON.stringify(request)}, import.meta.url);\nconsole.log(${name});\nexport { ${name} };\n`;
	}

	for (const [i, _item] of items.entries()) {
		const base64 = Buffer.from(
			generateSVG(`Data URI URL, Number - ${i}`),
			"utf8"
		).toString("base64");

		const name = `urlDataImage${i}`;

		code += `const ${name} = new URL(${JSON.stringify(`data:image/svg+xml;base64,${base64}`)}, import.meta.url);\nconsole.log(${name});\nexport { ${name} };\n`;
	}

	// import ... from ... code

	for (const [i, item] of items.entries()) {
		const request = `./files/import-${item}-${i}.svg`;
		const filename = resolve(generated, request);

		await fs.writeFile(filename, generateSVG(`Import, Number - ${i}`));

		const name = `importImage${i}`;

		code += `import ${name} from ${JSON.stringify(request)};\nconsole.log(${name});\nexport { ${name} };\n`;
	}

	for (const [i, _item] of items.entries()) {
		const base64 = Buffer.from(
			generateSVG(`Import Data URI, Number - ${i}`),
			"utf8"
		).toString("base64");

		const name = `dataURIImage${i}`;

		code += `import ${name} from ${JSON.stringify(`data:image/svg+xml;base64,${base64}`)};\nconsole.log(${name});\nexport { ${name} };\n`;
	}

	await fs.writeFile(`${generated}/module.js`, code);
}
