<script>
  import { t } from '$lib/translations';
  import { resolve } from '$app/paths';
  import { page } from '$app/state';
  import logo from '$lib/assets/logo.webp';
  import LanguageSwitch from './LanguageSwitch.svelte';
  import MediaQuery from './MediaQuery.svelte';

  let isOpen = $state(false);
  let route = $derived(page.url.pathname);
  $effect(() => {
    route; // track dependency
    isOpen = false;
  });
</script>

<header>
  <div class="logo">
    <MediaQuery query="(max-width: 768px)" let:matches>
      <a href={resolve('/')}>
        <img
          src={logo}
          alt="Embroidery viewer logo"
          width="150"
          height={matches ? 70 : 100}
        />
      </a>
    </MediaQuery>
  </div>

  <nav class:active={route !== '/'} id="menuToggle">
    <input type="checkbox" bind:checked={isOpen} />
    <span></span>
    <span></span>
    <span></span>

    <ul id="menu">
      <li><a href={resolve('/about')}>{$t('header.aboutNav')}</a></li>
      <li><a href={resolve('/viewer')}>{$t('header.viewerNav')}</a></li>
      <li><a href={resolve('/support-us')}>{$t('header.supportUsNav')}</a></li>
      <li style="font-size: 22px; padding: 10px 0;"><LanguageSwitch /></li>
    </ul>
  </nav>
</header>

<style>
  header {
    position: absolute;
    top: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 30px 10px 100px;
    width: 100%;
  }

  .logo {
    z-index: -1;
  }

  .logo img {
    height: auto;
    max-height: 60px;
  }

  .logo a {
    border-bottom: none;
  }

  .logo a:hover {
    background: transparent;
  }
  #menuToggle {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
  }

  /* Click area */
  #menuToggle input {
    display: block;
    width: 40px;
    height: 32px;
    position: absolute;
    top: -7px;
    left: -5px;
    cursor: pointer;
    opacity: 0;
    z-index: 2;
  }

  /* Bars */
  #menuToggle span {
    display: block;
    width: 30px;
    height: 3px;
    margin-bottom: 7px;
    background: #ffffff;
    border-radius: 2px;
    transition: all 0.35s ease;
    transform-origin: 4px 0px;
  }

  /* Adjust origins for rotation */
  #menuToggle span:nth-child(2) {
    transform-origin: 0% 0%;
    width: 20px;
  }

  #menuToggle span:nth-child(4) {
    transform-origin: 0% 100%;
    width: 20px;
  }

  /* === ANIMATION === */

  /* Top bar → rotates */
  #menuToggle input:checked ~ span:nth-child(2) {
    transform: rotate(45deg) translate(-1px, -1px);
    width: 30px;
  }

  /* Middle bar → fades out */
  #menuToggle input:checked ~ span:nth-child(3) {
    opacity: 0;
    transform: scale(0.2);
  }

  /* Bottom bar → rotates opposite */
  #menuToggle input:checked ~ span:nth-child(4) {
    transform: rotate(-45deg) translate(1px, -1px);
    width: 30px;
  }

  #menu {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    position: absolute;
    top: -80px;
    width: 400px;
    margin: -100px 0 0 0;
    padding: 50px;
    padding-top: 125px;
    right: -100px;
    background: var(--color-primary);
    list-style-type: none;
    -webkit-font-smoothing: antialiased;
    transform-origin: 0% 0%;
    transform: translate(100%, 0);
    transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1);
    z-index: -1;

    margin: 30px;
    border-radius: 70% 30% 30% 70% / 60% 40% 0 100%;
    box-shadow:
      0 2.8px 2.2px rgba(0, 0, 0, 0.02),
      0 6.7px 5.3px rgba(0, 0, 0, 0.028),
      0 12.5px 10px rgba(0, 0, 0, 0.035),
      0 22.3px 17.9px rgba(0, 0, 0, 0.042),
      0 41.8px 33.4px rgba(0, 0, 0, 0.05),
      0 100px 80px rgba(0, 0, 0, 0.07);
  }

  #menu li a {
    padding: 10px 0;
    font-size: 22px;
    color: white;
    display: block;
  }

  #menu li a:hover {
    color: #adadad;
    transition: all 0.5s ease;
  }

  #menuToggle input:checked ~ ul {
    transform: scale(1, 1);
    opacity: 1;
  }

  .active input:checked ~ span:nth-child(2),
  .active input:checked ~ span:nth-child(3),
  .active input:checked ~ span:nth-child(4) {
    background-color: white !important;
  }

  .active span {
    background-color: var(--color-primary) !important;
  }

  @media (max-width: 1458px) {
    #menuToggle span {
      background-color: var(--color-primary);
    }
    /* Top bar → rotates */
    #menuToggle input:checked ~ span:nth-child(2) {
      background-color: white;
    }

    /* Middle bar → fades out */
    #menuToggle input:checked ~ span:nth-child(3) {
      background-color: white;
    }

    /* Bottom bar → rotates opposite */
    #menuToggle input:checked ~ span:nth-child(4) {
      background-color: white;
    }
  }

  @media (max-width: 768px) {
    header {
      padding: 10px 20px;
    }

    #menu {
      width: 100vw;
      top: -80px;
      margin: 0px 0 0 0;
      right: -20px;
      border-radius: 0;
      border-radius: 0% 0% 80% 80%;
      transform: translate(0%, -100%);
    }
  }
</style>
