<script lang="ts">
	/* eslint svelte/no-at-html-tags: "off" */

	import SettingHint from '$lib/Settings/SettingHint.svelte';
	import Attributions from '$lib/Settings/Attributions.svelte';
	import { env } from '$env/dynamic/public';
	import HeadTitle from '$lib/Home/HeadTitle.svelte';
	import Display from '$lib/Settings/Categories/Display.svelte';
	import Calculation from '$lib/Settings/Categories/Calculation.svelte';
	import Debug from '$lib/Settings/Categories/Debug.svelte';
	import Cache from '$lib/Settings/Categories/Cache.svelte';
	import Category from '$lib/Settings/Category.svelte';
	import { getNotificationsContext } from 'svelte-notifications';
	import { options } from '$lib/Notification/options.js';
	import tooltip from '$lib/Tooltip/tooltip';
	import locale from '$stores/locale.js';

	export let data;

	const { addNotification } = getNotificationsContext();

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

	// 	manga.set('');
	// 	anime.set('');
	// 	await chapterDatabase.chapters.bulkDelete(ids);
	// };
</script>

<HeadTitle route="Settings" path="/settings" />

<blockquote>
	Have feedback or suggestions? Send a private message to
	<a
		href="https://anilist.co/user/fuwn"
		target="_blank"
		title={$locale().settings.tooltips.author}
		use:tooltip>@fuwn</a
	> on AniList!
</blockquote>

{#if data.user === undefined}
	<div class="card">Please log in to modify settings.</div>
{:else}
	<Category title={$locale().settings.rssFeeds.title} id="feeds">
		<button
			on:click={() => {
				addNotification(
					options({
						heading: 'RSS feed URL copied to clipboard'
					})
				);
				navigator.clipboard.writeText(
					`https://${
						env.PUBLIC_ANILIST_REDIRECT_URI?.includes('192.168') ? '192.168.1.60:5173' : 'due.moe'
					}/feeds/activity-notifications?token=${data.user.accessToken}&refresh=${
						data.user.refreshToken
					}`
				);
			}}
		>
			{$locale().settings.rssFeeds.buttons.copyToClipboard}
		</button>
		Your AniList notifications RSS feed URL
		<SettingHint lineBreak>
			This <a
				href="https://en.wikipedia.org/wiki/RSS"
				target="_blank"
				title={$locale().settings.rssFeeds.tooltips.rss}
				use:tooltip
				data-tooltip-above>RSS</a
			>
			feed will return the currently logged in AniList user's notification feed for external consumption.
			<br />
			Do not share this link with <b>anyone</b>!
		</SettingHint>
	</Category>
	<Category title={$locale().settings.display.title}><Display /></Category>
	<Category title={$locale().settings.calculation.title}><Calculation /></Category>
	<Category title={$locale().settings.cache.title}><Cache /></Category>
	<Category id="debug">
		<summary
			>{$locale().settings.debug.title}<button
				class="smaller-button button-badge badge-info"
				title={$locale().settings.debug.tooltips.version}
				use:tooltip>{data.commit.slice(0, 7)}</button
			></summary
		>

		<Debug />
	</Category>
	<Category title={$locale().settings.attributions.title} open={false}><Attributions /></Category>
{/if}
