 _ = require 'underscore'
 _s = require 'underscore.string'
 Backbone = require 'backbone'
+request = require 'superagent'
 sd = require('sharify').data
 Search = require './collections/search.coffee'
 mediator = require '../../lib/mediator.coffee'

    @search = new Search restrictType: @restrictType, mode: @mode, fairId: @fairId, size: @limit, agg: @enableAggregations

    @on 'search:start', @indicateLoading
    @on 'search:complete', @concealLoading
    @on 'search:complete', @maybeHighlight
    @on 'search:complete', @displayFeedback
    @on 'search:opened', @displaySuggestions
    @on 'search:closed', @hideSuggestions
    @on 'search:cursorchanged', @ensureResult

  indicateLoading: ->
    @renderFeedback()
    @$el.addClass 'is-loading'

  shouldMeasurePerformance: ->
    performance? and sd.VOLLEY_ENDPOINT

  recordPerformanceMeasurementStart: ->
    @performanceStart = performance.now()

  reportPerformanceMeasurement: ->
    duration = performance.now() - @performanceStart

    metricPayload = {
      type: "timing",
      name: "autocomplete-search-response",
      timing: duration,
      tags: [
        "device-type:desktop",
        "design:simple",
      ],
    }

    request
      .post(sd.VOLLEY_ENDPOINT)
      .send({ serviceName: "force", metrics: [metricPayload] })
      .end()

  concealLoading: ->
    @$el.removeClass 'is-loading'
