import { ref } from "vue";
import { getCompanies, getPublicUsersCompany } from '../services/public';

export default function useDirectory() {
    const companies = ref([]);
    const loading = ref(false);
    const users = ref([]);
    const companiesTotal = ref(0);
    const currentCompaniesPage = ref(0);


    const getCompaniesData = async(filterQuery) => {
        let filterArr = Object.values(filterQuery);
        let cleanfilterArr =  filterArr.filter(n=>n);
        let filterStr = "";

        if(cleanfilterArr.length >0){
            filterStr = cleanfilterArr[0] + "?"
            filterStr += cleanfilterArr.slice(1).join("&");
        }

        loading.value = true;
        const resp = await getCompanies(filterStr);
        if(resp !== null) {
            companies.value = resp.data;
            companiesTotal.value = resp.total;
            console.log(companiesTotal.value)
        } else {
            companies.value = [];
            companiesTotal.value = 0;
        }
        loading.value = false;
    }

    const getUsersData = async(companyId) => {
        const filter = companyId;
        loading.value = true;
        const resp = await getPublicUsersCompany(filter);
        users.value = resp;
        loading.value = false;
    }

    return {
        getCompaniesData,
        getUsersData,
        users,
        loading,
        companies,
        companiesTotal,
        currentCompaniesPage
    }
}