#include utility.js

const FutamiCommon = function(vars) {
    vars = vars || {};

    const get = function(name, fallback) {
        return vars[name] || fallback || null;
    };

    return {
        get: get,
        toJSON: () => vars,
        getJson: async (name, noCache) => {
            const options = { type: 'json' };
            if(noCache)
                options.headers = { 'Cache-Control': 'no-cache' };

            const resp = await $x.get(get(name), options);
            return resp.body();
        },
    };
};

FutamiCommon.load = async url => {
    if(typeof url !== 'string' && 'FUTAMI_URL' in window)
        url = window.FUTAMI_URL + '?t=' + Date.now().toString();

    const resp = await $x.get(url, {
        type: 'json',
        headers: {
            'Cache-Control': 'no-cache'
        }
    });

    return new FutamiCommon(resp.body());
};
