{{#not queryParams.dontRenderScriptTags}}<script>{{/not}}
var superAbTesting = window.superAbTesting || {}
superAbTesting.helper = (function(win, doc) {
  var testRndNr = 0

  var segmentCookie = 'tui-cookie',
      haveSegmentCookie = doc.cookie.match(/(^|;|; )tui-cookie=.+;?/gi) ? true : false,
      haveApicaCookie = doc.cookie.match(/(^|;|; )apica_test=test;?/gi) ? true : false,
      isApicaScenario = doc.location.search.match(/scenario=apica/gi) ? true : false,
      isCookieEnabled = navigator.cookieEnabled

  var getCookieFromUrl = function() {
    var url = typeof location === 'undefined' ? { search: '' } : location
    if(url && url.search && url.search.length > 0 && url.search.indexOf('?') > -1) {
      var queryParamString = url.search.replace('?', '')
      var queryParams = queryParamString.split('&')
      var match = null
      for ( var i = 0; i < queryParams.length; i++ ) {
        if(queryParams[i].toLowerCase().indexOf('tui-cookie') !== -1) {
          match = queryParams[i]
          break
        }
      }
      if (match) {
        var value = match.split('=')
        return decodeURIComponent(value[1])
      }
      return false
    }
    return false
  }

  var generateSegment = function() {
    var urlValue = getCookieFromUrl()
    testRndNr = urlValue || Math.floor(Math.random() * 100 + 1)
    return testRndNr
  }

  var getSegment = function() {
    var urlValue = getCookieFromUrl()
    var myNumber = urlValue || Number((((doc.cookie.match(/tui-cookie=[0-9]+/gi) || [])[0] || '').match(/\d+/gi) || [])[0] || '')
    if (myNumber && myNumber > 0 && myNumber <= 100) {
      return myNumber
    }
    return 0
  }

  var sendCookie = function(cookieValue) {
    var xhr = new XMLHttpRequest()
    xhr.open('POST', '/personalisation/cookie')
    xhr.setRequestHeader('Content-Type', 'application/json')
    xhr.send(JSON.stringify({ value: cookieValue }))
  }

  var shouldUpdateCookie = function(cookieSet) {
    try {
      return Date.now() - parseInt(cookieSet) > 3600 * 24 * 1000 // More than one day has passed
    } catch (e) {
      return true
    }
  }

  if (!haveApicaCookie && !isApicaScenario && isCookieEnabled) {
    if (!haveSegmentCookie) {
      testRndNr = generateSegment()
      localStorage.setItem('tuiCookieSet', Date.now())
      window.setTimeout(function() {
        sendCookie(testRndNr)
      }, 0)
    } else {
      testRndNr = getSegment()
      var cookieSet = localStorage.getItem('tuiCookieSet')
      if (!cookieSet || shouldUpdateCookie(cookieSet)) {
        localStorage.setItem('tuiCookieSet', Date.now())
        window.setTimeout(function() {
          sendCookie(testRndNr)
        }, 0)
      }
    }
  }

  var getDeviceType = function() {
    var width = window.innerWidth > 0 ? window.innerWidth : screen.width
    if (width < 768) {
      return 'Mobile'
    } else if (width > 767 && width < 992) {
      return 'Tablet'
    } else {
      return 'Desktop'
    }
  }

  var tests = []

  return {
    getSegment: function() {
      return testRndNr
    },
    getDeviceType: function() {
      return getDeviceType()
    },
    tests: tests
  }
})(window, document)
{{#not queryParams.dontRenderScriptTags}}</script>{{/not}}
