var win = window;
var doc = win.document;
var docEl = doc.documentElement;
var dpr = window.devicePixelRatio || 1;
var atomPixel = 1;
var rootFontSize = 0;
var scale = 0;
doc.addEventListener("touchstart", function () { }, true);
var _unlock = null;
var tid;
function resize() {
    clearTimeout(tid);
    tid = setTimeout(refreshRem, 300);
}
function pageshow(e) {
    if (e.persisted) {
        clearTimeout(tid);
        tid = setTimeout(refreshRem, 300);
    }
}
export function refreshRem() {
    if (!_unlock)
        return;
    var width = docEl.getBoundingClientRect().width;
    if (width / dpr > 980) {
        width = 980 * dpr;
    }
    rootFontSize = width / scale;
    docEl.style.fontSize = rootFontSize + 'px';
    atomPixel = width / (scale * 100);
}
export function lock(_scale) {
    scale = _scale || 7.5;
    if (!_unlock) {
        win.addEventListener('resize', resize, false);
        win.addEventListener('pageshow', pageshow, false);
        doc.addEventListener('DOMContentLoaded', refreshRem, false);
        _unlock = () => {
            win.removeEventListener('resize', resize, false);
            win.removeEventListener('pageshow', pageshow, false);
            doc.removeEventListener('DOMContentLoaded', refreshRem, false);
            docEl.style.fontSize = 'initial';
            _unlock = null;
            atomPixel = 1;
            rootFontSize = 0;
        };
    }
    refreshRem();
}
export function unlock() {
    if (_unlock)
        _unlock();
}
export default {
    get atomPixel() { return atomPixel; },
    get rootFontSize() { return rootFontSize; },
    refreshRem,
    initialize: function (_scale) {
        lock(_scale);
    },
};
