<script lang="ts">
	import { applicationStore } from '$lib/ApplicationsStore.svelte';
	import { onMount } from 'svelte';

	let filter = $state('');

	onMount(() => {
		applicationStore.loadApplications();
	});

	let internal = $derived(
		applicationStore.applications.filter((i) => {
			if (!filter) {
				return true;
			}
			const f = new RegExp(filter, 'ig');

			let x = i.title;

			if (i.company) {
				x = `${x} @ ${i.company}`;
			}

			return x.match(f);
		})
	);
</script>

<div class="w-2/12 card p-3 flex flex-col flex-shrink min-h-0">
	<h1>To Apply</h1>
	<div class="flex pb-2">
		<input placeholder="Filter" class="p-2 flex-grow" bind:value={filter} />
		<div>
			{internal.length}
		</div>
	</div>
	<div class="overflow-auto flex-grow p-2">
		{#each internal as item}
			<div
				class="card p-2 my-2 bg-slate-100 max-w-full"
				draggable="true"
				ondragstart={() => applicationStore.dragStart(item)}
				ondragend={() => {
					window.requestAnimationFrame(() => {
						applicationStore.dragEnd();
					});
				}}
				role="none"
			>
				<div
					class="max-w-full"
					class:animate-pulse={applicationStore.dragging?.id === item.id}
				>
					<h2 class="text-lg text-blue-500 flex gap-2 max-w-full overflow-hidden">
						<div class="flex-grow max-w-[90%]">
							<div class="whitespace-nowrap overflow-hidden">
								{item.title}
							</div>
							{#if item.company}
								<div class="text-violet-800">
									@ {item.company}
								</div>
							{/if}
						</div>
						<div>
							{#if item.url.includes('linkedin')}
								<span class="bi bi-linkedin"></span>
							{:else if item.url.includes('glassdoor')}
								<span class="bi bi-eyeglasses"></span>
							{/if}
						</div>
					</h2>
				</div>
			</div>
		{/each}
	</div>
</div>
