import { defineStore } from "pinia";
import axios from "axios";
import appService from "../../services/appService";

export const useFrontendItemCategoryStore = defineStore("frontendItemCategory", {
    state: () => ({
        lists: [],
        show: {},
    }),
    actions: {
        fetchLists: function (payload) {
            return new Promise((resolve, reject) => {
                let url = "frontend/item-category";
                if (payload) {
                    url = url + appService.requestHandler(payload);
                }
                axios.get(url).then((res) => {
                    if (typeof payload.pinia === "undefined" || payload.pinia === true) {
                        this.lists = res.data.data;
                    }
                    resolve(res);
                }).catch((err) => {
                    reject(err);
                });
            });
        },
        fetchShow: function (payload) {
            if (payload) {
                return new Promise((resolve, reject) => {
                    axios.get(`frontend/item-category/show/${payload.slug}`).then((res) => {
                        this.show = res.data.data;
                        resolve(res);
                    }).catch((err) => {
                        reject(err);
                    });
                });
            }
        },
    },
});
