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

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

  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 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);
    }
  });
</script>

{#if !$subsPlease && !dummy}
  <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: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:pendingUpdate
        {dummy}
        {disableFilter}
        {limit}
      />
    {/if}
  {:catch}
    <ListTitle time={0} count={-1337} {title} />

    <Error />
  {/await}
{/if}
