// Doghole Post
(function() {
    if (!window.Doghole) {
        window.Doghole = {};
    }
    let getPassElements = x => `.post-lock-input [type=${x}]`,
        Q = (x) => document.querySelector(x),
        b = Q(getPassElements('button')),
        o,
        t = Q('.post-pass-error');
    
    // Post password verification
    document.body.addEventListener('click', e => {
        if (e.target.classList.contains('post-lock-btn')) {
            if (!o) o = Q(getPassElements('password'))
            let c = 'btn-loading', btn = e.target,
            p = o.value, r = () => btn.classList.remove(c);
            btn.classList.add(c);
            if (!p) {
                return r(), t.textContent = '密码不能为空';
            }
            fetch(o.attributes.url.value, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: (() => {
                    var formData = new URLSearchParams();
                    formData.append('password', sha3_224(p));
                    return formData;
                })()
            })
            .then(resp => {
                if (!resp.ok) {
                    return r(), resp.json();
                }
                return r(), location.reload();
            })
            .then(json => {
                t.textContent = json.data;
            })
        }
    });
    
    // Post password input enter event
    document.body.addEventListener('keypress', e => {
        let t = e.target;
        if (e.charCode === 13 && t.tagName === 'INPUT' && t.parentNode.classList.contains('post-lock-input')) {
            b.click();
        }
    });
    
    // Post footnote tip
    document.querySelectorAll('.footnote-ref').forEach(e => {
        let href = e.attributes.href.value,
        t = Q(href).textContent.trim();
        t = t.substring(0, t.length - 1).trim();
        e.setAttribute('title', t);
    });
    
    
}).call(this);