/* global GOVUK */

(function () {
  'use strict'

  var root = this
  var $ = root.jQuery

  if (typeof root.GOVUK === 'undefined') { root.GOVUK = {} }

  $.expr[':'].contains = function (obj, index, meta) {
    return (obj.textContent || obj.innerText || '').toUpperCase().indexOf(meta[3].toUpperCase()) >= 0
  }

  var CountryFilter = function (input) {
    var enterKeyCode = 13
    var filterInst = this

    this.container = input.closest('.js-travel-container')
    input.keyup(function () {
      var filter = $(this).val()

      filterInst.filterListItems(filter)
      filterInst.track(filter)
    }).keypress(function (event) {
      // eslint-disable-next-line eqeqeq
      if (event.which == enterKeyCode) {
        event.preventDefault()
      }
    })

    $('.js-country-count', this.container).attr('aria-live', 'polite')

    $(document).bind('countrieslist', this.updateCounter)
  }

  CountryFilter.prototype.filterHeadings = function (countryHeadings) {
    var filterInst = this

    var headingHasVisibleCountries = function (headingFirstLetter) {
      var countries = filterInst.container.querySelector('#' + headingFirstLetter.toUpperCase()).querySelectorAll('li')
      var countryList = []

      for (var i = 0; i < countries.length; i++) {
        var innerVar = countries[i].style.display === 'none' ? countries[i] : undefined
        if (innerVar) { countryList.push(innerVar) }
      }

      return countryList.length < countries.length
    }

    for (var i = 0; i < countryHeadings.length; i++) {
      var header = countryHeadings[i].textContent.match(/[A-Z]{1}$/)[0]

      if (headingHasVisibleCountries(header)) {
        countryHeadings[i].parentNode.style.display = ''
      } else {
        countryHeadings[i].parentNode.style.display = 'none'
      }
    }
  }

  CountryFilter.prototype.doesSynonymMatch = function (elem, synonym) {
    var synonyms = elem.getAttribute('data-synonyms').split('|')
    var results = []

    for (var i = 0; i < synonyms.length; i++) {
      if (synonyms[i].toLowerCase().indexOf(synonym.toLowerCase()) > -1) {
        results.push(synonyms[i])
      }
    }

    return results
  }

  CountryFilter.prototype.filterListItems = function (filter) {
    var countryHeadings = this.container.querySelectorAll('h3.countries-initial-letter')
    var listItems = this.container.querySelectorAll('ul.js-countries-list li')

    var itemsShowing = 0
    var synonymMatch = false
    var filterInst = this
    var i = 0
    var listItem = null

    for (i = 0; i < listItems.length; i++) {
      listItem = listItems[i]
      var link = listItem.getElementsByTagName('a')[0]
      listItem.textContent = ''
      listItem.appendChild(link)
      listItem.style.display = ''
    }

    filter = filter.replace(/^\s+|\s+$/g, '')
    if (filter && filter.length > 0) {
      var hideCount = 0
      for (i = 0; i < listItems.length; i++) {
        listItem = listItems[i]
        if (listItem.children[0].firstChild.textContent.toLowerCase().includes(filter.toLowerCase())) {
          listItem.style.display = ''
        } else {
          listItem.style.display = 'none'
          hideCount += 1
        }
      }
      itemsShowing = listItems.length - hideCount

      for (i = 0; i < listItems.length; i++) {
        listItem = listItems[i]
        var synonyms = filterInst.doesSynonymMatch(listItem, filter)
        if (synonyms.length > 0) {
          synonymMatch = true
          listItem.style.display = ''
          for (var j = 0; j < synonyms.length; j++) {
            listItem.appendChild(document.createTextNode('(' + synonyms[j] + ') '))
          }
        }
      }

      if (synonymMatch) {
        itemsShowing = 0
        for (i = 0; i < listItems.length; i++) {
          if (listItems[i].style.display !== 'none') {
            itemsShowing += 1
          }
        }
      }
    } else {
      for (i = 0; i < countryHeadings.length; i++) {
        countryHeadings[i].style.display = ''
      }

      itemsShowing = listItems.length
    }

    this.filterHeadings(countryHeadings)
    this.updateCounter(itemsShowing)
  }

  CountryFilter.prototype.updateCounter = function (showingCount) {
    var counter = this.container.getElementsByClassName('js-country-count')[0]
    var filter = this.container.getElementsByClassName('js-filter-count')[0]

    filter.innerText = showingCount
    counter.innerHTML = counter.innerHTML.replace(/\s*results$/, '')
    // eslint-disable-next-line eqeqeq
    if (showingCount == 0) { // this is intentional type-conversion
      counter.appendChild(document.createTextNode(' results'))
    }
  }

  CountryFilter.prototype._trackTimeout = false

  CountryFilter.prototype.track = function (search) {
    clearTimeout(this._trackTimeout)
    var pagePath = this.pagePath()
    this._trackTimeout = root.setTimeout(function () {
      if (pagePath) {
        GOVUK.analytics.trackEvent('searchBoxFilter', search, { label: pagePath, nonInteraction: true })
      }
    }, 1000)
  }

  CountryFilter.prototype.pagePath = function () {
    window.location.pathname.split('/').pop()
  }

  GOVUK.countryFilter = CountryFilter

  var inputs = root.document.querySelectorAll('input#country')
  for (var i = 0; i < inputs.length; i++) {
    new GOVUK.countryFilter(inputs[i]) // eslint-disable-line new-cap, no-new
  }
}).call(this)

@@ -341,17 +341,23 @@
 nction (
-i
+searchI
 nput) %7B%0A
@@ -430,17 +430,23 @@
 ainer = 
-i
+searchI
 nput.clo
@@ -478,24 +478,51 @@
 r')%0A
+%0A
     
-input.
+searchInput.addEventListener('
 keyup
-(
+', 
 func
@@ -523,28 +523,125 @@
 , function (
+event
 ) %7B%0A
+      if (event.keyCode === enterKeyCode) %7B%0A        event.preventDefault()%0A      %7D else %7B%0A  
       var fi
@@ -651,23 +651,21 @@
 r = 
-$(
 this
-)
 .val
-()%0A%0A
+ue%0A  
     
@@ -703,24 +703,26 @@
 lter)%0A      
+  
 filterInst.t
@@ -742,215 +742,191 @@
     
-%7D).keypress(function (event) %7B%0A      // eslint-disable-next-line eqeqeq%0A      if (event.which == enterKeyCode) %7B%0A        event.preventDefault()%0A      %7D%0A    %7D)%0A%0A    $('.js-
+  %7D%0A    %7D)%0A%0A    if (this.container) %7B%0A      var countryCount = this.container.getElementsByClassName('js-country-count')%5B0%5D%0A      if (countryCount) %7B%0A        
 country
--c
+C
 ount
-', this.container).attr
+.setAttribute
 ('ar
@@ -949,66 +949,21 @@
 e')%0A
-%0A
     
-$(document).bind('countrieslist', this.updateCounter)
+  %7D%0A    %7D
 %0A  %7D

Remove jQ from CountryFilter