import { defineStore } from "pinia";

import route from "ziggy-js/src/js/index";

import axios from "axios";

export const usePostStore = defineStore("postStore", {
  state: () => ({
    data: {
      defaultLocaleSlug: "my",
      countryLocales: [],
      localeCategories: [],
      authors: [],
    },
  }),
  getters: {
    defaultLocaleSlug(state) {
      return state.data.defaultLocaleSlug;
    },
    countryLocales(state) {
      return state.data.countryLocales;
    },
    localeCategories(state) {
      return state.data.localeCategories;
    },
    authors(state) {
      return state.data.authors;
    },
  },
  actions: {
    async fetchAuthors() {
      try {
        const response = await axios.get(route("api.admin.authors"));
        console.log(response);
        this.data.authors = response.data.authors;
      } catch (error) {
        // alert(error);
        console.log(error);
      }
    },
    async fetchCountryLocales() {
      try {
        const response = await axios.get(route("api.admin.country-locales"));
        console.log(response);
        this.data.countryLocales = response.data.country_locales;
        this.data.defaultLocaleSlug = response.data.default_locale_slug;
      } catch (error) {
        // alert(error);
        console.log(error);
      }
    },
    async fetchLocaleCategories(countryLocaleSlug) {
      try {
        const response = await axios.get(
          route("api.admin.categories", {
            country_locale_slug: countryLocaleSlug,
          })
        );
        console.log(response);
        this.data.localeCategories = response.data.categories;
      } catch (error) {
        // alert(error);
        console.log(error);
      }
    },
  },
});
