<script lang="ts">
  import { Link, useLocation, useNavigate, useParams } from "svelte-navigator";
  import { useMatch } from "svelte-navigator";
  import Icon from "../utils/Icon.svelte";
  import Input from "../utils/Input.svelte";
  const navigate = useNavigate();
  const location = useLocation();

  const searchMatch = useMatch("/search/:query");

  let searchQuery = $searchMatch?.params.query || "";
  $: if (!$location.pathname.includes("search")) searchQuery = "";
  function onInput(event: InputEvent & { target: HTMLInputElement }) {
    searchQuery = event.target.value;

    if (searchQuery.length > 0) {
      navigate(`/search/${searchQuery}`);
    } else {
      navigate("/");
    }
  }
</script>

<div class="header">
  <h1>
    <Link to="/">
      <img class="logo" src="/assets/upend.svg" alt="UpEnd logo" />
      UpEnd
    </Link>
  </h1>
  <div class="input">
    <Input placeholder="Search" value={searchQuery} on:input={onInput}>
      <Icon name="search" slot="prefix" />
    </Input>
  </div>
</div>

<style lang="scss">
  .header {
    display: flex;
    align-items: center;
    padding: 0 0.5rem;
    height: 3.5rem;
    border-bottom: 1px solid var(--foreground);

    background: var(--background);

    h1 {
      font-size: 16pt;
      font-weight: normal;
      margin: 0;

      :global(a) {
        display: flex;
        align-items: center;

        color: var(--foreground-lightest);
        text-decoration: none;
        font-weight: normal;
      }

      img {
        margin-right: 0.5em;
      }
    }

    .logo {
      display: inline-block;
      height: 1.5em;
    }

    .input {
      margin-left: 1em;
      flex-grow: 1;
    }
  }
</style>
