
async function ajaxRequest(url, method, data) {
    const options = {
        url: url,
        method: method,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("X-CSRFToken", CSRF_MiddlewareToken);
        }
    }
    
    if (data) {
        options.data = data;
        options.processData = false;
        options.contentType = false;
    }

    try {
        const response = await $.ajax(options);
        return response
    }
    catch (error) {
        return error
    }
}

// Saved Guilds

async function getSavedGuilds() {
    return await ajaxRequest("/api/saved-guilds/", "GET");
}

async function getSavedGuild(id) {
    return await ajaxRequest(`/api/saved-guilds/${id}/`, "GET");
}

async function newSavedGuild(formData) {
    return await ajaxRequest("/api/saved-guilds/", "POST", formData);
}

async function deleteSavedGuild(id) {
    return await ajaxRequest(`/api/saved-guilds/${id}/`, "DELETE");
}

// Loading Guilds

async function loadGuilds() {
    return await ajaxRequest("/guilds/", "GET");
}

// Loading Channels
async function loadChannels(guildId) {
    return await ajaxRequest(`/channels?guild=${guildId}`, "GET");
}

// Subscriptions

async function getSubscriptions(guildId, page=1, pageSize=10) {
    const url = `/api/subscription/?guild_id=${guildId}&page=${page}&page_size=${pageSize}`;
    console.debug(url);
    return await ajaxRequest(url, "GET");
}

async function getSubscription(id) {
    return await ajaxRequest(`/api/subscription/${id}/`, "GET");
}

async function newSubscription(formData) {
    return await ajaxRequest("/api/subscription/", "POST", formData);
}

async function deleteSubscription(id) {
    return await ajaxRequest(`/api/subscription/${id}/`, "DELETE");
}

async function editSubscription(id, formData) {
    return await ajaxRequest(`/api/subscription/${id}/`, "PUT", formData);
}


// SubChannels

async function getSubChannels(subscriptionId) {
    return await ajaxRequest(`/api/subchannel/?subscription=${subscriptionId}`, "GET");
}

async function getSubChannel(id) {
    return await ajaxRequest(`/api/subchannel/${id}/`, "GET");
}

async function newSubChannel(formData) {
    return await ajaxRequest("/api/subchannel/", "POST", formData);
}

async function deleteSubChannel(id) {
    return await ajaxRequest(`/api/subchannel/${id}/`, "DELETE")
}

async function deleteSubChannels(subscriptionId) {
    return await ajaxRequest(`/api/subscription/${subscriptionId}/subchannels/`, "DELETE");
}


// Filters

async function getFilters(guildId) {
    return await ajaxRequest(`/api/filter/?guild_id=${guildId}`, "GET");
}

async function getFilter(id) {
    return await ajaxRequest(`/api/filter/${id}/`, "GET");
}

async function newFilter(formData) {
    return await ajaxRequest("/api/filter/", "POST", formData);
}

async function deleteFilter(id) {
    return await ajaxRequest(`/api/filter/${id}/`, "DELETE");
}

async function editFilter(id, formData) {
    return await ajaxRequest(`/api/filter/${id}/`, "PUT", formData);
}

async function getFilterOptions() {
    return await ajaxRequest("/api/filter/", "OPTIONS")
}


// Tracked Content

async function getTrackedContent(guildId, subscriptionId=null, page=1, pageSize=10) {
    var url = `/api/tracked-content/?subscription__guild_id=${guildId}&page=${page}&page_size=${pageSize}&ordering=-creation_datetime`;
    if (subscriptionId)
        url += `&subscription=${subscriptionId}`;

    return await ajaxRequest(url, "GET");
}

async function deleteTrackedContent(guid) {
    const encodedGuid = encodeURIComponent(guid);
    return await ajaxRequest(`/api/tracked-content/${encodedGuid}/`, "DELETE");
}

// Mutators

async function getMutators() {
    return await ajaxRequest("/api/article-mutator/?page_size=25", "GET");
}