import { ref } from "vue";
import api from "../lib/axios";

export default function useSearchLoads() {
    const loads = ref([]);
    const loading = ref(false);

    const getLoadsPublished = async(filterQuery) => {
        loading.value = true;
        let filterArr = Object.values(filterQuery);
        let cleanfilterArr =  filterArr.filter(n=>n);
        var filterStr = "";
        if(cleanfilterArr.length >0){
            filterStr = "?"+cleanfilterArr.join("&");
        }
        try {
            const endpoint = `/loads/${filterStr}`;
            const {data} = await api.get(endpoint);
            console.log(data);
            loads.value = data.data;
        } catch (error) {
            loads.value = [];
            console.log(error);
        }
        loading.value = false;
    }

    return {
        getLoadsPublished,
        loading,
        loads
    }
}