import {retryAction} from "../components/retry-action";
import {generateUrlWithQuery} from "./url-query";

const routes = {
    origin: window.location.origin,
}

export function callAction(payload, callback, ...args) {
    $.ajax({
        url: `${routes.origin}${generateUrlWithQuery(payload.url, payload.queries)}`,
        method: payload.type,
        headers: payload.headers ? payload.headers : {},
        data: payload.data ? payload.data : {},
        retryLimit: payload.retryLimit ? payload.retryLimit : 0,
        success: function (data) {
            callback(data, ...args);
        },
        error: function (xhr) {
            if (this.retryLimit) {
                retryAction(payload.error, this.retryLimit, xhr, ...args);
            } else {
                payload.error && payload.error(xhr, ...args);
            }
        },
        complete: payload.complete
    });
}

