import { defineStore } from "pinia";
import { ref, watch, onMounted } from "vue";
import { useAuthStore } from "./auth";
import { getCompany } from "../services/company";

export const useCompanyStore = defineStore('company', () => {

    const auth = useAuthStore();
    const company = ref(null)
    const loading = ref(false);

    const getCompanyData = async() => {
        loading.value = true;
        if(!company.value) {
            console.log('Se ejecuta');
            loading.value = true;
            const companyId = auth.user?.company;
            console.log({companyId});
            const resp = await getCompany(companyId);
            console.log(resp);
            company.value = resp;
        }
        loading.value = false;
    }

    const clear = () => {
        company.value = null;
        loading.value = false;
    }


   return {
        company,
        loading,
        getCompanyData,
        clear
   }
});