<script lang="ts">
	import displayUnresolved from '../../stores/displayUnresolved';
	import closeAnimeByDefault from '../../stores/closeAnimeByDefault';
	import closeMangaByDefault from '../../stores/closeMangaByDefault';
	import { chapterDatabase } from '$lib/chapterDatabase';
	import cacheMangaMinutes from '../../stores/cacheMangaMinutes';

	export let data;

	$: displayingUnresolved = $displayUnresolved === 'true';
	$: mangaClosed = $closeMangaByDefault === 'true';
	$: animeClosed = $closeAnimeByDefault === 'true';

	const pruneUnresolved = async () => {
		const unresolved = await chapterDatabase.chapters.where('chapters').equals(-1).toArray();

		const ids = unresolved.map((m) => m.id);

		await chapterDatabase.chapters.bulkDelete(ids);
	};

	const pruneAll = async () => {
		const all = await chapterDatabase.chapters.toArray();

		const ids = all.map((m) => m.id);

		await chapterDatabase.chapters.bulkDelete(ids);
	};
</script>

<a href="/">Home</a>

<p />

{#if data.user === undefined}
	Please log in to modify settings.
{:else}
	<ul>
		<li>
			<a
				href="#"
				on:click={() =>
					displayingUnresolved ? displayUnresolved.set('false') : displayUnresolved.set('true')}
				>{displayingUnresolved ? 'Hide' : 'Show'} unresolved</a
			>
		</li>
		<li>
			<a href="#" on:click={pruneUnresolved}
				>Prune <b>ALL</b> cached, unresolved manga (for instant re-cache)</a
			>
		</li>
		<li><a href="#" on:click={pruneAll}>Prune <b>ALL</b> cached manga</a></li>
		<li>
			<a
				href="#"
				on:click={() =>
					animeClosed ? closeAnimeByDefault.set('false') : closeAnimeByDefault.set('true')}
				>{animeClosed ? 'Expand' : 'Close'} anime panel by default</a
			>
		</li>
		<li>
			<a
				href="#"
				on:click={() =>
					mangaClosed ? closeMangaByDefault.set('false') : closeMangaByDefault.set('true')}
				>{mangaClosed ? 'Expand' : 'Close'} manga panel by default</a
			>
		</li>
		<li>
			Re-cache <b>ALL</b> manga every <input type="number" bind:value={$cacheMangaMinutes} /> minutes
		</li>
	</ul>
{/if}
