jQuery(function ($) {
    'use strict';

    prestashop.addListener('updatedProduct', function (resp) {
        [
            "product_prices",
            "product_cover_thumbnails",
            "product_customization",
            "product_details",
            "product_variants",
            "product_discounts",
            "product_add_to_cart",
            "product_additional_info",
            "product_images_modal"
        ].forEach(function (prop) {
            var replacement = resp[prop];
            if (typeof replacement !== 'string') {
                return;
            }

            $(replacement).filter('[data-ajax-refresh]').each(function () {
                var selector = '.' + this.getAttribute('data-ajax-refresh');
                $(selector + ':not(' + selector + ' ' + selector + ')').replaceWith(this);
            });
        });
        _$(document).trigger('bd-refresh-thumbnails');
    });

    $('button[data-action="show-password"]').on('click', function () {
        var elm = $(this).closest('.input-group').children('input.js-visible-password');
        if (elm.attr('type') === 'password') {
            elm.attr('type', 'text');
            $(this).text($(this).data('textHide'));
        } else {
            elm.attr('type', 'password');
            $(this).text($(this).data('textShow'));
        }
    });

    $('body').on('click', '[data-button-action="add-to-cart"]', function () {
        prestashop.addListener('updateCart', function () {
            location.href = $('#add-to-cart-or-refresh').attr('action');
        });
    });
});