// Generated by CoffeeScript 1.3.3
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  __hasProp = {}.hasOwnProperty,
  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

define(['environ', 'mediator', 'jquery', 'underscore', 'backbone', 'serrano/channels'], function(environ, mediator, $, _, Backbone, channels) {
  var DataContextCount;
  return DataContextCount = (function(_super) {

    __extends(DataContextCount, _super);

    function DataContextCount() {
      this.render = __bind(this.render, this);
      return DataContextCount.__super__.constructor.apply(this, arguments);
    }

    DataContextCount.prototype.className = 'datacontext-count';

    DataContextCount.prototype.initialize = function() {
      var _this = this;
      if (this.model && this.model.id) {
        this.model = this.model.id;
      }
      mediator.subscribe(channels.DATACONTEXT_SYNCING, function(model) {
        if (model.isSession()) {
          return _this.$el.addClass('loading');
        }
      });
      return mediator.subscribe(channels.DATACONTEXT_SYNCED, function(model, status) {
        if (model.isSession()) {
          _this.$el.removeClass('loading');
          return _this.render(model);
        }
      });
    };

    DataContextCount.prototype.render = function(model) {
      var count, delimited, label, suffixed;
      if (model && ((count = model.get('count')) != null)) {
        suffixed = App.Numbers.toSuffixedNumber(count);
        delimited = App.Numbers.toDelimitedNumber(count);
        label = count === 1 ? model.get('object_name') : model.get('object_name_plural');
        this.$el.html("" + suffixed + " <span class=muted>" + (label || '') + "</span>").attr('title', delimited);
        if (count === 0) {
          this.$el.addClass('text-error');
        } else {
          this.$el.removeClass('text-error');
        }
      } else {
        this.$el.html('&infin;');
      }
      return this.$el;
    };

    return DataContextCount;

  })(Backbone.View);
});
