<script setup lang="ts">

import { useDynamicPageStore } from "~/stores/dynamic-page"; 
import { useCategoryStore } from "~/stores/category";
import { useArticleStore } from "~/stores/articles";

const store = reactive({
  dynamicPage: useDynamicPageStore(),
  article: useArticleStore(),
  category: useCategoryStore()
})

const { currentArticle } = storeToRefs(store.article)
const { categoryTree } = storeToRefs(store.category)
if(!localStorage.getItem('step')) {
  localStorage.setItem('step', '0')
}
function increase() {
  
  const step = ref(Number(getComputedStyle(document.documentElement).getPropertyValue('--step').trim()))
  step.value += 2
  document.documentElement.style.setProperty('--step', step.value.toString());

}

function decrease() {
  const step = ref(Number(getComputedStyle(document.documentElement).getPropertyValue('--step').trim()))
  step.value -= 2
  document.documentElement.style.setProperty('--step', step.value.toString());
}

function findElementPathById(categories: any[], targetId: number, path = []) {
  for (const category of categories) {
    const currentPath = [...path, { title: category.title, code: category.code }];
    if (category.id === targetId) {
      return currentPath;
    }
    if (category.children) {
      const result: any = findElementPathById(category.children, targetId, currentPath);
      if (result) {
        return result;
      }
    }
  }
  return null;
}

await store.category.fetchBySiteId()
console.log(store.category.categoryTree, 'tree')
if(!categoryTree.value?.length) {
}

const currentCategoryTree = store.category.currentCategoryTree = findElementPathById(categoryTree.value, currentArticle.value.categoryId)
onMounted(() => {
  let detailEmagazine = document.querySelector('div[layout="ARTICLE_DETAIL_EMAGAZINE"]')
  let breakcrumb = document.querySelector('div[layout="BREADCRUM_DEFAULT"]')
  if( detailEmagazine && breakcrumb) {
    breakcrumb.classList.add('lg:max-w-640px','mx-auto')
  }
})

</script>
<template>
  <div class="flex justify-between items-center my-3">
    <ul class="flex gap-6 items-center text-md">
      <template v-for="(category, index) in currentCategoryTree" :key="index">
        <li class="first:text-primary-600 hover:text-primary-600 font-semibold relative after:absolute after:content-['\003E'] last:after:content-[''] after:right--4 after:text-gray-3">
            <nuxt-link :to="{ name: 'categories', params: { categories: category.code ?? '/' } }">{{ category.title }}</nuxt-link>
        </li>
      </template>
    </ul>

    <div class="flex gap-2">
      <div @click="increase()" class="w-10 h-10 border-1px border-solid shadow rounded-full relative cursor-pointer hover:bg-primary-100 hover:text-primary-600">
        <svg class="absolute top-50% left-50% translate-y--50% translate-x--55%" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"></path><path d="M11.246 15H4.75416L2.75416 20H0.600098L7.0001 4H9.0001L15.4001 20H13.246L11.246 15ZM10.446 13L8.0001 6.88516L5.55416 13H10.446ZM21.0001 12.5351V12H23.0001V20H21.0001V19.4649C20.4118 19.8052 19.7287 20 19.0001 20C16.791 20 15.0001 18.2091 15.0001 16C15.0001 13.7909 16.791 12 19.0001 12C19.7287 12 20.4118 12.1948 21.0001 12.5351ZM19.0001 18C20.1047 18 21.0001 17.1046 21.0001 16C21.0001 14.8954 20.1047 14 19.0001 14C17.8955 14 17.0001 14.8954 17.0001 16C17.0001 17.1046 17.8955 18 19.0001 18Z"></path></svg>
        <svg  class="absolute right-1.5 top-2 w-3" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M11 11V5H13V11H19V13H13V19H11V13H5V11H11Z"></path></svg>
      </div>
      <div @click="decrease()" class="w-10 h-10 border-1px border-solid shadow rounded-full relative cursor-pointer hover:bg-primary-100 hover:text-primary-600">
        <svg class="absolute top-50% left-50% translate-y--50% translate-x--55%" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"></path><path d="M11.246 15H4.75416L2.75416 20H0.600098L7.0001 4H9.0001L15.4001 20H13.246L11.246 15ZM10.446 13L8.0001 6.88516L5.55416 13H10.446ZM21.0001 12.5351V12H23.0001V20H21.0001V19.4649C20.4118 19.8052 19.7287 20 19.0001 20C16.791 20 15.0001 18.2091 15.0001 16C15.0001 13.7909 16.791 12 19.0001 12C19.7287 12 20.4118 12.1948 21.0001 12.5351ZM19.0001 18C20.1047 18 21.0001 17.1046 21.0001 16C21.0001 14.8954 20.1047 14 19.0001 14C17.8955 14 17.0001 14.8954 17.0001 16C17.0001 17.1046 17.8955 18 19.0001 18Z"></path></svg>
        <svg class="absolute right-1.5 top-2 w-3" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M5 11V13H19V11H5Z"></path></svg>
      </div>
    </div>
  </div>
</template>
<style lang="scss">
:root {
  --step: 0;
}


</style>