(function (root, $, undefined) {
  "use strict";
  $(function () {
    $(".ajuda #bannerFAQ input").on("keyup", function (e) {
      let req = $(this).val();
      let res = getAllQuestionsFilter(script_ajax.questions, req);
      $("#faq .faqs").html('');
      let imageSpinner = new Image();
      imageSpinner.src = script_ajax.directory_uri +'\/img\/spiner.gif';
      imageSpinner.classList.add("spinner");
      $("#faq .faqs").append(imageSpinner);
      let html = "";
      let imagePlus = new Image();
      imagePlus.src = script_ajax.directory_uri +'\/img\/icons\/plus.svg';
      res.map(function (item,key) {
        html += `<div class="item-faq">
        <button type="button" data-bs-toggle="collapse" data-bs-target="#collapseFaq${key}" aria-expanded="false" aria-controls="collapseFaq${key}">${item.quest}</button>
        <div class="content collapse" id="collapseFaq${key}">
            <p>${item.answer}</p>
        </div>
    </div>`;
      });
      setTimeout(function () {
        if (html === '') {
          $("#faq .faqs").html('Pesquisa não encontrada');
        } else {
          $("#faq .faqs").html(html);
          $("#faq .faqs button").append(imagePlus);
        }
      }, 1000);
    });

    function getAllQuestionsFilter(questionsArr, searched) {
      let resArr = [];
      questionsArr.map(function (item) {
        if (item.quest.toLowerCase().indexOf(searched.toLowerCase()) > -1) {
          resArr.push(item);
        }
      });
      return resArr;
    }

    $('#singNowModal .close').click(function() {
      $.ajax({
        url: script_ajax.directory_uri + '/ajax/close-modal.php',
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            $('#singNowModal').remove();
        },
      })
    })
    document.addEventListener( 'wpcf7submit', function( event ) {
      if ( '252' == event.detail.contactFormId ) {
        $('#singNowModal').remove();
        $('#dflixModal').modal('hide');
        window.open("https://cursos.dflix.com.br/","_blank");
      }
    }, false );
  });
})(this, jQuery);
