<template>
  <div class="search">
    <div
        @click="toggleSearch(true)"
        class="search__wrapper"
        :class="[{ active: isSearchActive }]"
    >
      <form class="search__form" @submit.prevent="submitSearch">
        <input
            type="text"
            v-model="query"
            :placeholder="t('fields.search')"
        />
        <div class="search__tools">
          <button
              type="button"
              @click="clearSearch"
              v-if="query"
          >
            <Icon name="gridicons:cross" size="16" />
          </button>
          <div class="search__tools-line" v-if="query"></div>
          <button type="submit">
            <Icon name="tabler:search" size="16" />
          </button>
        </div>
      </form>
      <div class="search__results" :class="[{ active: (searchResults && isSearchActive) || loading }]">
        <skeletons-header-search v-if="loading" />
        <div
            class="search__results-inner"
            v-for="(blocks, item) in filteredSearchResults"
            :key="item"
        >
          <div class="search__results-title">
            <p>{{ getBlockTitle(item) }}:</p>
          </div>
          <div
              class="search__item"
              v-for="item in blocks"
              :key="item.uuid"
          >
            <div class="search__item-left">
              <Icon name="ic:twotone-search" size="18" />
              <p>{{ item.name }}</p>
            </div>
            <Icon name="line-md:external-link" size="18" />
          </div>
        </div>
        <div class="search__results-empty" v-if="!hasResults && query && !loading">
          <p>{{ t('header.search.empty') }}</p>
        </div>
      </div>
    </div>
    <transition name="opacity" mode="out-in">
      <div
          class="search__bg"
          @click="toggleSearch(false)"
          v-if="isSearchActive"
      />
    </transition>
  </div>
</template>

<script setup lang="ts">
import { useSearchUI } from "@/composables/search";

const {t} = useI18n();
const router = useRouter();

const {
  query,
  isSearchActive,
  loading,
  searchResults,
  filteredSearchResults,
  hasResults,
  getBlockTitle,
  clearSearch,
  toggleSearch
} = useSearchUI();

function submitSearch() {
  if (query.value) {
    router.push({
      path: '/search',
      query: { q: query.value }
    })

    toggleSearch(false);
  }
}
</script>

<style lang="scss" scoped>
.search {
  width: 100%;
  position: relative;
  z-index: 1;
  height: 45px;

  &__bg {
    background-color: rgba(0, 0, 0, 0.2);
    height: 100vh;
    left: 0;
    position: fixed;
    top: 0;
    width: 100vw;
    z-index: 1;
    transition: 0.2s;
  }

  &__wrapper {
    width: 100%;
    background-color: #f7f7f7;
    border-radius: $default_border_radius;
    transition: 0.2s;
    position: relative;
    z-index: 2;

    &.active {
      background-color: $white;
      box-shadow: 0 0 0 1px #0000000a,0 4px 4px #0000000a,0 20px 40px #00000014;
    }

    @include hover {
      background-color: $white;
      box-shadow: 0 0 0 1px #0000000a,0 4px 4px #0000000a,0 20px 40px #00000014;
    }
  }

  &__form {
    width: 100%;
    height: 45px;
    position: relative;

    & input {
      background-color: transparent;
      width: 100%;
      height: 100%;
      padding-inline: 20px 150px;
      border: 1px solid #dedede;
      border-radius: $default_border_radius;

      font-size: 16px;
      font-weight: 500;
    }
  }

  &__tools {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 10px;

    & button {
      cursor: pointer;
      display: grid;
      place-items: center;
      border-radius: $default_border_radius;
      padding: 5px 12px;
      border: 1px solid $accent;
      background-color: rgba($accent, 0.2);
      transition: 0.2s;

      font-size: 12px;
      color: $accent;

      @include hover {
        background-color: rgba($accent, 1);
        color: $white;
      }
    }

    &-line {
      background-color: $accent;
      height: 15px;
      width: 1px;
    }
  }

  &__results {
    max-height: 0;
    overflow: auto;
    transition: 0.2s;

    &.active {
      max-height: 40vh;
    }

    &-title {
      background-color: rgba($accent, 0.2);
      padding: 7px 20px;

      font-weight: 600;
    }

    &-empty {
      padding: 10px 20px;
      font-size: 14px;
    }
  }

  &__item {
    cursor: pointer;
    padding: 7px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 30px;
    transition: 0.2s;
    font-size: 14px;

    @include hover {
      background-color: #efefef;
    }

    &-left {
      display: flex;
      align-items: center;
      gap: 15px;
    }

    & p {
      word-break: break-all;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
      overflow: hidden;
    }

    & span {
      color: #7c7c7c;
    }
  }
}
</style>