#include html.js

const $csrf = (() => {
    let elem;
    const getElement = () => {
        if(elem === undefined)
            elem = $query('meta[name="csrf-token"]');
        return elem;
    };

    return {
        get token() {
            return getElement()?.content ?? '';
        },
        set token(token) {
            if(typeof token !== 'string')
                throw 'token must be a string';

            const elem = getElement();
            if(elem instanceof HTMLMetaElement)
                elem.content = token;
        },
    };
})();