/* eslint no-var: "error" */
/* eslint-env es6*/

import {call} from 'core/ajax';
import notification from 'core/notification';

// Prepare default value.
let settingcache = null;

/**
 * Check if premium status is enabled.
 * @param {String} key
 * @returns {*}
 */
export function settings(key) {
    const settings = loadsettings();
    if (key in settings) {
        return settings[key];
    } else {
        return null;
    }
}

/**
 * Get settings from server or cache
 * @returns {Object} The settings object
 */
function loadsettings() {
    if (!settingcache) {
        // Retrieve setting cache if needed.
        call([{
            methodname: 'local_treestudyplan_getsettings',
            args: {}
        }])[0].then((response) => {
            settingcache = response;
            return;
        }).catch(notification.exception);
    }
    return settingcache;
}

// Preload premium status.
loadsettings();