import intlTelInput from 'intl-tel-input';
import 'intl-tel-input/build/js/utils';
import 'jquery-mask-plugin';

import countrySelect from 'country-select-bd';
import 'country-select-bd/build/css/countrySelect.min.css';


$(document).ready(function() {

  $("#user_registration_country , #user_adresse_country").countrySelect({
    defaultCountry: "tn",
    preferredCountries: ['tn','fr'],
    excludeCountries: ["il"],
    nationalMode: false,
    formatOnDisplay: true,           
    separateDialCode: true,
    autoHideDialCode: true, 
  });
  

  var input = document.querySelector("#user_registration_phoneNumber");
    

  if(input != null)  {
    var iti = intlTelInput(input, {
      nationalMode: false,
      formatOnDisplay: true,           
      separateDialCode: true,
      autoHideDialCode: true, 
      autoPlaceholder: "aggressive" ,
      initialCountry: "tn",
      placeholderNumberType: "MOBILE",
      preferredCountries: ['tn','fr'],
      excludeCountries: ["il"]
  })   

  var phoneInputID = "#user_registration_phoneNumber";

    $(phoneInputID).on("countrychange", function(event) {

        // Get the selected country data to know which country is selected.
        var selectedCountryData = iti.getSelectedCountryData();
        
        // Get an example number for the selected country to use as placeholder.
        var newPlaceholder = intlTelInputUtils.getExampleNumber(selectedCountryData.iso2, true, intlTelInputUtils.numberFormat.INTERNATIONAL);
        
        // Reset the phone number input.
        iti.setNumber("");
         
        // Convert placeholder as exploitable mask by replacing all 1-9 numbers with 0s
        var mask = newPlaceholder.replace(/[1-9]/g, "0"); 
    
        // Apply the new mask for the input
        $(this).mask(mask);
      });
    

      iti.promise.then(function() {
        $(phoneInputID).trigger("countrychange");
      });
    }
    


    var inputInfoPerson = document.querySelector("#user_info_perso_phoneNumber");

    if(inputInfoPerson != null)  {
      
      var iti = intlTelInput(inputInfoPerson, {
        nationalMode: false,
        formatOnDisplay: true,           
        separateDialCode: true,
        autoHideDialCode: true, 
        autoPlaceholder: "aggressive" ,
        initialCountry: "tn",
        placeholderNumberType: "MOBILE",
        preferredCountries: ['tn','fr'],
        excludeCountries: ["il"]
  
    })   

    $("#user_info_perso_phoneNumber").on("countrychange", function(event) {

        // Get the selected country data to know which country is selected.
        var selectedCountryData = iti.getSelectedCountryData();
        
        // Get an example number for the selected country to use as placeholder.
        var newPlaceholder = intlTelInputUtils.getExampleNumber(selectedCountryData.iso2, true, intlTelInputUtils.numberFormat.INTERNATIONAL);
        
        // Reset the phone number input.
        iti.setNumber("");
         
        // Convert placeholder as exploitable mask by replacing all 1-9 numbers with 0s
        var mask = newPlaceholder.replace(/[1-9]/g, "0"); 
    
        // Apply the new mask for the input
        $(this).mask(mask);
      });
    

      iti.promise.then(function() {
        $("#user_info_perso_phoneNumber").trigger("countrychange");
      });
    }
});
