'use strict';

(function ($, Drupal, drupalSettings) {

  Drupal.behaviors.locationFacetsFilter = {
    attach: function (context, settings) {

      $(document).ready(function (e) {

        // Add parent and child classes to formally define hierarchy.
        if ($('.block-facet-blocklocation .facets-widget-checkbox').length) {
          $('.block-facet-blocklocation .facets-widget-checkbox > ul > li').addClass('location-facet-parent');
        }
        if ($('.facet-item.location-facet-parent .facets-widget-').length) {
          $('.facet-item.location-facet-parent .facets-widget- > ul > li').addClass('location-facet-child');

          $('.location-facet-child').attr('data-default-visibility', 'hidden');
        }

        // When page loads, if any child facet is already selected, expand the parent, otherwise hide all children under that parent.
        if ($("#field-search-facets-location").length && $("#field-search-facets-location").val().trim.length == 0) {
          $('.facet-item.location-facet-parent').each(function (key, element) {
            if ($(this).hasClass('facet-item--active-trail')) {
              $(this).children('.facets-widget-').find('.facet-item.location-facet-child').attr('style', 'display: block');
              $(this).removeClass('facet-item--collapsed').addClass('facet-item--expanded');
            }
            else {
              $(this).children('.facets-widget-').find('.facet-item.location-facet-child').attr('style', 'display: none');
              $(this).removeClass('facet-item--expanded').addClass('facet-item--collapsed');
            }

            // If parent is checked, but none of the children are, we still show all children.
            if($("input", this).prop('checked') == true) {
              $(this).children('.facets-widget-').find('.facet-item.location-facet-child').attr('style', 'display: block');
              $(this).removeClass('facet-item--collapsed').addClass('facet-item--expanded');
            }

          });
        }

        $("#field-search-facets-location").on('change paste keyup blur', function (event) {
          var search_string = $(this).val();

          // Search through the locations as the user starts typing in the search box.
          // If a match is found, unhide that location. otherwise keep them hidden.
          var regExp = new RegExp(search_string, "i");
          $('.block-facet-blocklocation .facet-item').each(function () {
            var facet_text = $(this).text().trim();
            var matches = !! facet_text.match(regExp);
            $(this).toggle(matches);
          });

          // If search box is empty, then revert the facets to their default state of visibility.
          if (search_string.trim().length == 0) {
            $('.facet-item.location-facet-parent').each(function (key, element) {
              if ($(this).hasClass('facet-item--active-trail')) {
                $(this).children('.facets-widget-').find('.facet-item.location-facet-child').attr('style', 'display: block');
                $(this).removeClass('facet-item--collapsed').addClass('facet-item--expanded');
              }
              else {
                $(this).children('.facets-widget-').find('.facet-item.location-facet-child').attr('style', 'display: none');
                $(this).removeClass('facet-item--expanded').addClass('facet-item--collapsed');
              }
            });
          }

        });

      });
    }
  };
})(jQuery, Drupal, drupalSettings);