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

    document.querySelectorAll("[data-copy-target]").forEach(button => {
        button.addEventListener("click", function () {
            const inputId = this.getAttribute("data-copy-target");
            copyToClipboard(inputId);
        });
    });

});

function copyToClipboard(inputId) {
    const input = document.getElementById(inputId);
    const rootStyles = getComputedStyle(document.documentElement);
    const background = rootStyles.getPropertyValue('--swal-bg')?.trim();
    const color = rootStyles.getPropertyValue('--swal-text-color')?.trim();

    if (!input) return;

    navigator.clipboard.writeText(input.value)
        .then(() => {
            Swal.fire({
                background: background,
                color: color,
                title: __("copied"),
                text: __("copy_text_success"),
                icon: "success",
                confirmButtonText: __("ok")
            });
        })
        .catch(err => {
            console.error("Clipboard error:", err);

            Swal.fire({
                background: background,
                color: color,
                title: "Error!",
                text: "Failed to copy the content.",
                icon: "error",
                confirmButtonText: "OK"
            });
        });
}