import { defineStore } from 'pinia'
import axios from 'axios'


export const usePageStore = defineStore('pages',{
  state: () => ({
    locale: (localStorage.getItem('lang') || 'de'),
    pages: [] as Array<any>
  }),

   actions: {
    getPageByTitle (title: string) {
      return this.pages.find((page) => page.Title === title)
    },
    async setLocale(locale: string){
      if(this.pages.length == 0 || this.locale != locale ){
        this.locale = locale
        await this.fetchPages()
      }
    },
    async fetchPages(){
        const response = await axios.get('https://strapi.kornblum.one/api/pages?locale='+this.locale, {
          headers: {
            Authorization: "Bearer " + import.meta.env.VITE_STRAPI_TOKEN,
          },
        })
        this.pages = response.data.data
    }
   }
 })
