import { PLUGIN_PATH } from "../config";

export const DATASETS_URL = PLUGIN_PATH + "datasets/datasets.php";

/**
 * Returns the json object from the stored graph as promise.
 *
 * @param {String} spaceId Identification of graph to load.
 * 
 * @returns Promise returning graph object
 */
export function loadGraphJson(spaceId) {
    return fetch(DATASETS_URL + "?space_id=" + spaceId)
        .then((r) => r.json());
}

/**
 * Takes the graph json object and stores it in the backend.
 *
 * @param {String} spaceId Identification of graph to save.
 * @param {object} json Graph object
 */
export function saveGraphJson(spaceId, json) {
    var payload = {
        space_id: spaceId,
        graph: JSON.stringify(json),
    };

    var auth = getAuthPayload();
    if (auth === undefined) {
        return undefined;
    }

    return fetch(DATASETS_URL, {
        method: "POST",
        body: JSON.stringify(Object.assign(payload, auth)),
    });
}

function getAuthPayload() {
    //! TODO: Implement auth
    return {};
}