import backendRequest from 'lib/backend';

function _url(url, data) {
	return typeof url === "function" ? url(data) : url;
}

export function fetchRequest(url, modifyData) {
    let opts = {};
    if (typeof modifyData === "function") {
        opts.onResponseJson = modifyData;
    }
	return ({...data}) => backendRequest(_url(url, data), 'GET', opts);
}


export function createRequest(url, modifyData) {
    let opts = {};
    if (typeof modifyData === "function") {
        opts.onResponseJson = modifyData;
    }
	return (data, opts) => backendRequest(_url(url, { ...opts, ...data }), 'POST', { body: data, ...opts });
}


export function updateRequest(url, modifyData) {
    let opts = {};
    if (typeof modifyData === "function") {
        opts.onResponseJson = modifyData;
    }
	return (id, data, opts) => backendRequest(_url(url, { ...opts, ...data, id }), 'PUT', { body: data, ...opts });
}


export function deleteRequest(url, modifyData) {
	return ({...data}) => backendRequest(_url(url, data), 'DELETE');
}

export function formRequest(url, data, paramsFn) {
	return (data) => backendRequest(_url(data), 'GET', { params: typeof paramsFn === "function" ? paramsFn(data) : data })
}

