<script lang="ts">
import LogInRestricted from "$lib/Error/LogInRestricted.svelte";
import HeadTitle from "$lib/Home/HeadTitle.svelte";
import Spacer from "$lib/Layout/Spacer.svelte";
import Attributions from "$lib/Settings/Categories/Attributions.svelte";
import Cache from "$lib/Settings/Categories/Cache.svelte";
import Calculation from "$lib/Settings/Categories/Calculation.svelte";
import Debug from "$lib/Settings/Categories/Debug.svelte";
import Display from "$lib/Settings/Categories/Display.svelte";
import RssFeeds from "$lib/Settings/Categories/RSSFeeds.svelte";
import SettingSync from "$lib/Settings/Categories/SettingSync.svelte";
import Category from "$lib/Settings/Category.svelte";
import tooltip from "$lib/Tooltip/tooltip";
import locale from "$stores/locale.js";
import type { PageData } from "./$types";

export let data: PageData;

// 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 routeKey="settings" path="/settings" />

<blockquote>
  {$locale().routes?.settingsFeedbackPrefix}
  <a
    href="https://anilist.co/user/fuwn"
    target="_blank"
    title={$locale().settings.tooltips.author}
    use:tooltip>@fuwn</a
  >
  {$locale().routes?.settingsFeedbackSuffix}

  <!-- <Spacer />

	<p>
		<b>{$locale().settings.fields.notice}</b>
		AniList has temporarily increased their API rate-limits, so you may experience more occasional rate
		limits than usual, both on <a href={root('/')}>due.moe</a> and on AniList.
	</p> -->
</blockquote>

{#if data.user === undefined}
  <LogInRestricted />
{:else}
  <div class="small-categories">
    <Category title={$locale().settings.settingsSync.title} id="sync" newLine={false}>
      <SettingSync />
    </Category>
    <Category title={$locale().settings.rssFeeds.title} id="feeds" newLine={false}>
      <RssFeeds user={data.user} />
    </Category>
  </div>

  <Spacer />

  <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" open={false}>
    <summary>
      {$locale().settings.debug.title}
      <button
        class="smaller-button button-badge badge-info unclickable-button"
        title={$locale().settings.debug.tooltips.version}
        use:tooltip
        onclick={(e) => e.preventDefault()}
        >{data.commit.slice(0, 7)}
      </button></summary
    >

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

<style lang="scss">
  @use "breakpoints" as *;

  .small-categories {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    align-items: start;
  }

  @media (max-width: $bp-sm) {
    .small-categories {
      grid-template-columns: 1fr;
    }
  }
</style>
