<script lang="ts">
	import { browser } from '$app/environment';
	import { goto } from '$app/navigation';
	import type { UserIdentity } from '$lib/AniList/identity';
	import { onMount } from 'svelte';
	import { env } from '$env/dynamic/public';
	import HeadTitle from '$lib/HeadTitle.svelte';

	const user =
		browser && localStorage.getItem('userIdentity')
			? (JSON.parse(localStorage.getItem('userIdentity') || '') as UserIdentity).name
			: null;

	onMount(() => {
		if (user) {
			goto(`/user/${user}`);
		} else {
			goto(
				`https://anilist.co/api/v2/oauth/authorize?client_id=${env.PUBLIC_ANILIST_CLIENT_ID}&redirect_uri=${env.PUBLIC_ANILIST_REDIRECT_URI}&response_type=code`
			);
		}
	});
</script>

<HeadTitle route="Profile" path="/user" />
