// Generated by CoffeeScript 1.3.3
var __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', 'underscore', 'backbone', './channels'], function(environ, mediator, _, Backbone, channels) {
  var DataView, DataViews;
  DataView = (function(_super) {

    __extends(DataView, _super);

    function DataView() {
      return DataView.__super__.constructor.apply(this, arguments);
    }

    DataView.prototype.deferred = {
      save: true
    };

    DataView.prototype.url = function() {
      if (this.isNew()) {
        return DataView.__super__.url.apply(this, arguments);
      }
      return this.get('_links').self.href;
    };

    DataView.prototype.initialize = function() {
      var _this = this;
      DataView.__super__.initialize.apply(this, arguments);
      if (this.isArchived()) {
        this.resolve();
        return;
      }
      this.on('sync', function() {
        this.resolve();
        return mediator.publish(channels.DATAVIEW_SYNCED, this, 'success');
      });
      this.on('error', function() {
        return mediator.publish(channels.DATAVIEW_SYNCED, this, 'error');
      });
      this.on('change', function() {
        return mediator.publish(channels.DATAVIEW_CHANGED, this);
      });
      mediator.subscribe(channels.DATAVIEW_PAUSE, function(id) {
        if (_this.id === id || !id && _this.isSession()) {
          return _this.pending();
        }
      });
      mediator.subscribe(channels.DATAVIEW_RESUME, function(id) {
        if (_this.id === id || !id && _this.isSession()) {
          return _this.resolve();
        }
      });
      mediator.subscribe(channels.DATAVIEW_COLUMNS, function(id, columns) {
        var json;
        if (_.isArray(id)) {
          columns = id;
          id = null;
        }
        if (_this.id === id || !id && _this.isSession()) {
          json = _this.get('json') || {};
          json.columns = columns;
          _this.set('json', json);
          return _this.save();
        }
      });
      mediator.subscribe(channels.DATAVIEW_ORDERING, function(id, ordering) {
        var json;
        if (_.isArray(id)) {
          ordering = id;
          id = null;
        }
        if (_this.id === id || !id && _this.isSession()) {
          json = _this.get('json') || {};
          json.ordering = ordering;
          _this.set('json', json);
          return _this.save();
        }
      });
      return this.resolve();
    };

    DataView.prototype.isSession = function() {
      return this.get('session');
    };

    DataView.prototype.isArchived = function() {
      return this.get('archived');
    };

    DataView.prototype.toJSON = function() {
      return {
        id: this.id,
        json: this.get('json'),
        session: this.get('session'),
        archived: this.get('archived'),
        published: this.get('published')
      };
    };

    DataView.prototype.save = function() {
      DataView.__super__.save.apply(this, arguments);
      mediator.publish(channels.DATAVIEW_SYNCING, this);
      return this.pending();
    };

    return DataView;

  })(Backbone.Model);
  DataViews = (function(_super) {

    __extends(DataViews, _super);

    function DataViews() {
      return DataViews.__super__.constructor.apply(this, arguments);
    }

    DataViews.prototype.model = DataView;

    DataViews.prototype.initialize = function() {
      DataViews.__super__.initialize.apply(this, arguments);
      return this.on('reset', function(collection) {
        var model, _i, _len, _ref;
        this.resolve();
        _ref = collection.models;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          model = _ref[_i];
          model.trigger('sync');
        }
      });
    };

    DataViews.prototype.hasSession = function() {
      return !!(this.filter(function(model) {
        return model.get('session');
      }))[0];
    };

    return DataViews;

  })(Backbone.Collection);
  return {
    DataView: DataView,
    DataViews: DataViews
  };
});
