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

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

// Prepare default value.
let premiumstatusCache = null;

/**
 * Check if premium status is enabled.
 * @returns {Bool} True/False
 */
export function premiumenabled() {
    const premium = premiumstatus();
    if (premium) {
        return (premium.enabled) ? true : false;
    } else {
        return true; // Temporarily return true, that is easier than going full async.
    }
}

/**
 * Get premium status object
 * @returns {Object} The premium status object
 */
function premiumstatus() {
    if (!premiumstatusCache) {
        // Retrieve premium status if needed.
        call([{
            methodname: 'local_treestudyplan_premiumstatus',
            args: {}
        }])[0].then((response) => {
            premiumstatusCache = response;
            return;
        }).catch(notification.exception);
    }
    return premiumstatusCache;
}

// Preload premium status.
premiumstatus();