var wijmo;define(["./wijmo.data","jquery","breeze"],function(){var e=this.__extends||function(e,t){function r(){this.constructor=e}for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);r.prototype=t.prototype,e.prototype=new r};(function(t){(function(t){var n=function(n){function r(){n.apply(this,arguments),this._skipUpdatePredicate=!1}return e(r,n),r.prototype.toPredicate=function(e){var n=null;return e&&typeof e=="object"&&t.util.each(e,function(e,t){var i=r.opNameMap[t.op.name]||t.op.name,s=new breeze.Predicate(e,i,t.value);n=n?n.and(s):s}),n},r.prototype.onFilterChanged=function(e){n.prototype.onFilterChanged.call(this,e),this._skipUpdatePredicate||(this.predicate=this.toPredicate(this._compiledFilter.normalized))},r.prototype.setFilter=function(e){if(!(e instanceof breeze.Predicate)){n.prototype.setFilter.call(this,e);return}this.predicate=e,this._skipUpdatePredicate=!0;try{n.prototype.setFilter.call(this,null)}finally{this._skipUpdatePredicate=!1}},r.prototype.applyToQuery=function(e,t,n){typeof t=="undefined"&&(t=!0),typeof n=="undefined"&&(n=!0);var r=this._compiledSort;this.predicate&&(e=e.where(this.predicate));if(!r.isEmpty){var i=$.map(r.normalized,function(e){return e.property+(e.asc?"":" desc")}).join(", ");e=e.orderBy(i)}return t&&(this._skip>0&&(e=e.skip(this._skip)),this._take>0&&(e=e.take(this._take)),n&&(e=e.inlineCount(!0))),e},r.opNameMap={Greater:"GreaterThan",Less:"LessThan",GreaterOrEqual:"GreaterOrEqualThan",LessOrEqual:"LessOrEqualThan",BeginsWith:"StartsWith"},r}(t.Shape);t.BreezeShape=n;var r=function(r){function i(e,n,i){r.call(this),this.query=e,this.manager=n,this.options=i,e||t.errors.argumentNull("query"),n||t.errors.argumentNull("manager"),this.entityType=n.metadataStore.getEntityType(e.resourceName,!0),this._shape.entityType=this.entityType,this._construct(i)}return e(i,r),i.prototype._construct=function(e){e=$.extend({inlineCount:!0},e),r.prototype._construct.call(this,e)},i._breezeDataTypeToString=function(e){return e?e==breeze.DataType.Decimal?"number":e.isNumeric?"number":e==breeze.DataType.String?"string":e==breeze.DataType.DateTime||e==breeze.DataType.Time?"datetime":e==breeze.DataType.Boolean?"boolean":null:null},i.prototype.getProperties=function(){return this.entityType&&this.entityType.dataProperties?$.map(this.entityType.dataProperties,function(e){return{name:e.name,type:i._breezeDataTypeToString(e.dataType)}}):this.entityType?this.entityType.dataProperties:[]},i.prototype._createShape=function(e){return new n(e)},i.prototype._remoteRefresh=function(){var e=this,n=$.Deferred();this.shapedQuery=this._shape.applyToQuery(this.query,!this.localPaging,this.options.inlineCount);var r=this._currentRequest={canceled:!1};return this.manager.executeQuery(this.shapedQuery).fail(function(e){e&&t.util.logError(e),n.reject(e)}).then(function(i){if(r.canceled)return;r===e._currentRequest&&(e._currentRequest=null),e.sourceArray=i.results,t.util.isNumeric(i.inlineCount)&&e._totalItemCount(i.inlineCount),e._localRefresh().then(n.resolve)}),n.promise()},i.prototype.cancelRefresh=function(){this._currentRequest&&(this._currentRequest.canceled=!0)},i.prototype._beginEdit=function(e,n){(!e||!e.entityAspect)&&t.errors.breeze_notEntity(e),r.prototype._beginEdit.call(this,e,n)},i.prototype._currentItemHasChanges=function(){var e=this.currentEditItem(),t=e&&e.entityAspect,n;if(!t)return!1;for(n in t.originalValues)if(this.getProperty(this.currentEditItem,n)!==t.originalValues[n])return!0;return!1},i.prototype._ensureEntity=function(e){this.entityType||t.errors.breeze_entityTypeNotResolved(this.query.resourceName);if(!e||!e.entityAspect)e=this.entityType.createEntity(e);return e},i.prototype.canAddNew=function(){return!!this.entityType},i.prototype.addNew=function(e){return typeof e=="undefined"&&(e={}),this.add(e)},i.prototype.add=function(e){return r.prototype.add.call(this,this._ensureEntity(e))},i.prototype.commitEdit=function(){if(!this.currentEditItem())return;var e=this.currentEditItem(),t=this.isCurrentEditItemNew,n=this._currentItemHasChanges();r.prototype.commitEdit.call(this),t?this.manager.addEntity(e):n&&e&&e.entityAspect&&e.entityAspect.setModified()},i.prototype.cancelEdit=function(){if(!this.currentEditItem())return;var e=this.currentEditItem();r.prototype.cancelEdit.call(this),e&&e.entityAspect&&e.entityAspect.setDeleted()},i.prototype._remove=function(e){var t=e.item.entityAspect;return t&&t.setDeleted(),r.prototype._remove.call(this,e)},i.prototype.clone=function(){return new i(this.query,this.manager,$.extend(!0,{},this.options))},i}(t.RemoteDataView);t.BreezeDataView=r,t.errors._register({breeze_entityTypeNotResolved:"Entity type {0} not resolved",breeze_notEntity:"{0} is not a Breeze entity"})})(t.data||(t.data={}));var n=t.data})(wijmo||(wijmo={}))});