<script setup lang="ts">
import { apiFetch } from '~/utils/apiFetch';
import k_logo from '@/assets/LOGO.svg'

const { data: menuData } = await apiFetch<ApiMenuType>(`menu/1/`)
const pagesData = (menuData.value ? menuData.value.pages : []).sort((a, b) => a.order - b.order)
const route = useRoute()

const menu_visible = ref(false)
const toggle_menu = () => {
    if (window.innerWidth < 1280) {
        menu_visible.value = !menu_visible.value
    }
}
</script>
<template>
    <div class="header">
        <div class="container">
            <div class="logo">
                <NuxtLink to="/">
                    <k_logo />
                    <span class="logo_text">Kupizabor</span>
                </NuxtLink>
            </div>
            <div class="menu-toggle">
                <span @click="toggle_menu">
                    <Icon name="mdi:menu" />
                </span>
            </div>
            <div class="menu" :class="[{ 'toggle_visible': menu_visible }]">
                <template v-for="item in pagesData">
                    <NuxtLink :to="item.external_link || ((route.name == 'index' ? '' : '/') + `#${item.slug}`)"
                        :target="item.external_link ? '_blank' : '_self'" @click="toggle_menu">
                        {{ item.menu_title }}
                    </NuxtLink>
                </template>
            </div>
        </div>
    </div>
</template>
