import {defineStore} from 'pinia';
import moment from 'moment-timezone';
import {ref, computed} from 'vue';

import {useDark, useToggle } from '@vueuse/core';

const apikey = import.meta.env.VITE_API_KEY;

const apiUrl = import.meta.env.PROD
  ? "https://api.digisnaxx.com"
  : "http://localhost:8000";


export const useMenuStore = defineStore('MenuOptions', {
  state: () => {
    return {
      headers: {
            "content-type": "application/json",
            "Authorization": `${apikey}`,
          },
      showMenu: false,
      isHidden: true,
      today: ref(""),
      new_msg: "",
      social_links: [],
      social_images: [],
      blog_posts: [],
      snaxx: [],
    };
  },

  actions: {
    async get_today(){
      this.today = moment().tz("America/Chicago");
    },

    close_menu(name){
      this.showMenu = !this.showMenu;
      this.get_today();
    },

    toggleTheme(){
      const isDark = useDark();
      const toggleDark = useToggle(isDark);
      let theme = 'testing'
      console.log("dark: ", isDark.value)

      if (isDark.value == true){
        theme = 'light';
        console.log('jello');
      } else {
        theme = 'dark';
        console.log('belly');
      }

      console.log("theme: ", theme)

      localStorage.setItem("theme", theme);
      this.userTheme = theme;
      document.documentElement.setAttribute('data-theme', theme);
    },

    async fetchSocialLinks() {
      const results = await fetch(`${apiUrl}/socials/links`, {headers: this.headers});
      const data = await results.json();
      this.social_links = data;
    },

    async fetchSocialImages() {
      const results = await fetch(`${apiUrl}/socials/images`, {headers: this.headers});
      const data = await results.json();
      this.social_images = data;
    },

    async fetchBlogPosts() {
      const results = await fetch('https://canin.dreamfreely.org/ghost/api/content/posts/?key=ef0447585c290da508e6684916&filter=tag%3A-digisnaxx');
      const data = await results.json();
      this.blog_posts = data.posts;
    },

    async fetchDigiSnaxx() {
      const results = await fetch('https://canin.dreamfreely.org/ghost/api/content/posts/?key=ef0447585c290da508e6684916&filter=tag%3Adigisnaxx');
      const data = await results.json();
      this.snaxx = data.posts;
    },
  }
});