<script lang="ts">
	import Popup from './Popup.svelte';
	import announcementHash from '$stores/announcementHash';
	import { env } from '$env/dynamic/public';

	const announcement = env.PUBLIC_ANNOUNCEMENT;
	const dismissButton = env.PUBLIC_ANNOUNCEMENT_DISMISS;

	const hash = (s: string) =>
		s
			.split('')
			.reduce((previous, current) => ((previous << 5) - previous + current.charCodeAt(0)) | 0, 0);

	const dismiss = () => {
		if (announcement) announcementHash.set(hash(announcement));
	};
</script>

{#if announcement && $announcementHash !== hash(announcement) && $announcementHash !== 0}
	<Popup fullscreen onLeave={dismiss}>
		{announcement}

		<p />

		<button on:click={dismiss} class="dismiss">{dismissButton || 'Dismiss'}</button>
	</Popup>
{/if}

<style>
	.dismiss {
		float: right;
	}
</style>
