/*
Theme Name: Kalamullah Online
Theme URI: https://www.portotheme.com/wordpress/porto
Author: P-THEMES
Author URI: https://www.portotheme.com/
Description: Porto Responsive WordPress + eCommerce Theme.
Version: 1.1
Template: porto
License: Commercial
License URI: http://themeforest.net/licenses/regular_extended
Tags: woocommerce, corporate, ecommerce, responsive, blue, black, green, white, light, dark, red, two-columns, three-columns, four-columns, left-sidebar, right-sidebar, fixed-layout, responsive-layout, custom-menu, editor-style, featured-images, flexible-header, full-width-template, microformats, post-formats, rtl-language-support, sticky-post, theme-options, translation-ready, accessibility-ready
*/

/*************** ADD YOUR CUSTOM CSS HERE  ***************/


// === Make CF7 country dropdown searchable (Select2) ===
function elpha_enqueue_select2_for_cf7() {
  // Select2 CSS
  wp_enqueue_style(
    'select2',
    'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css',
    array(),
    '4.1.0-rc.0'
  );

  // jQuery + Select2 JS
  wp_enqueue_script(
    'select2',
    'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js',
    array('jquery'),
    '4.1.0-rc.0',
    true
  );

  // Init on CF7 fields
  wp_add_inline_script('select2', "
    jQuery(function($){
      function initCountrySelect(){
        var \$sel = $('#country_code');
        if(!\$sel.length || \$sel.hasClass('select2-hidden-accessible')) return;

        \$sel.select2({
          width: '100%',
          placeholder: \$sel.find('option:first').text() || 'Search country / code',
          allowClear: true,
          matcher: function(params, data){
            if ($.trim(params.term) === '') return data;
            if (typeof data.text === 'undefined') return null;
            var term = params.term.toLowerCase();
            var text = data.text.toLowerCase();
            // Search by country name OR dial code OR emoji
            return text.indexOf(term) > -1 ? data : null;
          }
        });

        // Move placeholder behavior: ensure blank option stays on top
        \$sel.prepend('<option></option>').trigger('change');
      }

      // Init on ready and also when CF7 renders forms via AJAX
      initCountrySelect();
      document.addEventListener('wpcf7mailsent', initCountrySelect);
      document.addEventListener('wpcf7invalid', initCountrySelect);
      document.addEventListener('wpcf7submit', initCountrySelect);
    });
  ");
}
add_action('wp_enqueue_scripts', 'elpha_enqueue_select2_for_cf7');
