import axios from 'axios';
axios.defaults.baseURL = process.env.REACT_APP_API_URL;

export const getManufacturers = async () => {
    const {request, data} = await axios.get(
        '/manufacturers'
    );

    if (request.status === 200) {
        return data;
    }
    return [];
}

export const deleteManufacturer = async (bearer, id) => {
    const {request} = await axios.delete(
        `/manufacturers/${id}`,
        {
            headers: {
                "Authorization": `Bearer ${bearer}`
            },
        }
    )

    return request.status === 200;
}

export const createManufacturer = async (bearer, manufacturer) => {
    const {request, data} = await axios.post(
        '/manufacturers',
        {
            ...manufacturer
        },
        {
            headers: {
                "Authorization": `Bearer ${bearer}`
            }
        }
    )
    if (request.status === 200) {
        return data;
    }
    return undefined;
}

export const updateManufacturer = async (bearer, manufacturer) => {
    const {request} = await axios.put(
        `manufacturers/${manufacturer.id}`,
        {
            ...manufacturer
        },
        {
            headers: {
                "Authorization": `Bearer ${bearer}`
            }
        }
    )
    return request.status === 200;
}
