jQuery(document).ready(function () {
    jQuery('#new_account_btn').click(function() {
      jQuery('#integrazo-wczo-select_account_section').hide();
      jQuery('#integrazo-wczo-new_account_name').show();
      jQuery('#integrazo-wczo-new_account_fields').slideDown();
      jQuery('.integrazo-wczo-setup-step2-save-btn').hide();
      jQuery('#step2_auth').parent('div').show();
  });
  // -------------------------------------------------------- Common
  var urlParams = new URLSearchParams(window.location.search);
  var step = urlParams.get('step');

  if (step === '2') {
    jQuery('#step2 .integrazo-wczo-setup-accordion-content').slideDown();
    jQuery('#step2 .integrazo-wczo-setup-toggle-icon').text('−');
  } else if (step === '3') {
    jQuery('#step3 .integrazo-wczo-setup-accordion-content').slideDown();
    jQuery('#step3 .integrazo-wczo-setup-toggle-icon').text('−');
  } else {
    jQuery('#step1 .integrazo-wczo-setup-accordion-content').slideDown();
    jQuery('#step1 .integrazo-wczo-setup-toggle-icon').text('−');
  }

  jQuery('.integrazo-wczo-setup-accordion-header').on('click', function () {
    var content = jQuery(this).next('.integrazo-wczo-setup-accordion-content');
    var icon = jQuery(this).find('.integrazo-wczo-setup-toggle-icon');

    if (content.is(':visible')) {
      content.slideUp();
      icon.text('+');
    } else {
      jQuery('.integrazo-wczo-setup-accordion-content').slideUp();
      jQuery('.integrazo-wczo-setup-toggle-icon').text('+');
      content.slideDown();
      icon.text('−');
    }
  });

  // -------------------------------------------------------- Step 1: tile select (plugin/product)
// Works for BOTH free (.integrazo-wczo-setup-step1-plugin-item) and pro (.integrazo-wczo-pro-plugin).
// Pro tiles are treated as disabled: no selection action.
jQuery(document).on('click', '[data-group="product"]', function () {
  var $tile = jQuery(this);

// pro tile disabled
if ($tile.hasClass('integrazo-wczo-pro-plugin')) return;

var name      = String($tile.attr('data-name') || '');
var productId = String($tile.attr('data-id') || '');

// set product hidden input
jQuery('input[name="to_product"]').val(productId);

// highlight selection
jQuery('[data-group="product"]').removeClass('selected');
$tile.addClass('selected');

// update product selection box
var $box = jQuery('[data-target="#integrazo-wczo-product-popup"]');
$box.find('.integrazo-wczo-setup-step1-plugin-title').text(name);
$box.find('.integrazo-wczo-setup-step1-label').text('');

var $iconDiv = $box.find('div.integrazo-wczo-setup-step1-add-icon, div.integrazo-wczo-setup-step1-plugin-thumb');
$iconDiv.removeClass().addClass('integrazo-wczo-setup-step1-plugin-thumb ' + productId);

// close popup
jQuery('#integrazo-wczo-product-popup').removeClass('integrazo-wczo-setup-step1-fullscreen-popup').hide();
jQuery('#integrazo-wczo-product-popup-close').hide();

});


  // Validate buttons
jQuery('#step1_next').on('click', function (e) {
  var integrationName = String(jQuery('#integration_name').val() || '').trim();
  var selectedProduct = String(jQuery('[name="to_product"]').val() || '').trim();

  if (!integrationName) {
    alert('Please enter an Integration Name.');
    e.preventDefault();
    return false;
  }

  if (!selectedProduct) {
    alert('Please select an integration product.');
    e.preventDefault();
    return false;
  }

  return true;
});


  jQuery('#step1_save_only').on('click', function (e) {
    var integrationName = String(jQuery('#integration_name').val() || '').trim();
    if (!integrationName) {
      alert('Please enter an Integration Name.');
      e.preventDefault();
      return false;
    }
    return true;
  });

  // Open/Close popups
  jQuery('.integrazo-wczo-setup-step1-expand-toggle').on('click', function () {
    var targetSelector = jQuery(this).attr('data-target');
    var $container = jQuery(targetSelector);
    var $closeBtn = $container.find('.integrazo-wczo-setup-step1-popup-close');

    if ($container.hasClass('integrazo-wczo-setup-step1-fullscreen-popup')) {
      $container.removeClass('integrazo-wczo-setup-step1-fullscreen-popup').hide();
      $closeBtn.hide();
    } else {
      $container.addClass('integrazo-wczo-setup-step1-fullscreen-popup').show();
      $closeBtn.show();
    }

      // Re-run search to ensure empty state visibility on open (Product only)
    if (targetSelector === '#integrazo-wczo-product-popup') {
      runSearch(
        jQuery('#integrazo-wczo-product-popup-search'),
        '#integrazo-wczo-product-popup-list',
        'product'
      );
    }

  });

  jQuery('.integrazo-wczo-setup-step1-popup-close').on('click', function () {
    var $popup = jQuery(this).closest('.integrazo-wczo-setup-step1-overlay');
    $popup.removeClass('integrazo-wczo-setup-step1-fullscreen-popup').hide();
    jQuery(this).hide();
  });

  // -------------------------------------------------------- Search (both free + pro) with empty-state (no inline styles)
  var REQUEST_LINK = 'https://integrazo.com/pages/request-feature';

  function ensureEmptyState(listSelector) {
  var $list  = jQuery(listSelector);
  var cls    = 'integrazo-wczo-empty-state';
  var $empty = $list.children('.' + cls);

  if ($empty.length === 0) {
    var text = 'Your searched product is not available at the moment. Kindly raise your requirement at this link and we will try to add it as soon as possible.';

    $empty = jQuery(
      '<div class="' + cls + '">' +
        '<span class="integrazo-wczo-empty-state-text">' + text + '</span>' +
        '<a class="integrazo-wczo-empty-state-link" href="' + REQUEST_LINK + '" target="_blank" rel="noopener noreferrer">Raise your requirement</a>' +
      '</div>'
    );
    $list.append($empty);
  }

  return $empty;
}

  function runSearch($input, listSelector, group) {
    var $list   = jQuery(listSelector);
    var val     = String($input.val() || '').toLowerCase().trim();
    var $cards  = $list.find('[data-group="' + group + '"]');

    var visibleCount = 0;
    $cards.each(function () {
      var $el   = jQuery(this);
      var name  = String($el.attr('data-name') || '').toLowerCase();
      var id    = String($el.attr('data-id')   || '').toLowerCase();
      var title = String($el.find('.integrazo-wczo-setup-step1-plugin-title').text() || '').toLowerCase();
      var hay   = (name + ' ' + id + ' ' + title);
      var show  = (val === '' || hay.indexOf(val) !== -1);
      $el.toggle(show);
      if (show) visibleCount++;
    });

    var $empty = ensureEmptyState(listSelector);
    if (visibleCount === 0) {
      $empty.show();
    } else {
      $empty.hide();
    }
  }

  // Product popup search
  jQuery('#integrazo-wczo-product-popup-search').on('input', function () {
    runSearch(jQuery(this), '#integrazo-wczo-product-popup-list', 'product');
  });
});
