<script lang="ts">
  import { getAllProcessors } from "../audio/processors/registry";
  import type { AudioProcessor } from "../audio/processors/AudioProcessor";

  interface Props {
    onselect: (processor: AudioProcessor) => void;
  }

  let { onselect }: Props = $props();

  const allProcessors = getAllProcessors().sort((a, b) =>
    a.getName().localeCompare(b.getName())
  );
</script>

<div class="processor-popup">
  {#each allProcessors as processor}
    <button
      class="processor-tile"
      data-description={processor.getDescription()}
      onclick={() => onselect(processor)}
    >
      {processor.getName()}
    </button>
  {/each}
</div>

<style>
  .processor-popup {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background-color: #000;
    border: 2px solid #fff;
    padding: 0.5rem;
    z-index: 1000;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.4rem;
    width: 90vw;
    max-width: 400px;
    margin-bottom: 0.5rem;
    max-height: 60vh;
    overflow-y: auto;
  }

  .processor-tile {
    background-color: #1a1a1a;
    border: 1px solid #444;
    padding: 0.5rem 0.4rem;
    text-align: center;
    cursor: pointer;
    transition: background-color 0.2s, border-color 0.2s;
    font-size: 0.8rem;
    color: #fff;
    position: relative;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .processor-tile:hover {
    background-color: #2a2a2a;
    border-color: #646cff;
  }

  .processor-tile::after {
    content: attr(data-description);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    padding: 0.5rem 0.75rem;
    background-color: #0a0a0a;
    border: 1px solid #444;
    color: #ccc;
    font-size: 0.85rem;
    width: max-content;
    max-width: 200px;
    white-space: normal;
    word-wrap: break-word;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s;
    z-index: 1001;
    margin-bottom: 0.25rem;
    display: none;
  }

  @media (min-width: 768px) {
    .processor-popup {
      grid-template-columns: repeat(3, 1fr);
      width: 500px;
      max-width: 500px;
      padding: 0.6rem;
      gap: 0.5rem;
    }

    .processor-tile {
      font-size: 0.85rem;
      padding: 0.6rem 0.5rem;
    }

    .processor-tile::after {
      display: block;
      max-width: 300px;
    }

    .processor-tile:hover::after {
      opacity: 1;
    }
  }

  @media (min-width: 1024px) {
    .processor-popup {
      grid-template-columns: repeat(4, 1fr);
      width: 600px;
      max-width: 600px;
      padding: 0.75rem;
    }

    .processor-tile {
      font-size: 0.85rem;
      padding: 0.6rem 0.4rem;
    }
  }
</style>
