"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileTokenHandler = void 0;
const Config_1 = require("../Config");
const WizError_1 = require("../WizError");
const jsonwebtoken_1 = require("jsonwebtoken");
const TOKENS_KEY = 'tokens';
class FileTokenHandler {
    constructor() {
    }
    get() {
        return Config_1.Config.get()
            .then(result => result[TOKENS_KEY])
            .then((tokens) => {
            if (tokens) {
                return tokens;
            }
            throw new WizError_1.WizError('NOT_LOGGED_IN');
        });
    }
    set(data) {
        return Config_1.Config.set({
            ewizard: {
                apiUrl: getApiUrl(data.access_token)
            },
            [TOKENS_KEY]: filterData(data),
        });
    }
    clear() {
        return Config_1.Config.reset([TOKENS_KEY]);
    }
}
exports.FileTokenHandler = FileTokenHandler;
function filterData(data) {
    return {
        access_token: data.access_token,
        refresh_token: data.refresh_token,
    };
}
function getApiUrl(token) {
    const data = (0, jsonwebtoken_1.decode)(token);
    return data.apiUrl;
}
//# sourceMappingURL=FileTokenHandler.js.map