jQuery(function($) {

  "use strict";

  function product_quick_view_ajax(id) {

    $.ajax({

      url: nova_ajax_url,

      data: {
        "action": "nova_product_quick_view",
        'product_id': id
      },

      success: function(results) {
        $(".nova_wc_quickview__content").empty().html(results);
        $("body").removeClass("progress");
        if (nova_js_var.product_swatches == 'yes') {
          variation_swatches_plugin();
        }
        if (typeof $.fn.select2 === 'function') {
          $('#nova_wc_quickview .variations_form select').select2({
            minimumResultsForSearch: -1,
            placeholder: nova_js_var.select_placeholder,
            allowClear: true,
          });
        }
        if ($('.qv-carousel').length > 0) {
          $('.qv-carousel').slick();
        }

				$('#nova_wc_quickview .woocommerce-product-details__short-description').showMore({
						minheight: 100,
						buttontxtmore: nova_js_var.read_more_btn,
						buttontxtless: nova_js_var.read_less_btn,
						animationspeed: 250
				});
        setTimeout(function() {
          $('#nova_wc_quickview .variations_form').each(function() {
            $(this).wc_variation_form();
          });
        }, 1100);

        setTimeout(function() {
          $('#nova_wc_quickview .woocommerce-product-gallery').wc_product_gallery();
        }, 1000);

        setTimeout(function() {
          $(document.body).trigger('opened_product_quickview');
          $('#nova_wc_quickview').addClass('open');
        }, 500);

        setTimeout(function() {
          $('#nova_wc_quickview .nova_wc_quickview__content').addClass('maybe_scroll');
        }, 1200);
      },

    });
  }

  function variation_swatches_plugin() {
    $.fn.tawcvs_variation_swatches_form = function() {
      return this.each(function() {
        var $form = $(this),
          clicked = null,
          selected = [];

        $form
          .addClass('swatches-support')
          .on('click', '.swatch', function(e) {
            e.preventDefault();
            var $el = $(this),
              $select = $el.closest('.value').find('select'),
              attribute_name = $select.data('attribute_name') || $select.attr('name'),
              value = $el.data('value');

            $select.trigger('focusin');

            // Check if this combination is available
            if (!$select.find('option[value="' + value + '"]').length) {
              $el.siblings('.swatch').removeClass('selected');
              $select.val('').change();
              $form.trigger('tawcvs_no_matching_variations', [$el]);
              return;
            }

            clicked = attribute_name;

            if (selected.indexOf(attribute_name) === -1) {
              selected.push(attribute_name);
            }

            if ($el.hasClass('selected')) {
              $select.val('');
              $el.removeClass('selected');

              delete selected[selected.indexOf(attribute_name)];
            } else {
              $el.addClass('selected').siblings('.selected').removeClass('selected');
              $select.val(value);
            }

            $select.change();
          })
          .on('click', '.reset_variations', function() {
            $(this).closest('.variations_form').find('.swatch.selected').removeClass('selected');
            selected = [];
          })
          .on('tawcvs_no_matching_variations', function() {
            window.alert(wc_add_to_cart_variation_params.i18n_no_matching_variations_text);
          });
      });
    };

    $('.variations_form').tawcvs_variation_swatches_form();
    $(document.body).trigger('tawcvs_initialized');
  }

  function close_quickview_modal() {
    $('#nova_wc_quickview').removeClass('open');
    $('#nova_wc_quickview .nova_wc_quickview__content').removeClass('maybe_scroll');
    $('#nova_wc_quickview .nova_wc_quickview__content').empty();
    $(document.body).trigger('closed_product_quickview');
  }

  $('.site-content').on('click', '.nova_product_quick_view_btn', function(e) {
    e.preventDefault();
    close_quickview_modal();
    var product_id = $(this).data('product-id');
    product_quick_view_ajax(product_id);
  });

  $('#nova_wc_quickview').on('click', function(e) {
    var containers = [
      ".nova_wc_quickview__content"
    ];

    var container = $(containers.join(", "));

    if (!container.is(e.target) && container.has(e.target).length === 0) {
      close_quickview_modal();
    }
  });

  $('#nova_wc_quickview').on('click', 'button.close-button', function(e) {
    close_quickview_modal();
  });
});
