/**
 * TamRank — full attachment edit screen layout.
 *
 * Moves the TamRank attachment fields (Exclude-from-sitemap toggle + AI Alt
 * Text generator) out of the main column's compat-fields table and into a
 * side-column box directly under the WordPress "Save" box. The actual DOM
 * node is moved (not cloned), so event handlers and the inline card styling
 * bound to the .compat-field-* wrappers are preserved, and the inputs stay
 * inside the #post form so saving is unaffected. The media-insert modal uses
 * the same attachment_fields_to_edit fields and is left untouched.
 */
(function () {
    function run() {
        var table  = document.querySelector('.compat-attachment-fields');
        var submit = document.getElementById('submitdiv');
        if (!table || !submit) {
            return;
        }
        // Only relocate when our own fields are actually present.
        if (!table.querySelector('.compat-field-tamrank_ai_alt, .compat-field-tam_rank_exclude_from_sitemap')) {
            return;
        }
        // Defensive against a double run.
        if (document.getElementById('tamrank-media-fields')) {
            return;
        }

        var box = document.createElement('div');
        box.id = 'tamrank-media-fields';
        box.className = 'postbox';

        var header = document.createElement('div');
        header.className = 'postbox-header';
        var h2 = document.createElement('h2');
        h2.className = 'hndle';
        h2.style.cursor = 'default';
        h2.textContent = 'TamRank SEO';
        header.appendChild(h2);

        var inside = document.createElement('div');
        inside.className = 'inside';
        inside.appendChild(table); // move the real node

        box.appendChild(header);
        box.appendChild(inside);

        // Place directly after the Save box.
        submit.parentNode.insertBefore(box, submit.nextSibling);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', run);
    } else {
        run();
    }
})();
