(function() {
    'use strict';

    function is_displaying(elt) {
        const r = elt.getBoundingClientRect();
        return r.width > 0 || r.height > 0;
    };

    function element_replace_with_placeholder(elt, className) {
        const rect = elt.getBoundingClientRect();
        const div = document.createElement('div');
        div.style.width = rect.width + 'px';
        div.style.height = rect.height + 'px';
        if (className != undefined) {
            div.className = className;
        };
        elt.replaceWith(div);
        return div;
    };

    // 縺薙�繧ケ繧ッ繝ェ繝励ヨ縺悟ョ溯。後&繧後k譎らせ縺ァrecaptcha v2縺瑚、�焚縺ゅk蝣エ蜷医r蜃ヲ逅�☆繧�
    function set_placeholder() {
      const melt = document.querySelector('form:not(form[id="quick-reply"]) .g-recaptcha');
        const qelt = document.querySelector('form[id="quick-reply"] .g-recaptcha');

        if (qelt == null) {
            return null;
        };

        if (melt.querySelector('.g-recaptcha-response') !== null) {
          return element_replace_with_placeholder(qelt, 'g-recaptcha-placeholder');
        } else {
          return element_replace_with_placeholder(melt, 'g-recaptcha-placeholder');
        };
    };

    // quick-reply縺ョ蜃コ迴セ繧貞セ�▽縲�
    function phase1() {
        if (document.getElementById('quick-reply') !== null) {
            phase2();
            return;
        };
        const observer = new MutationObserver(mutations => {
            for (const mutation of mutations) {
                for (const addedNode of mutation.addedNodes) {
                    if (addedNode.id === 'quick-reply') {
                        phase2();
                        observer.disconnect();
                        return;
                    };
                };
            };
        });

        observer.observe(document.body, {childList: true});
    };

    // quick-reply蜃コ迴セ蠕後�g-recaptcha縺ョ遘サ蜍輔r陦後≧.
    function phase2() {
        const mainform = document.querySelector('form:not(form[id="quick-reply"])');
        const quickform = document.querySelector('form[id="quick-reply"]');
        const observer = new MutationObserver(mutations => {
            if (quickform.style.display === 'none') {
                activate_recaptcha(mainform);
            } else {
                activate_recaptcha(quickform);
            };
        });

        set_placeholder();
        activate_recaptcha(quickform);
        observer.observe(quickform, { attributes: true, attributeFilter: ['style'] });
    };

    function activate_recaptcha(form) {
        if (form.getElementsByClassName('g-recaptcha').length > 0) {
            return;
        };
        const recaptcha = document.querySelector('.g-recaptcha');
        const next = recaptcha.nextSibling;
        const parent = recaptcha.parentElement;
        const placeholder = document.querySelector('.g-recaptcha-placeholder');
        placeholder.parentElement.insertBefore(recaptcha, placeholder);
        parent.insertBefore(placeholder, next);
    };

    if (document.readyState === "loading") {

      document.addEventListener("DOMContentLoaded", function() {

        phase1();
      });

    } else {

      phase1();
    };

})();