
/**
 * Basic API interaction & local caching interface
 */
class IAPIObject {
    /**
     * Local storage key used to save data.
     * @type {string}
     */
    static storage_key = undefined;

    static _checkStorageSupport() {
        if (this.storage_key === undefined) {
            throw Error('This doesn\'t support local storage');
        }
    }

    _getStorageKey() {
        return Object.getPrototypeOf(this).constructor.storage_key;
    }

    _IcheckStorageSupport() {
        if (this._getStorageKey() === undefined) {
            throw Error('This doesn\'t support local storage');
        }
    }

    /** @returns {ThisType} */
    static restoreFromLocalStorage() {
        _checkStorageSupport();
        if (!window.sessionStorage.getItem(this.storage_key))
            return false;
        return new APIToken(window.sessionStorage.getItem(this.storage_key));
    }

    /** @returns {boolean} */
    static isCached() {
        this._checkStorageSupport();
        if (window.sessionStorage.getItem(this.storage_key)) {
            return true;
        }
        return false;
    }

    /**
     * Save this object to local storage
     * @throws {QuotaExceededError}
     * @returns {void}
     */
    saveToLocalStorage() {
        this._IcheckStorageSupport();

        window.sessionStorage.setItem(this._getStorageKey(), JSON.stringify(this));
    }
}

export { IAPIObject };