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 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);
        companies.value = resp;
        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
    }
}