<script lang="ts">
  import { cn } from '$lib/utils';
  import { Heart } from 'lucide-svelte';

  interface Sparkle {
    id: string;
    x: string;
    y: string;
    color: string;
    delay: number;
    scale: number;
  }

  let {
    className,
    colors = {
      first: '#9E7AFF',
      second: '#FE8BBB',
    },
    count = 10,
  }: {
    className: string;
    colors?: { first: string; second: string };
    count?: number;
  } = $props();

  // Helper to check if two sparkles are too close
  function isTooClose(x1: number, y1: number, x2: number, y2: number, minDist: number) {
    const dx = x1 - x2;
    const dy = y1 - y2;
    return Math.sqrt(dx * dx + dy * dy) < minDist;
  }

  // Generate sparkles with minimum distance between them
  let sparkles: Sparkle[] = [];
  const minDistance = 25; // in percentage points (adjust as needed)

  let attempts = 0;
  while (sparkles.length < count && attempts < count * 20) {
    attempts++;
    let starX = Math.random() * 100;
    let starY = Math.random() * 100;
    let tooClose = sparkles.some((s) =>
      isTooClose(Number.parseFloat(s.x), Number.parseFloat(s.y), starX, starY, minDistance)
    );
    if (tooClose) continue;

    let color = Math.random() > 0.5 ? colors.first : colors.second;
    let delay = Math.random() * 3;
    let scale = Math.random() * 1 + 0.3;
    let id = `${starX}-${starY}-${Date.now()}-${Math.random()}`;
    sparkles.push({
      id,
      x: `${starX}%`,
      y: `${starY}%`,
      color,
      delay,
      scale,
    });
  }
</script>

<div class={cn('relative', className)}>
  <span class={cn('relative inline-block', className)}>
    {#each sparkles as item (item.id)}
      <div
        class="sparkle pointer-events-none absolute z-20 top-0 left-0"
        id={item.id}
        style="left: {item.x}; top: {item.y}; color: {item.color}; animation-delay: {item.delay}s; transform: scale({item.scale});"
      >
        <!-- <svg width="13" height="13" viewBox="0 0 21 21">
          <path
            d="M9.82531 0.843845C10.0553 0.215178 10.9446 0.215178 11.1746 0.843845L11.8618 2.72026C12.4006 4.19229 12.3916 6.39157 13.5 7.5C14.6084 8.60843 16.8077 8.59935 18.2797 9.13822L20.1561 9.82534C20.7858 10.0553 20.7858 10.9447 20.1561 11.1747L18.2797 11.8618C16.8077 12.4007 14.6084 12.3916 13.5 13.5C12.3916 14.6084 12.4006 16.8077 11.8618 18.2798L11.1746 20.1562C10.9446 20.7858 10.0553 20.7858 9.82531 20.1562L9.13819 18.2798C8.59932 16.8077 8.60843 14.6084 7.5 13.5C6.39157 12.3916 4.19225 12.4007 2.72023 11.8618L0.843814 11.1747C0.215148 10.9447 0.215148 10.0553 0.843814 9.82534L2.72023 9.13822C4.19225 8.59935 6.39157 8.60843 7.5 7.5C8.60843 6.39157 8.59932 4.19229 9.13819 2.72026L9.82531 0.843845Z"
            fill={item.color}
          />
        </svg> -->
        <Heart fill={item.color} size={14} />
      </div>
    {/each}
  </span>
</div>

<style lang="postcss">
  .sparkle {
    animation: sparkle-fade 1.2s linear infinite;
    opacity: 0;
  }

  @keyframes sparkle-fade {
    0% {
      opacity: 0;
      transform: scale(0) rotate(-50deg);
    }
    50% {
      opacity: 1;
    }
    80% {
      opacity: 1;
      transform: scale(1) rotate(0deg);
    }
    100% {
      opacity: 0;
      transform: scale(0) rotate(50deg);
    }
  }
</style>
