---
export interface Props {
  categories: string[];
  activeCategory?: string;
  currentPage?: number;
}

const { categories, activeCategory = 'Все', currentPage = 1 } = Astro.props;
---

<div class="blog-categories">
  <div class="site-container">
    <div class="categories-inner animate-on-scroll" data-animation="fade-up">
      <div class="categories-wrapper">
        {categories.map((cat) => (
          <a
            href={cat === 'Все' ? '/blog' : `/blog/category/${cat.toLowerCase()}`}
            class={`category-btn ${cat === activeCategory ? 'active' : ''}`}
            data-category={cat}
          >
            {cat}
          </a>
        ))}
      </div>

      <button class="search-icon-btn" id="search-icon-btn" data-modal-target="search-modal" aria-label="Поиск статей">
        <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <circle cx="11" cy="11" r="8"></circle>
          <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
        </svg>
      </button>
    </div>
  </div>
</div>

<style>
  .blog-categories {
    padding: 2rem 0 0;
    background: #f8fafc;
  }

  .categories-inner {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    flex-wrap: wrap;
  }

  .categories-wrapper {
    display: contents;
  }

  .search-icon-btn {
    background: #ffffff;
    border: 2px solid #e2e8f0;
    color: #64748b;
    width: 42px;
    height: 42px;
    border-radius: 2rem;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  .search-icon-btn:hover {
    border-color: #d4af37;
    color: #d4af37;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(212, 175, 55, 0.3);
  }

  .category-btn {
    padding: 0.6rem 1.25rem;
    border: 2px solid #e2e8f0;
    border-radius: 2rem;
    background: #ffffff;
    color: #64748b;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    white-space: nowrap;
    text-decoration: none;
    display: inline-block;
  }

  .category-btn:hover {
    border-color: #d4af37;
    color: #d4af37;
    transform: translateY(-2px);
    text-decoration: none;
  }

  .category-btn.active {
    background: linear-gradient(135deg, #d4af37 0%, #eac26e 100%);
    border-color: #d4af37;
    color: #1e293b;
    box-shadow: 0 4px 12px rgba(212, 175, 55, 0.3);
    text-decoration: none;
  }

  /* Анимации */
  .animate-on-scroll {
    opacity: 0;
    will-change: opacity, transform;
  }

  [data-animation="fade-up"] {
    transform: translateY(30px);
    transition: opacity 0.7s cubic-bezier(0.4, 0, 0.2, 1),
                transform 0.7s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .animate-on-scroll.is-visible {
    opacity: 1;
    transform: translateY(0);
  }

  @media (prefers-reduced-motion: reduce) {
    .animate-on-scroll {
      opacity: 1;
      transform: none;
      transition: none;
    }
  }

  @media (max-width: 640px) {
    .categories-inner {
      flex-wrap: wrap;
    }

    .categories-wrapper {
      gap: 0.5rem;
    }

    .category-btn {
      padding: 0.5rem 1rem;
      font-size: 0.8rem;
    }
  }
</style>

<script>
  // Клиентская логика фильтрации
  document.addEventListener('DOMContentLoaded', initFilter);
  document.addEventListener('astro:page-load', initFilter);

  function initFilter() {
    const links = document.querySelectorAll('.category-btn');

    // Открытие поиска
    const searchBtn = document.getElementById('search-icon-btn');
    searchBtn?.addEventListener('click', () => {
      window.dispatchEvent(new CustomEvent('open-modal', {
        detail: 'search-modal'
      }));
    });
  }
</script>
