
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
    }
}

function makeQuerystring(filters, sort) {
    let querystring = "?";
    for (key in filters) {
        querystring += `${key}=${filters[key]}&`;
    }
    return querystring += `ordering=${sort}`;
}

// 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(filters, sort) {
    let querystring = makeQuerystring(filters, sort);
        url = `/api/subscription/${querystring}`;
    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);
}

async function getSubscriptionOptions() {
    return await ajaxRequest("/api/subscription/", "OPTIONS")
}


// 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(filters, sort) {
    let querystring = makeQuerystring(filters, sort);
        url = `/api/filter/${querystring}`;
    return await ajaxRequest(url, "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(filters, sort) {
    let querystring = makeQuerystring(filters, sort);
        url = `/api/tracked-content/${querystring}`;
    return await ajaxRequest(url, "GET");
}

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

async function getTrackedContentOptions() {
    return await ajaxRequest("/api/tracked-content/", "OPTIONS")
}

// Mutators

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

// guild settings

async function getGuildSettings(guildId) {
    return await ajaxRequest(`/api/guild-settings/?guild_id=${guildId}`, "GET");
}

async function editGuildSettings(id, formData) {
    return await ajaxRequest(`/api/guild-settings/${id}/`, "PUT", formData);
}
