import axios from "axios";
import { defineStore } from "pinia";

export const useSocialLoginStore = defineStore("socialLogin", {
    state: () => ({
        lists: [],
        page: {},
        pagination: [],
        show: {},
        temp: {
            temp_id: null,
            isEditing: false,
        },
    }),
    actions: {
        fetchLists: function (payload) {
            return new Promise((resolve, reject) => {
                let url = "admin/setting/social-login";
                axios
                    .get(url)
                    .then((res) => {
                        if (
                            typeof payload.pinia === "undefined" ||
                            payload.pinia === true
                        ) {
                            this.lists = res.data.data;
                            if (
                                typeof res.data.meta !== "undefined" &&
                                res.data.meta !== null
                            ) {
                                this.page = {
                                    from: res.data.meta.from,
                                    to: res.data.meta.to,
                                    total: res.data.meta.total,
                                };
                                this.pagination = res.data;
                            }
                        }
                        resolve(res);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            });
        },
        save: function (payload) {
            return new Promise((resolve, reject) => {
                axios
                    .put(`/admin/setting/social-login`, payload.form)
                    .then((res) => {
                        this.fetchLists(payload.search).then().catch();
                        resolve(res);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            });
        },
    },
});
