if (typeof wp !== 'undefined' && wp.i18n) {
	const { __ } = wp.i18n;
}


jQuery(document).ready(function ($) {
  "use strict";

  // Select-Unselect all place types
  $(document).on("click", "#bpchk-select-all-place-types", function () {
    var pt_names = [],
      i;
    var pt_options = plc_types_selectize.options;
    for (i in pt_options) {
      pt_names.push(pt_options[i]["value"]);
    }
    plc_types_selectize.setValue(pt_names);
  });
  $(document).on("click", "#bpchk-unselect-all-place-types", function () {
    plc_types_selectize.setValue([]);
  });

  // Support tab
  var acc = document.getElementsByClassName("wbcom-faq-accordion");
  var i;
  for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function () {
      this.classList.toggle("active");
      var panel = this.nextElementSibling;
      if (panel.style.maxHeight) {
        panel.style.maxHeight = null;
      } else {
        panel.style.maxHeight = panel.scrollHeight + "px";
      }
    };
  }
  $(document).on("click", ".wbcom-faq-accordion", function () {
    return false;
  });

  var latitude = "";
  var longitude = "";
  bpchk_get_current_geolocation();
  function bpchk_get_current_geolocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    } else {
      console.log( "Geolocation is not supported by your browser." );
    }
  }
  function showPosition(position) {
    latitude = position.coords.latitude;
    longitude = position.coords.longitude;
  }

  $(document).on("keyup", "#bpchk-api-key", function () {
    var apikey = $(this).val();
    if (apikey != "") {
      $("#bpchk-verify-apikey").show();
    } else {
      $("#bpchk-verify-apikey").hide();
    }
  });

  // Verify the api key
  $(document).on("click", "#bpchk-verify-apikey", function () {
    $(this).attr('disabled', 'disabled');
    $(this).children('.fa-times').remove();
    $(this).children('.fa-check').remove();
    var btn_val = $(this).html();
    var btn = $(this);
    var apikey = $("#bpchk-api-key").val();
    $(this).html(btn_val + ' <i class="fa fa-refresh fa-spin"></i>');

    var data = {
      action: "bpchk_verify_apikey",
      apikey: apikey,
      latitude: latitude,
      longitude: longitude,
      nonce: bpchk_admin_js_obj.ajax_nonce,
    };
    $.ajax({
      dataType: "JSON",
      url: bpchk_admin_js_obj.ajaxurl,
      type: "POST",
      data: data,
      success: function (response) {
        console.log( response["data"] );
        if (response['data']['error_response'] !== '' && response['data']['message'] == 'not-verified') {
          var html_response = '';
          for (const [key, value] of Object.entries(response['data']['error_response'])) {
            if (value.hasOwnProperty("error")) {
              html_response += '<p><i class="fa fa-times"><span class="admin-error-notice">' + key + ' : ' + value.error + '</span></i></p>';
            } else {
              html_response += '<p><i class="fa fa-check"><span class="admin-error-notice">' + key + ' : ' + value.success + '</span></i></p>';
            }
          }
          $('.bpchk-notice-wrapper').html(html_response);
        } else if (response["data"]["message"] == "not-verified") {
          $('.bpchk-notice-wrapper').html('<i class="fa fa-times"> <span class="admin-error-notice">Map API Key Status is Unverified. Please verify your API key to access map services.</span></i>');
            
        } else {
          $('.bpchk-notice-wrapper').html(' <i class="fa fa-check"> <span class="admin-error-notice">Map API key has been successfully verified and is ready for use.</span></i>');
             
        }
        $('#bpchk-verify-apikey').removeAttr('disabled');
        $('#bpchk-verify-apikey').html(btn_val);
      },
    });
  });
});
