 # Format of opts.referralProgram
 # Referral Program Object
 
-Shop.riot = riot
+Shop.riot      = riot
 Shop.analytics = analytics
-
-Shop.isEmpty = ->
+Shop.isEmpty   = ->
   items = @data.get 'order.items'
   items.length == 0
 
-getQueries = ()->
+getQueries = ->
   search = /([^&=]+)=?([^&]*)/g
   q = window.location.href.split('?')[1]
   qs = {}


  store.set 'referrer', referrer

  promo = queries.promo ? ''

  items     = store.get 'items'
  cartId    = store.get 'cartId'
  meta      = store.get 'order.metadata'

  @data = refer
    taxRates:       opts.taxRates || []

    riot.update()

  if referrer? && referrer != ''
    @client.referrer.get(referrer).then (res) =>
      promoCode = res.affiliate.couponId
      @data.set 'order.promoCode', promocode
      m.trigger Events.ForceApplyPromoCode
    .catch ->
  else if promo != ''
    @data.set 'order.promoCode', promo
    m.trigger Events.ForceApplyPromoCode

  ps = []
  for tag in tags
    p = new Promise (resolve) ->
      tag.one 'updated', ->
        resolve()
    ps.push p

  Promise.settle(ps).then ->
    requestAnimationFrame ->
      m.trigger Events.Ready
    #try to deal with long running stuff
    setTimeout ->
      riot.update()
    , 1000
  .catch (err) ->
    window?.Raven?.captureException err

  # quite hacky
  m.data = @data
  m.on Events.SetData, (@data) =>
    @cart.invoice()

  m.on Events.DeleteLineItem, (item) ->
    id = item.get 'id'
    if !id
      id = item.get 'productId'
