#
# Common utility functions.
#

$ = jQuery
context = window
context.JK ||= {};

class ModUtils
  constructor: () ->
    @logger = context.JK.logger

  init: () =>

  # creates a new show structure suitable for applying to a user update
  noShow: (noShowName) =>
    noShowValue = {}
    noShowValue[noShowName] = true
    {no_show: noShowValue}

  updateNoShow: (noShowName) =>
    context.JK.app.updateUserModel({mods: this.noShow(noShowName)})

  # returns a deferred, so use .done
  shouldShow: (noShowName) =>
    deferred = new $.Deferred();
    context.JK.app.user()
    .done((user) => (
        noShows = user.mods?.no_show
        shouldShowForName = if noShows? then !noShows[noShowName] else true
        deferred.resolve(shouldShowForName)
    ))
    return deferred;

  # returns a gear mod by name
  getGear: (name) =>
    gear = context.JK.app.currentUser().mods?.gear
    if gear? then gear[name] else undefined

# global instance
context.JK.ModUtils = new ModUtils()