import {callAction} from "../utils/http-action";
import {ROUTES} from '../utils/routes';

export const CSRF = {
    token: '',
    getToken: function () {
        return this.token;
    }
}

export function getCSRFToken(promise) {
    if (!Wick.checkLocalEnv) {
        const payload = {
            url: ROUTES.GET_CSRF_TOKEN,
            type: 'GET',
        };

        callAction(payload, updateCSRFToken, promise);
    }
}

function updateCSRFToken(data, promise) {
    CSRF.token = data.csrfToken;
    promise && promise();
}
