import api from "../lib/axios";

export const getLoadDirectories = async(filterStr) => {
    try {
        const endpoint = `/v1/public-loads${filterStr}`;
        const {data} = await api.get(endpoint);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const getVehicleDirectories = async(filter) => {
    try {
        const endpoint = `/v1/public-vehicles/published${filter}`;
        const {data} = await api.get(endpoint);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const getFreeVehicles = async() => {
    try {
        // console.log(process.env.API_URL + "/vehicles/?status=Free&updatedAt[$gt]=" + moment.utc(lasthour).valueOf());
        const endpoint = `/v1/public-vehicles/location`;
        console.log({endpoint});
        const {data} = await api.get(endpoint);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const getNews = async() => {
    try {
        const endpoint = `/news`;
        console.log({endpoint});
        const {data} = await api.get(endpoint);
        console.log(data);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const getCompanies = async(filter) => {
    try {
        const endpoint = `/v1/public-companies/${filter}`;
        console.log(endpoint);
        const {data} = await api.get(endpoint);
        console.log(data);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const getUsersCompany = async(filter) => {
    try {
        const endpoint = `/v1/users?${filter}`;
        // console.log({endpoint});
        const {data} = await api.get(endpoint);
        return data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const getPublicUsersCompany = async(filter) => {
    try {
        const endpoint = `/v1/public-companies/users/${filter}`;
        // console.log({endpoint});
        const {data} = await api.get(endpoint);
        // console.log(data);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const getSettingsQuery = async(filter) => {
    try {
        const endpoint = "/v1/meta-data/find?regex=" + filter.query;
        const {data} = await api.get(endpoint);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const searchcategories = async(query) => {
    try {
        const endpoint = "/v1/product-categories/find?regex=" + query;
        const {data} = await api.get(endpoint);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const searchProducts = async(query) => {
    try {
        const endpoint = "/products/?name[$regex]=" + query + "&name[$options]=i";
        const {data} = await api.get(endpoint);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const searchstates = async(query) => {
    try {
        const endpoint = "/v1/states/find?regex=" + query;
        const {data} = await api.get(endpoint);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}

export const searchcities = async(query) => {
    try {
        // const endpoint = "/cities/?city_name[$regex]=" + query + "&city_name[$options]=i";
        const endpoint = "/v1/cities/find?regex=" + query;
        const {data} = await api.get(endpoint);
        return data.data;
    } catch (error) {
        console.log(error);
        return [];
    }
}