<script lang="ts">
  import { toggleFollow } from '$lib/Data/AniList/follow';
  import type { AniListAuthorisation } from '$lib/Data/AniList/identity';
  import LogInRestricted from '$lib/Error/LogInRestricted.svelte';

  export let user: AniListAuthorisation;

  let input = '';
  let submit = '';
</script>

{#if user === undefined}
  <LogInRestricted />
{:else}
  <p>
    <input
      type="text"
      minlength="1"
      placeholder="Username"
      bind:value={input}
      onkeypress={(e) => {
        if (e.key === 'Enter') {
          submit = input;

          // eslint-disable-next-line no-undef
          umami.track('Fix Follow');
        }
      }}
    />
    <a href={'#'} onclick={() => (submit = input)}>
      Toggle follow for {input.length === 0 ? '...' : input}
    </a>
  </p>

  {#if submit.length > 0}
    {#await toggleFollow(user, submit)}
      Toggling follow ...
    {:then response}
      <p>
        Successfully toggled follow for {submit}.

        {submit}
        {response.isFollower ? 'is' : 'is not'} following you, {response.isFollowing
          ? 'but'
          : 'and'} you {response.isFollowing ? 'are' : 'are not'} following them.
      </p>
    {:catch}
      <p>Failed to toggle follow for {submit}.</p>
    {/await}
  {/if}
{/if}
