/* ScanVault Label Designer — Scanner Bridge JS
 * Injects a "Print (Designer)" button into the ScanVault scanner modal.
 * Only activates when the main ScanVault plugin modal is present.
 */
(function ($) {
    'use strict';

    var params = svld_bridge;

    // Wait for the ScanVault modal to open and inject our button
    $(document).on('click', '.wbs-print-btn-modal', function () {
        // The main plugin's print button was clicked — we intercept here only
        // if the user has explicitly chosen to use the Label Designer.
        // In bridge mode we add our OWN button instead of replacing the original.
    });

    // Inject button after modal product display renders
    $(document).on('wbs:product_displayed', function (e, data) {
        injectBridgeButton(data && data.product_id);
    });

    // Fallback: observe for the print button to appear in the modal
    var observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (m) {
            m.addedNodes.forEach(function (node) {
                if (node.nodeType !== 1) return;
                var $node = $(node);
                if ($node.hasClass('wbs-product-actions') || $node.find('.wbs-print-btn-modal').length) {
                    var productId = $('#wbs-product-id').val();
                    if (productId) injectBridgeButton(parseInt(productId));
                }
            });
        });
    });

    $(document).ready(function () {
        var modal = document.getElementById('wbs-scanner-modal');
        if (modal) {
            observer.observe(modal, { childList: true, subtree: true });
        }
    });

    function injectBridgeButton(productId) {
        if (!productId) return;
        if ($('#svld-bridge-print-btn').length) {
            $('#svld-bridge-print-btn').data('id', productId);
            return;
        }

        var templateHint = params.scanner_template_name
            ? ' <small style="opacity:.7;font-size:11px">(' + params.scanner_template_name + ')</small>'
            : '';

        var $btn = $('<button>', {
            id:    'svld-bridge-print-btn',
            type:  'button',
            class: 'wbs-btn wbs-btn-secondary',
            css:   { marginLeft: '6px' },
        }).html('<span class="dashicons dashicons-tag" style="vertical-align:middle;margin-top:1px;"></span> ' + params.btn_label + templateHint);

        $btn.data('id', productId);
        $('.wbs-print-btn-modal').after($btn);
    }

    // Handle bridge print button click
    $(document).on('click', '#svld-bridge-print-btn', function () {
        var productId = $(this).data('id') || $('#wbs-product-id').val();
        if (!productId) return;

        var $btn = $(this);
        $btn.prop('disabled', true).text(params.generating);

        $.post(params.ajax_url, {
            action:     params.print_action,
            nonce:      params.nonce,
            product_id: productId,
        }, function (res) {
            $btn.prop('disabled', false).html('<span class="dashicons dashicons-tag" style="vertical-align:middle;margin-top:1px;"></span> ' + params.btn_label);
            if (res.success && res.data.pdf_url) {
                window.open(res.data.pdf_url, '_blank');
            } else {
                alert(params.error);
            }
        }).fail(function () {
            $btn.prop('disabled', false).html('<span class="dashicons dashicons-tag" style="vertical-align:middle;"></span> ' + params.btn_label);
            alert(params.error);
        });
    });

}(jQuery));
