/**
 * Freefactu Debug Invoice Page Script
 *
 * Handles copy and toggle functionality for debug page.
 *
 * @package Freefactu
 * @since 1.3.0
 */

(function () {
    'use strict';

    document.addEventListener('DOMContentLoaded', function () {
        // Copy hash button
        document.querySelectorAll('.copy-hash-btn').forEach(function (btn) {
            btn.addEventListener('click', function () {
                var hash = this.getAttribute('data-hash');
                navigator.clipboard.writeText(hash).then(function () {
                    var icon = btn.querySelector('.dashicons');
                    icon.classList.remove('dashicons-clipboard');
                    icon.classList.add('dashicons-yes');
                    setTimeout(function () {
                        icon.classList.remove('dashicons-yes');
                        icon.classList.add('dashicons-clipboard');
                    }, 1500);
                });
            });
        });

        // Toggle hash visibility button
        document.querySelectorAll('.toggle-hash-btn').forEach(function (btn) {
            btn.addEventListener('click', function () {
                var container = this.closest('.freefactu-hash-display').parentNode;
                var fullHash = container.querySelector('.hash-full');
                var icon = this.querySelector('.dashicons');

                if (fullHash.style.display === 'none' || fullHash.style.display === '') {
                    fullHash.style.display = 'block';
                    icon.classList.remove('dashicons-visibility');
                    icon.classList.add('dashicons-hidden');
                } else {
                    fullHash.style.display = 'none';
                    icon.classList.remove('dashicons-hidden');
                    icon.classList.add('dashicons-visibility');
                }
            });
        });

        // Toggle XML visibility
        document.querySelectorAll('.toggle-xml-btn').forEach(function (btn) {
            btn.addEventListener('click', function () {
                var container = this.parentNode.querySelector('.xml-container');
                var icon = this.querySelector('.dashicons');

                if (container.style.display === 'none') {
                    container.style.display = 'block';
                    icon.classList.remove('dashicons-visibility');
                    icon.classList.add('dashicons-hidden');
                } else {
                    container.style.display = 'none';
                    icon.classList.remove('dashicons-hidden');
                    icon.classList.add('dashicons-visibility');
                }
            });
        });

        // Copy XML button
        document.querySelectorAll('.copy-xml-btn').forEach(function (btn) {
            btn.addEventListener('click', function () {
                var xml = this.getAttribute('data-xml');
                navigator.clipboard.writeText(xml).then(function () {
                    var icon = btn.querySelector('.dashicons');
                    icon.classList.remove('dashicons-clipboard');
                    icon.classList.add('dashicons-yes');
                    setTimeout(function () {
                        icon.classList.remove('dashicons-yes');
                        icon.classList.add('dashicons-clipboard');
                    }, 1500);
                });
            });
        });
    });
})();
