<script lang="ts">
import localforage from "localforage";
import { onMount } from "svelte";
import { browser } from "$app/environment";
import type { AniListAuthorisation } from "$lib/Data/AniList/identity";
import type { Media } from "$lib/Data/AniList/media";
import RateLimitedError from "$lib/Error/RateLimited.svelte";
import type { SubsPlease } from "$lib/Media/Anime/Airing/Subtitled/subsPlease";
import identity from "$stores/identity";
import settings from "$stores/settings";
import subsPlease from "$stores/subsPlease";
import ListTitle from "../ListTitle.svelte";
import type { Title } from "../mediaTitle";
import CleanAnimeList from "./CleanAnimeList.svelte";
import PlaceholderList from "./PlaceholderList.svelte";

export let endTime: number;
export let cleanMedia: (
	media: Media[],
	displayUnresolved: boolean,
	subsPlease: SubsPlease | null,
	plannedOnly?: boolean,
) => Media[];
export let animeLists: Promise<Media[]>;
export let user: AniListAuthorisation;
export let title: Title;
export let completed = false;
export let plannedOnly = false;
export let upcoming = false;
export let notYetReleased = false;
export let dummy = false;
export let disableFilter = false;
export let limit: number | undefined = undefined;

let lastUpdatedMedia = -1;
let previousAnimeList: Media[];
let previousCount: number | undefined;
let pendingUpdate: number | null = null;
let lastListSize = 8;

onMount(async () => {
	if (browser) {
		const lastStoredList = (await localforage.getItem(
			`last${
				notYetReleased
					? "NotYetReleased"
					: upcoming
						? "Upcoming"
						: completed
							? "Completed"
							: ""
			}AnimeListLength`,
		)) as string | null;

		if (lastStoredList) lastListSize = parseInt(lastStoredList, 10);
	}
});
</script>

{#if !$subsPlease && !dummy}
  <PlaceholderList count={lastListSize} {title} />
{:else if !animeLists}
  <PlaceholderList count={lastListSize} {title} />
{:else}
  {#await animeLists}
    {#if previousAnimeList}
      <CleanAnimeList
        media={previousAnimeList}
        {title}
        bind:animeLists
        {user}
        {endTime}
        bind:lastUpdatedMedia
        {completed}
        {notYetReleased}
        {upcoming}
        bind:previousAnimeList
        bind:previousCount
        bind:pendingUpdate
        {dummy}
        {disableFilter}
        {limit}
      />
    {:else}
      <PlaceholderList count={lastListSize} {title} />
    {/if}
  {:then media}
    {#if $identity.id === -2 && !dummy}
      <PlaceholderList count={lastListSize} {title} />
    {:else}
      <CleanAnimeList
        media={cleanMedia(media, $settings.displayUnresolved, $subsPlease, plannedOnly)}
        {title}
        bind:animeLists
        {user}
        {endTime}
        bind:lastUpdatedMedia
        {completed}
        {notYetReleased}
        {upcoming}
        bind:previousAnimeList
        bind:previousCount
        bind:pendingUpdate
        {dummy}
        {disableFilter}
        {limit}
      />
    {/if}
  {:catch}
    <ListTitle time={0} count={-1337} {title} />

    <RateLimitedError />
  {/await}
{/if}
