text: /**
   * @license
   * Copyright (c) 2014, 2019, Oracle and/or its affiliates.
   * The Universal Permissive License (UPL), Version 1.0
   * @ignore
   */
  define(["ojs/ojcore","jquery","ojs/ojconfig","ojs/ojlogger"],function(t,e,o,n){"use strict";function r(t){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}t.Events=t.Events={on:function(t,e,o){return this.OnInternal(t,e,o,!1,!1)},off:function(t,e,o){return this._offInternal(t,e,o,!1)},trigger:function(e){var o=Array.prototype.slice.call(arguments);return o.unshift(!1),t.Events.TriggerInternal.apply(this,o)},once:function(t,e,o){return this._onceInternal(t,e,o,!1,null)},listenTo:function(e,o,n){var r,i,s,l,a,c,u,h={};for(u in o.constructor===String?h[o]=n:h=o,h)if(h.hasOwnProperty(u))for(r=t.Events._getEvents(u),i=0;i<r.length;i+=1)c={event:s=r[i].event,attribute:l=r[i].attribute,object:e,callback:h[u]},a=l?s+":"+l:s,void 0===this._listeningTo&&(this._listeningTo=[]),this._listeningTo.push(c),e.OnInternal(a,h[u],null,!0,!1);return this},listenToOnce:function(e,o,n){var r,i,s,l,a,c,u,h={};for(u in o.constructor===String?h[o]=n:h=o,h)if(h.hasOwnProperty(u))for(r=t.Events._getEvents(u),i=0;i<r.length;i+=1)c={event:s=r[i].event,attribute:l=r[i].attribute,object:e,callback:h[u]},a=l?s+":"+l:s,void 0===this._listeningTo&&(this._listeningTo=[]),this._listeningTo.push(c),e._onceInternal(a,h[u],null,!0,this);return this},stopListening:function(e,o,n){var r,i,s,l,a,c,u,h,p,d,f,_,g,v={};if(null==arguments||arguments.length<=1){for(_=this._listeningTo?this._listeningTo.length:0,f=0;f<_;f++)c=this._listeningTo[f],(u=!e||e===c.object)&&c.object._offInternal.apply(c.object,[c.event,c.callback,c.context,!0]);return this._listeningTo=[],this}for(g in i=o,e&&e.constructor===String&&(i=e),i.constructor===String?v[i]=n:v=i,v)if(v.hasOwnProperty(g))for(r=t.Events._getEvents(g),s=0;s<r.length;s+=1)for(l=r[s].event,a=r[s].attribute,f=(_=this._listeningTo?this._listeningTo.length:0)-1;f>=0;f-=1)c=this._listeningTo[f],u=!e||e===c.object,h=!l||l===c.event,p=!n||v[g]===c.callback,d=!a||a===c.attribute,u&&h&&p&&d&&(this._listeningTo[f].object._offInternal.apply(this._listeningTo[f].object,[this._listeningTo[f].event,this._listeningTo[f].callback,this._listeningTo[f].context,!0]),this._listeningTo.splice(f,1));return this}},t.Events.bind=t.Events.on,t.Events.unbind=t.Events.off,t.Events.EventType={ADD:"add",ALLADDED:"alladded",REMOVE:"remove",RESET:"reset",REFRESH:"refresh",SORT:"sort",CHANGE:"change",DESTROY:"destroy",ALLREMOVED:"allremoved",REQUEST:"request",SYNC:"sync",ERROR:"error",INVALID:"invalid",READY:"ready",ALL:"all"},t.Events.Mixin=function(t,e){var o,n=e||this;for(o in n)"function"==typeof n[o]&&(t[o]=n[o]);t.eventHandlers={},t._listeningTo=[]},t.Events._onceInternal=function(t,e,o,n,r){var i,s,l,a,c,u,h;u=this._getEventMap(t,e,o),c=u.map,h=u.context;var p=this;return Object.keys(c||{}).forEach(function(t){if(Object.prototype.hasOwnProperty.call(c,t))for(i=p._getEvents(t),s=0;s<i.length;s+=1)l=i[s].event,a=i[s].attribute,void 0===p.eventHandlers&&(p.eventHandlers=[]),void 0===p.eventHandlers[l]&&(p.eventHandlers[l]=[]),p.eventHandlers[l].push({callback:c[t],context:h,attribute:a,once:!0,fired:!1,listen:n,otherObj:r})}),this},t.Events._shouldFire=function(t){return!t.once||!t.fired&&(t.fired=!0,!0)},t.Events._getContext=function(t,e){return e.context||e.otherObj||t},t.Events.TriggerInternal=function(e,o){var n,r,i,s,l,a,c,u,h,p=this._getEvents(o);for(s=[],n=0;n<p.length;n+=1)r=p[n].event,i=p[n].attribute,s.push({event:r,attribute:i});for(n=0;n<s.length;n+=1){for(u=this._getHandlers(this.eventHandlers,t.Events.EventType.ALL),l=t.Events._getHandlers(this.eventHandlers,s[n].event,!1),a=0;a<(l?l.length:0);a+=1)l[a].attribute===s[n].attribute&&l[a].callback&&(c=Array.prototype.slice.call(arguments),l&&l[a]&&l[a].once&&(this._removeHandler(t.Events._getHandlers(this.eventHandlers,s[n].event,!0),l[a]),l[a].otherObj&&l[a].otherObj.stopListening(this,o,l[a].callback)),l&&l[a]&&this._shouldFire(l[a])&&(h=l[a].callback,e&&!l[a].ignoreSilent||h.apply(t.Events._getContext(this,l[a]),c.slice(2))));for(a=0;a<(u?u.length:0);a+=1)(c=Array.prototype.slice.call(arguments)).length>0&&(s[n].attribute?c[1]=s[n].event+":"+s[n].attribute:c[1]=s[n].event),u&&u[a]&&u[a].callback&&this._shouldFire(u[a])&&(h=u[a].callback,e&&!u[a].ignoreSilent||h.apply(t.Events._getContext(this,u[a]),c.slice(1))),u&&u[a]&&u[a].once&&(this._removeHandler(this._getHandlers(this.eventHandlers,t.Events.EventType.ALL,!0),u[a]),u[a].otherObj&&u[a].otherObj.stopListening(this,t.Events.EventType.ALL,u[a].callback))}return this},t.Events.OnInternal=function(e,o,n,r,i){var s,l,a,c,u,h,p,d,f=this._getEventMap(e,o,n);for(d in s=f.map,p=f.context,s)if(s.hasOwnProperty(d))for(l=this._getEvents(d),a=0;a<l.length;a+=1)c=l[a].event,u=l[a].attribute,void 0===this.eventHandlers&&(this.eventHandlers=[]),void 0===this.eventHandlers[c]&&(this.eventHandlers[c]=[]),h={callback:s[d],context:p,attribute:u,listen:r,ignoreSilent:i},-1===this._checkForHandler(this.eventHandlers[c],h,t.Events._handlersIdentical)&&this.eventHandlers[c].push(h);return this},t.Events._offInternal=function(t,e,o,n){var r,i,s,l;if(null==arguments||0===arguments.length)return this.eventHandlers={},this;if(null==t)return this._removeEvent(t,e,o,n),this;for(l in r=(i=this._getEventMap(t,e,o)).map,s=i.context,r)r.hasOwnProperty(l)&&this._removeEvent(l,r[l],s,n);return this},t.Events._getEventMap=function(t,e,o){var n={};return t.constructor!==String?{map:n=t,context:e}:(n[t]=e,{map:n,context:o})},t.Events._removeEvent=function(e,o,n,r){var i,s,l,a,c,u,h,p,d,f=[];if(e)f=t.Events._getEvents(e);else if(void 0!==this.eventHandlers){var _=this;Object.keys(this.eventHandlers||{}).forEach(function(t){Object.prototype.hasOwnProperty.call(_.eventHandlers,t)&&f.push({event:t})})}for(i=0;i<f.length;i+=1)if(s=f[i].event,a=f[i].attribute,void 0!==this.eventHandlers&&this.eventHandlers[s]instanceof Array){for(l=(c=this.eventHandlers[s]).length-1;l>=0;l-=1)u=null==o||c[l].callback===o,h=null==n||c[l].context===n,p=null==a||c[l].attribute===a,d=null==r||c[l].listen===r,u&&h&&p&&d&&c.splice(l,1);0===c.length&&delete this.eventHandlers[s]}},t.Events._removeHandler=function(t,e){var o,n,r,i,s,l;if(t)for(o=t.length-1;o>=0;o-=1)n=void 0===e.callback||null===e.callback||t[o].callback===e.callback,r=void 0===e.context||null===e.context||t[o].context===e.context,i=void 0===e.attribute||null===e.attribute||t[o].attribute===e.attribute,s=void 0===e.listen||null===e.listen||t[o].listen===e.listen,l=void 0===e.once||null===e.once||t[o].once===e.once,n&&r&&i&&s&&l&&t.splice(o,1)},t.Events._getEvents=function(t){var e,o,n,r,i=t?t.split(" "):[],s=[];for(e=0;e<i.length;e+=1)n=(o=i[e].split(":"))[0],r=o.length>1?o[1]:null,s.push({event:n,attribute:r});return s},t.Events._handlersIdentical=function(t,e){return t.callback===e.callback&&t.attribute===e.attribute&&t.context===e.context&&t.listen===e.listen&&t.once===e.once},t.Events._listenersIdentical=function(t,e){return t.event===e.event&&t.attribute===e.attribute&&t.context===e.context&&t.object===e.object},t.Events._checkForHandler=function(t,e,o){var n;if(void 0===t)return-1;for(n=0;n<t.length;n+=1)if(o(t[n],e))return n;return-1},t.Events._getHandlers=function(t,e,o){if(t&&t[e]instanceof Array){if(o)return t[e];var n,r=[];for(n=0;n<t[e].length;n++)r.push(t[e][n]);return r}return null},t.Collection=function(e,o){t.Collection._justExtending||t.Collection._init(this,e,o,null)},t.Object.createSubclass(t.Collection,t.Object,"oj.Collection"),t.Collection.prototype.model=null,t.Collection.prototype.modelId=function(t){var e=this.model;return e&&t?t[e.idAttribute||e.prototype.idAttribute||"id"]:null},t.Collection.prototype.length=void 0,t.Collection.prototype.models=void 0,t.Collection.prototype._modelIndices=[],t.Collection.prototype.url=null,t.Collection.prototype.changes=[],t.Collection.prototype.customURL=null,t.Collection.prototype.customPagingOptions=null,t.Collection.prototype.lastFetchSize=void 0,t.Collection.prototype.hasMore=!1,t.Collection.prototype.totalResults=void 0,t.Collection.prototype.lastFetchCount=void 0,t.Collection.prototype.modelLimit=-1,t.Collection.prototype.offset=void 0,t.Collection.prototype.fetchSize=-1,t.Collection.prototype.sortDirection=1,t.Collection.prototype.comparator=null,t.Collection.prototype.omitLanguageHeader=!1,t.Collection.prototype.Init=function(){t.Collection.superclass.Init.call(this)},t.Collection.extend=function(o,n){var r,i=null;return t.Collection._justExtending=!0,i=new t.Collection,t.Collection._justExtending=!1,e.extend(i,this.prototype),r=o&&o.constructor&&Object.prototype.hasOwnProperty.call(o,"constructor")?o.constructor:function(e,n){t.Collection._init(this,e,n,o)},n&&Object.keys(n).forEach(function(t){Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}),o&&Object.keys(o).forEach(function(t){Object.prototype.hasOwnProperty.call(o,t)&&(i[t]=o[t])}),e.extend(r,this),r.prototype=i,r.extend=t.Collection.extend,r.prototype.constructor=r,r},t.Collection._init=function(e,o,n,r){var i,s,l,a;if(e.Init(),t.Events.Mixin(e),r)for(a in r)Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a]);var c=n||{};for(s=["comparator","customPagingOptions","customURL",t.Collection._FETCH_SIZE_PROP,"model","modelLimit","sortDirection","url"],i=0;i<s.length;i++)Object.prototype.hasOwnProperty.call(c,s[i])&&void 0!==c[s[i]]&&(e[s[i]]=c[s[i]]);void 0===e._getFetchSize(null)&&e.setFetchSize(-1),void 0===e.modelLimit&&e.setModelLimit(-1),e.hasMore=!1,e.lruCount=0,e._setModels([],!0);var u=o;c.parse&&(u=e.parse(o)),null!=u&&(c.noparse=!0,l=u instanceof Array?u:[u],e._addInternal(l,c,!0,!1)),e._setLength(),u||(e.totalResults=void 0),r&&r.initialize&&r.initialize.call(e,u,c)},t.Collection.prototype.on=function(t,e){},t.Collection.prototype.OnInternal=function(t,e,o,n,r){},t.Collection.prototype.TriggerInternal=function(t,e,o,n,r){},t.Collection.prototype._fireRequest=function(e,o,n,r){this.TriggerInternal(r,t.Events.EventType.REQUEST,e,o,n)},t.Collection.prototype._resetChanges=function(){this.changes=[]},t.Collection.prototype._setChangeAt=function(t,e){for(var o=t;o<t+e;o++)-1===this.changes.indexOf(o)&&this.changes.push(o)},t.Collection.prototype._setModels=function(t,e){if(this.models=t,e)this._modelIndices=[],this._resetChanges();else for(var o=0;o<t.length;o++)t[o]&&this._modelIndices.push(o)},t.Collection.prototype._getModels=function(){return this.models},t.Collection.prototype._getModelsLength=function(){return this._getModels().length},t.Collection.prototype._overUpperLimit=function(t){return!(t<this._getModelsLength())&&!(this.IsVirtual()&&(!this._hasTotalResults()||0===this._getModelsLength()))},t.Collection.prototype._hasTotalResults=function(){return t.Collection._defined(this.totalResults)},t.Collection._defined=function(t){return null!=t},t.Collection.prototype._pushModel=function(t){this._getModels().push(t),this._modelIndices.push(this._getModelsLength()-1),this._setChangeAt(this._getModelsLength()-1,1)},t.Collection.prototype._pushModels=function(t){this._makeModelHead(t),this._pushModel(t),this.lruCount=this.lruCount+1,t.SetIndex(this._getModelsLength()-1)},t.Collection.prototype._reduceLRU=function(t){if(t)for(var e=0;e<t.length;e++)t[e]&&(this.lruCount=this.lruCount-1)},t.Collection.prototype._spliceModels=function(t,e,o){for(var n=t;n<t+e;n++)this._removePrevNext(this._getModel(n));void 0===o?(this._reduceLRU(this._getModels().splice(t,e)),this._spliceModelIndices(t,t+e-1)):(this._reduceLRU(this._getModels().splice(t,e,o)),this._spliceModelIndices(t,t+e-1),this._insertModelIndex(t),this._makeModelHead(o)),this._setChangeAt(t,e),this.lruCount<0&&(this.lruCount=0),this._realignModelIndices(t)},t.Collection.prototype._getModel=function(t){return this._getModels()[t]},t.Collection.prototype._realignModelIndices=function(t){for(var e,o=0;o<this._modelIndices.length;o++)(e=this._modelIndices[o])>=t&&this._getModel(e)&&this._getModel(e).SetIndex(e)},t.Collection.prototype._removePrevNext=function(t){if(t){var e=t.GetPrevious(),o=t.GetNext();e?e.SetNext(o):this.head=o,o?o.SetPrevious(e):this.tail=e}},t.Collection.prototype._makeModelHead=function(t){t.SetNext(this.head),this.head?this.head.SetPrevious(t):this.tail=t,t.SetPrevious(null),this.head=t},t.Collection.prototype._setModelIndex=function(t){-1===this._modelIndices.indexOf(t)&&this._modelIndices.push(t)},t.Collection.prototype._insertModelIndex=function(t){for(var e=0;e<this._modelIndices.length;e++)this._modelIndices[e]>=t&&(this._modelIndices[e]=this._modelIndices[e]+1);this._modelIndices.push(t)},t.Collection.prototype._spliceModelIndices=function(t,e){var o=void 0===e?t:e;this._clearModelIndices(t,o);for(var n=o-t+1,r=0;r<this._modelIndices.length;r++)this._modelIndices[r]>o&&(this._modelIndices[r]-=n)},t.Collection.prototype._clearModelIndices=function(t,e){for(var o=void 0===e?t:e,n=t;n<=o;n++){var r=this._modelIndices.indexOf(t);r>-1&&this._modelIndices.splice(r,1)}},t.Collection.prototype._setModel=function(t,e){var o=this._getModel(t);this._removePrevNext(o),o||(this.lruCount=this.lruCount+1),this._setChangeAt(t,1),this._getModels()[t]=e,e&&(this._setModelIndex(t),e.SetIndex(t),this._makeModelHead(e))},t.Collection.prototype._clearOutModels=function(t){var e,o,n=this.tail,r=0;for(this.tail=null;n&&r<t;)e=n.GetIndex(),(o=this._getModel(e))&&o.hasChanged()?(this.tail||(this.tail=n),n=n.GetPrevious()):(this.lruCount=this.lruCount-1,e>-1&&(this._setModel(e,void 0),this._clearModelIndices(e,e)),n.SetNext(null),n=n.SetPrevious(null),r+=1);this.tail||(this.tail=n),this.lruCount<0&&(this.lruCount=0),0===this.lruCount&&(this.head=null,this.tail=null)},t.Collection.prototype._resetLRU=function(){this.lruCount=0,this.head=null,this.tail=null},t.Collection.prototype._manageLRU=function(t){if(this.IsVirtual()){var e=this._getModelLimit();e>-1&&this.lruCount+t>e&&this._clearOutModels(this.lruCount+t-e)}},t.Collection.prototype.clone=function(){return this._cloneInternal(!0)},t.Collection.prototype._cloneInternal=function(t){var e,o,n=new this.constructor;if(this.IsVirtual()&&(n=this._copyFetchProperties(n))._resetModelsToFullLength(this.totalResults),n=this._copyProperties(n),t){var r,i=[];for(e=0;e<this._modelIndices.length;e++)i.push(this._modelIndices[e]);for(i.sort(function(t,e){return t-e}),e=0;e<i.length;e++)r=i[e],(o=this._atInternal(r,null,!0,!1))&&n._addInternal(o.clone(),{at:r},!0,!1)}return n},t.Collection.prototype._copyProperties=function(t){var e,o,n=["comparator","model","modelId"];for(o=0;o<n.length;o++)t[e=n[o]]=this[e];return t},t.Collection.prototype._copyFetchProperties=function(e){var o,n,r=["totalResults","hasMore",t.Collection._FETCH_SIZE_PROP];for(n=0;n<r.length;n++)e[o=r[n]]=this[o];return e},t.Collection.prototype._getLength=function(){return this.length},t.Collection.prototype._setLength=function(){var t=this._getModelsLength();this.length=t,this.IsVirtual()||(this.totalResults=t)},t.Collection._createModel=function(t,o,n){return t.model?e.isFunction(t.model)?new t.model(o,n):new t.model.constructor(o,n):null},t.Collection.prototype._newModel=function(e,o,n,r){var i,s=null,l=n||{};return l.ignoreDefaults=r,e instanceof t.Model?s=e:this.model?s=t.Collection._createModel(this,e,l):(l.collection=this,s=new t.Model(e,l)),l.validate&&s.validate&&(i=s.validate(s.attributes))?(l.validationError=i,this.TriggerInternal(!1,t.Events.EventType.INVALID,s,i,l),null):s},t.Collection.prototype.add=function(t,e){this._manageLRU(1);var o=e||{};return this._handlePromise(this._addInternal(t,e,!1,o.deferred))},t.Collection.prototype._addInternal=function(e,o,r,i){var s,l,a,c=o||{},u=[],h=c.at,p=c.silent,d=c.force,f=c.merge||!1,_=c.sort,g=!0,v=!1,y=[],C=[];function m(e,o,i,s,u){var C,m=null;return!d&&f&&i?(g=i.Merge(o,e.comparator,p),m=i):(d||(C=r&&s.isNew()?void 0:e._getLocal(s))&&r&&h!==C.index&&n.warn("Duplicate ID fetched or added without merging, the id = "+C.GetId()),void 0===C?(!function(t,e){void 0===h?(t._pushModels(e),l=t._getModelsLength()-1,t._getModel(l).SetCid()):(l=h,t.IsVirtual()&&r?t._setModel(l,e):t._spliceModels(l,0,e),t._getModel(l).SetCid(),h+=1),void 0===e.GetCollection()&&e.SetCollection(t),t._setLength(),t._listenToModel(e),v=!0}(e,s),m=s):m=C),function(e,o,n,i,s){var u=c||{};r&&(u.fillIn=!0);var d=u.comparator||e._hasComparator();if((!r||r&&d)&&g&&void 0===o&&!_&&void 0===h&&e._getLength()>1){l>-1&&(a=e._getModel(l).cid);var f={};t.CollectionUtils.copyInto(f,u),f.add=!0,e.sort(f),l>-1&&(l=e.IsVirtual()?-1:e.indexOf(e.getByCid(a),s))}v&&(u.at&&(u.index=l),i?(i.TriggerInternal(p,t.Events.EventType.ADD,i,e,u),y.push(i)):(n.TriggerInternal(p,t.Events.EventType.ADD,n,e,u),y.push(n)))}(e,C,i,s,u),m}function I(e,o,n){v=!1;var i=e._newModel(o,!0,c,!1),s=null,a=null;if(null!=i){if(l=-1,i.SetupId(),s=o instanceof t.Model?o:i,n)return d?new Promise(function(t){var o=m(e,s,void 0,i,n);C.push(o),t(o)}):e._getInternal(s,{silent:!0},n,!0).then(function(t){a=t.m;var o=m(e,s,a,i,n);C.push(o)});!d&&f&&(a=r?e._getLocal(s):e.get(s));var u=m(e,s,a,i,n);u&&C.push(u)}else C.push(!1);return Promise.resolve()}function M(t,e){return t.parse&&c.parse&&!c.noparse?t.parse(e):e}if(void 0!==h&&h<0&&(h+=this._getLength()+1),e instanceof Array?u=e:u.push(e),!r&&(this.IsVirtual()||i)){var E=this;return new Promise(function(e,o){var n=function(t){return new Promise(function(e,o){I(E,u[t],!0).then(function(){e(t+1)},o)})},r=Promise.resolve(0);for(u=M(E,u),s=0;s<u.length;s++)r=r.then(n);r.then(function(){y.length>0&&E.TriggerInternal(c.silent,t.Events.EventType.ALLADDED,E,y,c),e(t.Collection._returnModels(C))},o)})}for(u=M(this,u),s=0;s<u.length;s++)I(this,u[s],!1);return y.length>0&&this.TriggerInternal(c.silent,t.Events.EventType.ALLADDED,this,y,c),t.Collection._returnModels(C)},t.Collection._returnModels=function(t){return 1===t.length?t[0]:t},t.Collection.prototype._hasComparator=function(){return t.Collection._defined(this.comparator)},t.Collection.prototype.sort=function(e){var o,n,r=e||{},i=r.silent,s=this.comparator;if(!this._hasComparator())return null;if(this.IsVirtual()){var l=this.totalResults;this._hasTotalResults()?this._setModels(new Array(l),!0):(this._setModels([],!0),this._resetLRU(),this._setLength()),n=r.add?{add:!0}:null,this.TriggerInternal(i,t.Events.EventType.SORT,this,n,null);var a=r.startIndex;return null!=a?this.setRangeLocal(a,this._getFetchSize(r)):null}return o=this,this._getModels().sort(function(e,n){return t.Collection.SortFunc(e,n,s,o,o)}),this._realignModelIndices(0),n=r.add?{add:!0}:null,this.TriggerInternal(i,t.Events.EventType.SORT,this,n,null),null},t.Collection._getKey=function(e,o){return e instanceof t.Model?e.get(o):t.Model.GetPropValue(e,o)},t.Collection.SortFunc=function(o,n,r,i,s){var l,a,c,u;if(e.isFunction(r)){if(1===r.length){l=r.call(s,o),a=r.call(s,n);var h=t.StringUtils.isString(l)?l.split(","):[l],p=t.StringUtils.isString(a)?a.split(","):[a];for(c=0;c<h.length;c++)if(0!==(u=t.Collection._compareKeys(h[c],p[c],i.sortDirection)))return u}return r.call(s,o,n)}if(t.StringUtils.isString(r)){var d=r.split(",");for(c=0;c<d.length;c++)if(l=t.Collection._getKey(o,d[c]),a=t.Collection._getKey(n,d[c]),0!==(u=t.Collection._compareKeys(l,a,i.sortDirection)))return u}return 0},t.Collection.prototype.sortedIndex=function(o,n){var r,i,s=n||this.comparator;return s?(this._throwErrIfVirtual("sortedIndex"),r=this,i=function(o,n){var i,l;if(e.isFunction(s)){if(1===s.length){i=s.call(r,o),l=s.call(r,n);var a,c,u=t.StringUtils.isString(i)?i.split(","):[i],h=t.StringUtils.isString(l)?l.split(","):[l];for(c=0;c<u.length;c++)if(0!==(a=t.Collection._compareKeys(u[c],h[c],r.sortDirection)))return a}return s.call(r,o,n)}return t.StringUtils.isString(s)?(i=o.get(s),l=n.get(s),t.Collection._compareKeys(i,l,r.sortDirection)):0},t.Collection._find(this._getModels(),o,i)):-1},t.Collection._find=function(t,e,o){return function n(r,i){var s,l,a;return r>i?-1:(s=e.GetCid(),l=e.GetId(),t[r].Match(l,s)?r:t[i].Match(l,s)?i:(a=Math.floor((i+r)/2),-1===o(t[a],e)?n(r+1,a):1===o(t[a],e)?n(a,i-1):a))}(0,t.length-1)},t.Collection._compareKeys=function(t,e,o){if(-1===o){if(t<e)return 1;if(e<t)return-1}else{if(t>e)return 1;if(e>t)return-1}return 0},t.Collection.prototype.unshift=function(e,o){var n={};return t.CollectionUtils.copyInto(n,o||{}),n.at||(n.at=0),this._manageLRU(1),this._handlePromise(this._addInternal(e,n,!1,n.deferred))},t.Collection.prototype._handlePromise=function(t){return e.isFunction(t.then)?this._addPromise(function(){return t}):t},t.Collection.prototype.shift=function(e){var o,n=e||{},r=this._getDeferred(n);if(this.IsVirtual()||r){var i=this;return this._atInternal(0,n,!1,!0).then(function(e){return o=i._removeInternal(e,0,n),i.TriggerInternal(n.silent,t.Events.EventType.ALLREMOVED,i,[o],n),o})}return o=this._removeInternal(this.at(0),0,n),this.TriggerInternal(n.silent,t.Events.EventType.ALLREMOVED,this,[o],n),o},t.Collection.prototype.initial=function(t){var e=void 0===t?1:t;this._throwErrIfVirtual("initial");var o,n=[];for(o=0;o<this._getLength()-e;o+=1)n.push(this.at(o));return n},t.Collection.prototype._getDeferred=function(t){return(t||{}).deferred},t.Collection.prototype.last=function(t,e){var o,n=this._getDeferred(e),r=void 0===t?1:t;if(1===r)return 0===(o=this._getModelsLength())&&(o=r),o>0?this._atInternal(o-1,e,!1,n):null;var i,s=[];if(o=this._getLength(),n||this.IsVirtual()){var l=o-t;l<0&&(l=0),0===o&&(o=t);var a=this;return this._addPromise(function(){return a.IterativeAt(l,o)})}for(i=o-t;i<o;i+=1)s.push(this.at(i));return s},t.Collection.prototype.IterativeAt=function(t,e){var o,n=[],r=this;return new Promise(function(i,s){var l=function(t){return r.IsVirtual()&&r._hasTotalResults()&&t>=r.totalResults?Promise.resolve(t+1):new Promise(function(e,o){r._deferredAt(t,null).then(function(o){n.push(o),e(t+1)},o)})},a=Promise.resolve(t);for(o=t;o<e;o++)a=a.then(l);a.then(function(){i(n)},s)})},t.Collection.prototype._getDefaultFetchSize=function(e){return null==e?this[t.Collection._FETCH_SIZE_PROP]:e},t.Collection.prototype._calculateNextStart=function(){var e=this.lastFetchCount;return null==e&&(e=this[t.Collection._FETCH_SIZE_PROP]),void 0===this.offset||null===this.offset?e:this.offset+e},t.Collection.prototype.next=function(e,o){var n=o||{};n[t.Collection._FETCH_SIZE_PROP]=this._getDefaultFetchSize(e);var r=this._calculateNextStart(),i=this._getLength();if(0===i&&n[t.Collection._FETCH_SIZE_PROP]>0)r=0;else if(r>=i){return n.success&&n.success.call(t.Model.GetContext(n,this),this,null,n),null}return n.startIndex=r,this.fetch(n)},t.Collection.prototype._calculatePrevStart=function(t){return void 0===this.offset||null===this.offset?0:this.offset-t},t.Collection.prototype.previous=function(e,o){var n=o||{};if(0===this.offset){return n.success&&this.lastFetchCount&&n.success.call(t.Model.GetContext(n,this),this,null,n),null}n[t.Collection._FETCH_SIZE_PROP]=this._getDefaultFetchSize(e);var r=this._calculatePrevStart(n[t.Collection._FETCH_SIZE_PROP]);return r<0&&(n[t.Collection._FETCH_SIZE_PROP]=this.offset,r=0),n.startIndex=r,this.fetch(n)},t.Collection.prototype.setModelLimit=function(t){this.modelLimit=t,this._manageLRU(0)},t.Collection.prototype._getModelLimit=function(){return this.modelLimit},t.Collection.prototype.setFetchSize=function(e){this[t.Collection._FETCH_SIZE_PROP]=e},t.Collection.prototype.rest=function(t,e){var o,n=this._getDeferred(e),r=void 0===t?1:t,i=[];if(this.IsVirtual()||n){var s=this;return this._addPromise(function(){return s.IterativeAt(r,s._getLength())})}for(o=r;o<this._getLength();o+=1)i.push(this.at(o));return i},t.Collection.prototype.remove=function(e,o){var n,r=o||{},i=[];e instanceof Array?i=e:i.push(e);var s=[];for(n=i.length-1;n>=0;n-=1)s.unshift(this._removeInternal(i[n],-1,r));return this.TriggerInternal(r.silent,t.Events.EventType.ALLREMOVED,this,i,r),t.Collection._returnModels(s)},t.Collection.prototype._removeInternal=function(e,o,n){var r=n||{},i=-1===o?this._getInternal(e):t.Collection._getModinfo(o,e),s=r.silent,l=i.index;if(l>-1){var a=i.m;void 0!==a&&a.GetCollection()===this&&a.SetCollection(null),this._spliceModels(l,1),this._setLength();var c={};t.CollectionUtils.copyInto(c,r),c.index=l,void 0!==a&&a.TriggerInternal(s,t.Events.EventType.REMOVE,a,this,c),this._unlistenToModel(a)}return i.m},t.Collection.prototype._unlistenToModel=function(t){void 0!==t&&t.off(null,null,this)},t.Collection.prototype._listenToModel=function(e){e.OnInternal(t.Events.EventType.ALL,this._modelEvent,this,!1,!0)},t.Collection.prototype._modelEvent=function(e,o,n,r){if(e===t.Events.EventType.DESTROY&&this.remove(o),!(void 0!==n&&n instanceof t.Collection&&n!==this)){var i=r&&r.silent;this.TriggerInternal(i,e,o,n,r)}},t.Collection.prototype.refresh=function(e){var o,n=e||{},r=this;return this._addPromise(function(){return new Promise(function(e,i){if(!r.IsVirtual()){o=void 0!==n.silent&&n.silent;try{r.reset(null,{silent:!0});var s={};return Object.keys(n).forEach(function(t){Object.prototype.hasOwnProperty.call(n,t)&&(s[t]=n[t])}),s.success=function(n,i,s){r.TriggerInternal(o,t.Events.EventType.REFRESH,r,s,null),e({collection:n,response:i,options:s})},s.error=function(e,o,s){i(t.Collection._createRejectionError(e,o,s,r,n,!1))},void r._fetchInternal(s,-1,!1)}catch(i){if(i instanceof t.URLError)return r.TriggerInternal(o,t.Events.EventType.REFRESH,r,n,null),void e({collection:r,options:n});throw i}}var l=n.startIndex;r._setModels([],!0),r._resetLRU(),r.totalResults=void 0,r._setLength(),o=void 0!==n.silent&&n.silent,r.TriggerInternal(o,t.Events.EventType.REFRESH,r,n,null),null==l&&(l=0),null!=l?r._setRangeLocalInternal(l,r._getFetchSize(n)).then(function(t){e(t)},function(t){i(t)}):e(void 0)})})},t.Collection.prototype.reset=function(e,o){var n,r,i={};t.CollectionUtils.copyInto(i,o||{}),i.previousModels=this._getModels();for(var s=0;s<this._modelIndices.length;s++)n=this._modelIndices[s],(r=this._getModel(n))&&(this._unlistenToModel(r),r.SetCollection(null));this._setModels([],!0),this._resetLRU();var l=void 0!==i.silent&&i.silent;if(!e)return this._setLength(),this.totalResults=void 0,this.TriggerInternal(l,t.Events.EventType.RESET,this,i,null),null;var a,c=e;return i.parse&&(c=this.parse(e)),this._manageLRU(c instanceof Array?c.length:1),i.noparse=!0,a=this._addInternal(c,i,!0,!1),this._setLength(),this.TriggerInternal(l,t.Events.EventType.RESET,this,i,null),this._handlePromise(a)},t.Collection.prototype.at=function(t,e){var o=this._getDeferred(e);return this._atInternal(t,e,!1,o)},t.Collection.prototype._atInternal=function(t,e,o,n){var r=t;if(r<0&&(r+=this._getLength()),r<0||this._overUpperLimit(r))return o||!this.IsVirtual()&&!n?null:this._addPromise(function(){return Promise.resolve(null)});var i=this;return o||!this.IsVirtual()&&!n?this._getModel(r):this._addPromise(function(){return i._deferredAt(r,e)})},t.Collection.prototype.whenReady=function(){return this._promises?this._promises:Promise.resolve()},t.Collection.prototype._addPromise=function(e){var o=this;return void 0===this._promises&&(this._promiseCount=0,this._promises=Promise.resolve()),this._promiseCount=this._promiseCount+1,this._promises=this._promises.then(e.bind(o)).then(function(e){return o._promiseCount-=1,0===o._promiseCount&&(o._promises=void 0,o.TriggerInternal(!1,t.Events.EventType.READY,o,null,null)),e},function(t){return o._promiseCount-=1,0===o._promiseCount&&(o._promises=void 0),Promise.reject(t)}),this._promises},t.Collection.prototype._addxhr=function(t){if(t&&t.abort){void 0===this._xhrs&&(this._xhrs=[]);var e=this;this._xhrs.push(t),t.done(function(){var o=e._xhrs?e._xhrs.indexOf(t):-1;o>-1&&e._xhrs.splice(o,1)})}},t.Collection.prototype.abort=function(){var t=this;function e(e,o){t._xhrs[e].then(function(n,r){"abort"===r&&(t._xhrs.splice(e,1),0===t._xhrs.length&&t.whenReady().then(function(){o(null)},function(){o(null)}))},function(){t._xhrs.splice(e,1),0===t._xhrs.length&&t.whenReady().then(function(){o(null)},function(){o(null)})})}return this._xhrs&&this._xhrs.length>0?new Promise(function(o){for(var n=t._xhrs.length-1;n>=0;n--)e(n,o),t._xhrs[n].abort()}):Promise.resolve()},t.Collection.prototype._deferredAt=function(e,o){var n=this,r=n._getModel(e);return void 0===r?new Promise(function(r,i){var s={};t.CollectionUtils.copyInto(s,o||{}),s.context=n,s.startIndex=e,s.error=function(e,r,s){i(t.Collection._createRejectionError(e,r,s,n,o,!1))},s.success=function(){r(n._getModel(e))},n._fetchInternal(s,-1,!1)}):new Promise(function(t){t(r)})},t.Collection.prototype.getByCid=function(t){for(var e,o=this._getModels(),n=null,r=0;r<this._modelIndices.length;r++)if(o[e=this._modelIndices[r]]&&t===o[e].cid){n=o[e];break}if(n)return n;if(this.IsVirtual())throw new Error("Not found locally and not supported by server for virtual collections");return null},t.Collection.prototype.get=function(t,o){var n=this._getDeferred(o),r=this._getInternal(t,o,n);if(r){if(e.isFunction(r.then))return this._addPromise(function(){return new Promise(function(t,e){r.then(function(e){t(e.m)},function(t){e(t)})})});if(this.IsVirtual())return this._addPromise(function(){return new Promise(function(t){t(r.m)})});if(Object.prototype.hasOwnProperty.call(r,"m"))return r.m}return null},t.Collection.prototype._getLocal=function(t){var e=this._getLocalInternal(t);return e?e.m:null},t.Collection.prototype._getLocalInternal=function(e){var o=e,n=e;e instanceof t.Model?(o=e.GetCid(),n=e.GetId()):t.Collection._defined(e)&&void 0!==e.id&&(n=e.id);for(var r,i,s=null,l=this._modelIndices.length,a=this._getModels(),c=0;c<l;c++)if(void 0!==(r=a[i=this._modelIndices[c]])&&r.Match(n,o)){s=t.Collection._getModinfo(i,r);break}return s||t.Collection._getModinfo(-1,void 0)},t.Collection.prototype._getInternal=function(e,o,n,r){var i=e,s=e,l=void 0!==r&&r;e instanceof t.Model?(i=e.GetCid(),s=e.GetId()):t.Collection._defined(e)&&void 0!==e.id&&(s=e.id);for(var a,c=null,u=this._getModels(),h=0;h<this._modelIndices.length;h++)if(u[a=this._modelIndices[h]]&&u[a].Match(s,i)){var p=t.Collection._getModinfo(a,u[a]);c=p;break}if(c)return n?new Promise(function(t){t(c)}):c;if(this.IsVirtual()){if(void 0===s&&void 0!==i)return new Promise(function(e){e(t.Collection._getModinfo(-1,void 0))});var d=this;return new Promise(function(e,n){var r={};t.CollectionUtils.copyInto(r,o||{}),r.context=d,r.startID=s,r.error=function(e,r,i){n(t.Collection._createRejectionError(e,r,i,d,o,!1))},r.success=function(o,n){!function(o){if(null!=o){var n=d._getOffset(),r=d._getModel(n);void 0!==r&&r.Match(s,i)?e(t.Collection._getModinfo(n,r)):e(t.Collection._getModinfo(-1,void 0))}else e(t.Collection._getModinfo(-1,void 0))}(n)},d._fetchInternal(r,-1,l)})}var f=t.Collection._getModinfo(-1,void 0);return n?new Promise(function(t){t(f)}):f},t.Collection._getModinfo=function(t,e){return{index:t,m:e}},t.Collection.prototype._parseImpl=function(t){if(t instanceof Array)return t;if(!t)return t;var e;for(e in t)if(Object.prototype.hasOwnProperty.call(t,e)&&t[e]instanceof Array)return t[e];return t},t.Collection.prototype.parse=t.Collection.prototype._parseImpl,t.Collection.prototype._checkActual=function(t,e,o){return!!(this._hasTotalResults()&&o.start+o.count>=this.totalResults)||o.start===t&&o.count===e},t.Collection.prototype.setRangeLocal=function(t,e,o){var n=this;return this._addPromise(function(){return n._setRangeLocalInternal(t,e,o)})},t.Collection.prototype._setRangeLocalInternal=function(t,e,o){this.IsVirtual()&&this._resetModelsToFullLength(this.totalResults);var n=this._getLocalRange(t,e),r=this;if(this._checkActual(t,e,n))return new Promise(function(t){t(n)});var i=this._getModelLimit();return i>-1&&i<e&&(this.modelLimit=e),new Promise(function(n,i){r._setRangeLocalFetch(t,e,-1,{start:t,count:e},n,i,!0,o)})},t.Collection.prototype._setRangeLocalFetch=function(e,o,n,r,i,s,l,a){var c=this,u=e,h=u+o;this[t.Collection._FETCH_SIZE_PROP]&&this[t.Collection._FETCH_SIZE_PROP]>o&&(h=this[t.Collection._FETCH_SIZE_PROP]+u);var p=null;if(this.IsVirtual()){var d=this._getFirstMissingModel(u,h);d>u&&(h=(u=d)+o,this[t.Collection._FETCH_SIZE_PROP]&&this[t.Collection._FETCH_SIZE_PROP]>o&&(h=this[t.Collection._FETCH_SIZE_PROP]+u)),p={context:this,startIndex:u,fetchSize:h-u}}else p={context:this};p.error=function(e,o,n){s(t.Collection._createRejectionError(e,o,n,c,null,!1))},p.success=function(){!function(){var t=c._getLocalRange(r.start,r.count);if(l&&c._hasTotalResults()&&t.count<r.count){var e=t.start+t.count,n=u+(c.lastFetchCount?c.lastFetchCount:o);n<c.totalResults?c._setRangeLocalFetch(n,o,e,r,i,s,l,a):i(t)}else i(t)}()},a&&a.silent&&(p.silent=a.silent);try{this._fetchInternal(p,n,n>-1)}catch(e){if(e instanceof t.URLError){var f=c._getLocalRange(u,o);i(f)}}},t.Collection._createRejectionError=function(e,o,n,r,i,s){var l=!1;i&&i.silent&&(l=i.silent),s&&t.Model._triggerError(r,l,i,o,n,e);var a=new Error(o);return a.xhr=e,a.error=n,a.collection=r,a.status=o,a},t.Collection.prototype._getMaxLength=function(t,e){var o=this._getModelsLength();return 0===o?t+e:t+e>o?o:t+e},t.Collection.prototype.isRangeLocal=function(t,e){var o=this._getLocalRange(t,e);return 0===this._getModelsLength()?0===e:t===o.start&&(e===o.count||t+e>this._getModelsLength())},t.Collection.prototype._getModelArray=function(t,e){for(var o=[],n=this._getModels(),r=t+e,i=t;i<r;i++)o.push(n[i]);return o},t.Collection.prototype._getLocalRange=function(t,e){if(!this.IsVirtual()){if(this._getModelsLength()>0){if(t+e>this._getModelsLength()){var o=this._getModelsLength()-t;return{start:t,count:o,models:this._getModelArray(t,o)}}return{start:t,count:e,models:this._getModelArray(t,e)}}return{start:t,count:0,models:[]}}var n=this._getMaxLength(t,e);if(!this._hasTotalResults()&&n<t+e)return{start:t,count:n-t,models:this._getModelArray(t,n-t)};if(0===n)return{start:t,count:0,models:[]};var r=this._getFirstMissingModel(t,n);if(r>-1)return{start:t,count:r-t,models:this._getModelArray(t,r-t)};var i=e;return t>n?i=0:t+i>n&&(i=n-t),{start:t,count:i,models:this._getModelArray(t,i)}},t.Collection.prototype._getFirstMissingModel=function(t,e){for(var o=t;o<e;o++)if(void 0===this._getModel(o))return o;return-1},t.Collection.prototype.fetch=function(t){var e=this._fetchInternal(t,-1,!1);return this._addPromise(function(){return Promise.resolve(e)}),e},t.Collection.prototype._fetchInternal=function(e,o,n){function r(t,e,o,n){t.IsVirtual()?o||t._resetModelsToFullLength(n):e.add||e.useset||t.reset(null,{silent:!0})}var i,s=e||{},l=s.success,a=s.error;return s.set&&(s.useset=!!s.set),void 0===s.parse&&(s.parse=!0),i=this,s.error=function(o,n,r){t.Model._triggerError(i,!1,e,n,r,o),a&&a.call(t.Model.GetContext(e,i),o,n,r)},s.success=function(a){var c,u;try{c=i.parse(a,e)}catch(o){return void t.Collection._reportError(i,o,s.error,e)}i._setPagingReturnValues(a,e,c,n)||(u=i.totalResults);var h=null,p=!1,d=o;if(s.add||i.model){r(i,s,n,u);try{-1===o&&(p=!0,d=i._getOffset()),h=i._fillInCollectionWithParsedData(c,d,p,s)}catch(o){return void t.Collection._reportError(i,o,s.error,e)}}else n||(i.IsVirtual()?(r(i,s,n,u),-1===o&&(p=!0,d=i._getOffset()),h=i._putDataIntoCollection(c,d,p)):s.useset?i._setInternal(c,!1,s,!1):i.reset(c,{silent:!0}));i.IsVirtual()&&h&&(i.lastFetchCount=h.length);var f=!!s.silent;i.TriggerInternal(f,t.Events.EventType.SYNC,i,a,s),l&&l.call(t.Model.GetContext(e,i),i,a,s)},this._fetchCall(s)},t.Collection.prototype._putDataIntoCollection=function(t,e,o){var n;if(t){n=t instanceof Array?t:[t];var r={};o&&this._manageLRU(n.length);for(var i=e,s=this.IsVirtual(),l=null,a=0;a<n.length;a+=1)s&&(r={at:i},l=this._atInternal(i,null,!0,!1)),r.silent=!0,this._addInternal(n[a],r,!0,!1),this._atInternal(i,null,!0,!1)!==l&&(i+=1)}return n},t.Collection.prototype._fillInCollectionWithParsedData=function(e,o,n,r){var i=r||{},s=i.parse,l=t.Collection._createModel(this),a=null;if(e){a=e instanceof Array?e:[e];var c,u={};n&&this._manageLRU(a.length);var h,p=this.IsVirtual();if(i.useset&&!p){for(h=0;h<a.length;h+=1)c=l&&s?l.parse(a[h]):a[h],a[h]=c;this._setInternal(a,!1,i,!1)}else{var d=null,f=o;for(h=0;h<a.length;h+=1)c=l&&s?l.parse(a[h]):a[h],p&&(u={at:f},d=this._atInternal(f,u,!0,!1)),u.silent=!0,this._addInternal(c,u,!0,!1),this._atInternal(f,null,!0,!1)!==d&&(f+=1)}}return a},t.Collection._reportError=function(e,o,r,i){n.error(o.toString()),r&&r.call(t.Model.GetContext(i,e),e,o,i)},t.Collection.prototype._fetchOnly=function(e){var o,n,r=e||{},i=r.success;return void 0===r.parse&&(r.parse=!0),n=this,r.success=function(s){var l,a,c,u=null,h=[];try{c=n.parse(s,e)}catch(o){return void t.Collection._reportError(n,o,r.error,e)}if(r.add||n.model){if(a=t.Collection._createModel(n),c)for(u=c instanceof Array?c:[c],l=0;l<u.length;l+=1){if(a&&r.parse)try{o=a.parse(u[l])}catch(o){return void t.Collection._reportError(n,o,r.error,e)}else o=u[l];h.push(n._newModel(o))}}else u=c instanceof Array?c:[c];n.TriggerInternal(!1,t.Events.EventType.SYNC,n,s,r),i&&i.call(t.Model.GetContext(e,n),n,h,r)},this._fetchCall(r)},t.Collection.prototype._fetchCall=function(e){try{return t.Model._internalSync("read",this,e)}catch(o){throw t.Model._triggerError(this,!1,e,null,o,null),o}},t.Collection.prototype._resetModelsToFullLength=function(t){return void 0!==t&&this._getModelsLength()!==t&&(this._setModels(new Array(t),!0),this._resetLRU(),this._setLength(),!0)},t.Collection.prototype._getFetchSize=function(e){return(e||{})[t.Collection._FETCH_SIZE_PROP]||this[t.Collection._FETCH_SIZE_PROP]},t.Collection.prototype.IsVirtual=function(){return this._getFetchSize(null)>-1},t.Collection.prototype._getReturnProperty=function(e,o,n,r,i){var s=parseInt(t.Collection._getProp(e,o,n),10);return null==s||isNaN(s)?r||i:s},t.Collection.prototype._cleanTotalResults=function(t){if(-1!==t)return t},t.Collection.prototype._setPagingReturnValues=function(e,o,n,r){var i={};this.customPagingOptions&&((i=this.customPagingOptions.call(this,e))||(i={}));var s=o||{};this.lastFetchSize=this._getReturnProperty(i,e,"limit",s.fetchSize,this.fetchSize),this.offset=this._getReturnProperty(i,e,"offset",s.startIndex,0),this.lastFetchCount=this._getReturnProperty(i,e,"count",this.lastFetchCount,this.lastFetchCount),this.totalResults=this._cleanTotalResults(this._getReturnProperty(i,e,"totalResults",this.totalResults,this.totalResults)),this.hasMore=this._getHasMore(t.Collection._getProp(i,e,"hasMore"),this.offset,this.lastFetchSize,this.totalResults);var l=!1;if(!r){var a=this._cleanTotalResults(parseInt(t.Collection._getProp(i,e,"totalResults"),10)),c=parseInt(t.Collection._getProp(i,e,"count"),10);this.totalResults=this._adjustTotalResults(a,this.hasMore,this.offset,c,n&&Array.isArray(n)?n.length:0),l=void 0===a||isNaN(a)||null===a}return!this.IsVirtual()&&this.totalResults&&this.totalResults!==this.lastFetchCount&&this.lastFetchSize&&this.setFetchSize(this.lastFetchSize),l},t.Collection.prototype._adjustTotalResults=function(t,e,o,n,r){if(!e&&isNaN(t))return(isNaN(n)?r:n)+o;return this.totalResults},t.Collection.prototype._getHasMore=function(e,o,n,r){return t.Collection._defined(e)?e:null==r||!(o+n>r)},t.Collection._getProp=function(t,e,o){return Object.prototype.hasOwnProperty.call(t,o)?t[o]:e?e[o]:void 0},t.Collection.prototype._getOffset=function(){return t.Collection._defined(this.offset)?this.offset:0},t.Collection.prototype.create=function(e,o){var n=this,r=o||{},i=this._getDeferred(r);function s(o,n,r,i){return n.save(e instanceof t.Model?null:e,i),n}function l(t,e){return r.wait?n.IsVirtual()||i?n._addPromise(function(){return Promise.resolve(void 0)}):null:n.add(t,e)}var a=this._newModel(e,!0,r,!1),c=r.success,u=r.context;r.validate;if(r.context=this,r.success=function(t,e,o){o.xhr&&(r.xhr=o.xhr),r.wait&&n.add(a,r),c&&c.call(null!=u?u:n,t,e,r)},null==a)return!1;r.forceNew=null!=a.GetId();var h=t.Model._copyOptions(r);return a.SetCollection(this),i||this.IsVirtual()?new Promise(function(t){h.merge=!0,h.deferred=!0,l(a,h).then(function(){r.success=function(e,o,i){i.xhr&&(r.xhr=i.xhr),r.wait?(n.IsVirtual()&&(h.force=!0),n.add(a,h).then(function(){c&&c.call(null!=u?u:n,e,o,r),t(e)})):(c&&c.call(null!=u?u:n,e,o,r),t(e))};var e=s(0,a,0,r);e||t(e)})}):(h.merge=!0,l(a,h),s(0,a,0,r))},t.Collection.prototype.pluck=function(t){var e,o=[];for(this._throwErrIfVirtual("pluck"),e=0;e<this._getLength();e+=1)o.push(this.at(e).get(t));return o},t.Collection.prototype.where=function(t,e){return this._handlePromise(this._whereInternal(t,e))},t.Collection.prototype._whereInternal=function(e,o){var n=o||{},r=this._getDeferred(n),i=this;if(this.IsVirtual())return new Promise(function(o,r){var s={query:e,all:!0,success:function(t,e){o(e)},error:function(e,o,s){r(t.Collection._createRejectionError(e,o,s,i,n,!0))}};i._fetchOnly(s)});var s,l,a=[];for(s=0;s<this._getLength();s+=1)(l=this.at(s)).Contains(e)&&a.push(l);return r?new Promise(function(t){t(a)}):a},t.Collection.prototype.whereToCollection=function(e,o){var n=o||{},r=this._getDeferred(n),i=this;if(this.IsVirtual()||r)return i._addPromise(function(){return new Promise(function(t,o){return i._whereInternal(e,n).then(function(e){var o=i._makeNewCollection(e);t(o)},function(t){o(t)})})});var s=this._whereInternal(e,n),l=this._makeNewCollection(s);return l[t.Collection._FETCH_SIZE_PROP]=-1,l._setLength(),l},t.Collection.prototype._makeNewCollection=function(t){var e=this._cloneInternal(!1);return e._setModels(t,!1),e._resetLRU(),e._setLength(),e},t.Collection.prototype._throwErrIfVirtual=function(t){if(this.IsVirtual())throw new Error(t+" not valid on a virtual Collection")},t.Collection.prototype.map=function(t,e){var o,n=[];return this._throwErrIfVirtual("map"),this._getModels().forEach(function(r){o=t.call(e||this,r),n.push(o)}),n},t.Collection.prototype.each=function(t,e){this._throwErrIfVirtual("each"),this._getModels().forEach(t,e)},t.Collection.prototype.size=function(){return this._getLength()},t.Collection.prototype.sortBy=function(o,n){var r,i=[];return this._throwErrIfVirtual("sortBy"),this._getModels().forEach(function(t){i.push(t)}),r=this,i.sort(function(i,s){var l,a;return e.isFunction(o)?(l=o.call(n||r,i),a=o.call(n||r,s),t.Collection._compareKeys(l,a,r.sortDirection)):(l=i.get(o),a=s.get(o),t.Collection._compareKeys(l,a,r.sortDirection))}),i},t.Collection.prototype.groupBy=function(t,o){var n,r={};return this._throwErrIfVirtual("groupBy"),this._getModels().forEach(function(i){n=e.isFunction(t)?t.call(o||this,i):i.get(t),void 0===r[n]&&(r[n]=[]),r[n].push(i)},this),r},t.Collection.prototype.indexBy=function(t,o){var n,r={};return this._throwErrIfVirtual("indexBy"),this._getModels().forEach(function(i){n=e.isFunction(t)?t.call(o||this,i):i.get(t),r[n]=i},this),r},t.Collection.prototype.min=function(t,e){var o,n,r={};return this._throwErrIfVirtual("min"),0===this._getModelsLength()?null:(r=this._getModel(0),o=t.call(e||this,this._getModel(0)),this._getModels().forEach(function(i,s){s>=1&&(n=t.call(e||this,i))<o&&(r=i,o=n)},this),r)},t.Collection.prototype.max=function(t,e){var o,n,r={};return this._throwErrIfVirtual("max"),0===this._getModelsLength()?null:(r=this._getModel(0),o=t.call(e,this._getModel(0)),this._getModels().forEach(function(i,s){s>=1&&(n=t.call(e||this,i))>o&&(r=i,o=n)},this),r)},t.Collection.prototype.filter=function(t,e){var o=[];return this._throwErrIfVirtual("filter"),this._getModels().forEach(function(n){t.call(e||this,n)&&o.push(n)}),o},t.Collection.prototype.without=function(t){var e,o,n,r,i,s=[];this._throwErrIfVirtual("without");for(var l=0;l<this._getModels().length;l++){for(r=!0,i=this._getModel(l),e=0;e<arguments.length;e+=1)if(n=arguments[e].GetCid(),o=arguments[e].GetId(),i.Match(o,n)){r=!1;break}r&&s.push(i)}return s},t.Collection.prototype.difference=function(t){var e,o,n,r,i,s,l=[];this._throwErrIfVirtual("difference");for(var a=0;a<this._getModels().length;a++){for(i=!0,s=this._getModel(a),e=0;e<arguments.length;e+=1){for(o=0;o<arguments[e].length;o++)if(r=arguments[e][o].GetCid(),n=arguments[e][o].GetId(),s.Match(n,r)){i=!1;break}if(!i)break}i&&l.push(s)}return l},t.Collection.prototype.isEmpty=function(){return 0===this._getLength()},t.Collection.prototype.any=function(t,e){var o;this._throwErrIfVirtual("any");for(var n=0;n<this._getModelsLength();n+=1)if(o=this._getModel(n),t.call(e||this,o))return!0;return!1},t.Collection.prototype.findWhere=function(t,e){var o=this._getDeferred(e),n=this;if(this.IsVirtual()||o)return this._addPromise(function(){return new Promise(function(o){n._whereInternal(t,e).then(function(t){t&&t.length>0&&o(t[0]),o(null)})})});var r=this._whereInternal(t,e);return r.length>0?r[0]:null},t.Collection.prototype.slice=function(t,e,o){var n,r=this._getDeferred(o),i=[],s=e;if(void 0===s){if(this.IsVirtual()&&!this._hasTotalResults())throw new Error("End must be set for virtual collections with no totalResults");s=this._getModelsLength()}if(r||this.IsVirtual()){var l=this;return this._addPromise(function(){return l.IterativeAt(t,s)})}for(n=t;n<s;n+=1)i.push(this._getModel(n));return i},t.Collection.prototype.set=function(t,e){var o=this._getDeferred(e);return this._setInternal(t,!0,e,o||this.IsVirtual())},t.Collection._removeAfterSet=function(t,e,o,n,r){if(o)for(var i=e.length-1;i>=0;i-=1)-1===n.indexOf(i)&&t._removeInternal(e[i],i,r)},t.Collection.prototype._swapModels=function(t,e,o,n){if(this._hasComparator()||!o||!n)return{index:t,swapped:!1};var r=this._getModelsLength();if(t>=r||e>=r)return{index:t,swapped:!1};var i=this._getModel(t),s=this._getModel(e);return this._setModel(t,s),s.SetIndex(t),this._setModel(e,i),i.SetIndex(e),{index:e,swapped:e!==t}},t.Collection.prototype._setInternal=function(e,o,n,r){var i,s,l=n||{},a=void 0===l.add||l.add,c=void 0===l.remove||l.remove,u=void 0===l.merge||l.merge,h=[],p=null,d=o?this.parse(e):e;if(s=Array.isArray(d)?d:[d],r){var f=this;return this._addPromise(function(){return f._deferredSet(s,f._getModels(),l,c,a,u,o)})}var _=!1;for(i=0;i<s.length;i+=1)if(-1!==(p=this._updateModel(this._newModel(s[i],o,l,!0),a,u,r))){var g=this._swapModels(p,i,c,a),v=g.index;g.swapped&&(_=!0),-1===h.indexOf(v)&&h.push(v)}if(_){var y=l.add?{add:!0}:null;this.TriggerInternal(l.silent,t.Events.EventType.SORT,this,y,null)}return t.Collection._removeAfterSet(this,this._getModels(),c,h,l),null},t.Collection.prototype._deferredSet=function(e,o,n,r,i,s,l){var a,c=[],u=this;return new Promise(function(h,p){var d=function(t){return new Promise(function(o,r){u._updateModel(u._newModel(e[t],l,n,!0),i,s,!0).then(function(e){-1!==e&&c.push(e),o(t+1)},r)})},f=Promise.resolve(0);for(a=0;a<e.length;a+=1)f=f.then(d);f.then(function(){t.Collection._removeAfterSet(u,o,r,c,n),h(void 0)},p)})},t.Collection.prototype._updateModel=function(t,e,o,n){function r(n,r,i){var s=r?r.index:-1,l=r?r.m:null,a={};if(l){if(o){if(a={merge:o},i)return new Promise(function(e){n._addInternal(t,a,!1,!0).then(function(){e(s)})});n.add(t,a)}}else if(e){if(i)return new Promise(function(e){n._addInternal(t,a,!1,!0).then(function(){e(n._getLength()-1)})});n.add(t),s=n._getLength()-1}return s}if(n||this.IsVirtual()){var i=this;return new Promise(function(e){i._getInternal(t,{silent:!0},n).then(function(t){r(i,t,!0).then(function(t){e(t)})})})}return r(this,this._getInternal(t),!1)},t.Collection.prototype.toJSON=function(){var t=[];return this._throwErrIfVirtual("toJSON"),this._getModels().forEach(function(e){t.push(e.toJSON())}),t},t.Collection.prototype.first=function(t,e){var o,n=this._getDeferred(e),r=this._getLength(),i=[],s=this,l=t;l?r=l:l=1;var a=this.IsVirtual()||n;if(1===l)return a?this._addPromise(function(){return s._deferredAt(0,null)}):this._getModelsLength()>0?this._getModel(0):null;if(r>this._getModelsLength()&&(this.IsVirtual()&&!this._hasTotalResults()||(r=this._getModelsLength())),a)return this._addPromise(function(){return s.IterativeAt(0,r)});for(o=0;o<r;o+=1)i.push(this._getModel(o));return i},t.Collection.prototype.indexOf=function(t,e){var o=this._getDeferred(e);if(this.IsVirtual()||o){var n=this;return this._addPromise(function(){return n._getInternal(t,null,!0).then(function(t){return t.index})})}return this._getInternal(t).index},t.Collection.prototype.contains=function(t,e){var o=this._getDeferred(e);if(this.IsVirtual()||o){var n=this;return this._addPromise(function(){return n._getInternal(t,null,!0).then(function(t){return t.index>-1})})}return this._getInternal(t).index>-1},t.Collection.prototype.include=t.Collection.prototype.contains,t.Collection.prototype._localIndexOf=function(t){var e=this._getLocalInternal(t);return void 0!==e?e.index:-1},t.Collection.prototype.pop=function(t){var e=this._getDeferred(t);if(this.IsVirtual()||e){var o=this;return this._atInternal(this._getLength()-1,t,!1,!0).then(function(e){return o.remove(e,t),e})}var n=this.at(this._getLength()-1);return this.remove(n,t),n},t.Collection.prototype.push=function(t,e){var o=this._getDeferred(e);return this._manageLRU(1),this._handlePromise(this._addInternal(t,e,!1,o))},t.Collection.prototype.lastIndexOf=function(e,o){var n,r=o;for(this._throwErrIfVirtual("lastIndexOf"),void 0===r&&(r=0),n=this._getLength()-1;n>=r;n-=1)if(t.Object.__innerEquals(e,this.at(n)))return n;return-1},t.Collection.prototype._getSortAttrs=function(t){return void 0===t?[]:t.split(",")},t.Collection._getQueryString=function(o){function n(t,e,o){return t+o+e}function r(o,r){var i,s=r;return Object.keys(o||{}).forEach(function(r){if(Object.prototype.hasOwnProperty.call(o,r))for(var l=Array.isArray(o[r])?o[r]:[o[r]],a=0;a<l.length;a++){if(t.Model.IsComplexValue(l[a])){var c=l[a].value,u=null,h=l[a].comparator;u=e.isFunction(h)?h(null,r,c):h,i=n(r,c,u)}else i=n(r,o[r],"=");s+=i+"+"}}),s=s.substring(0,s.length-1)+","}var i,s=Array.isArray(o)?o:[o],l="";for(i=0;i<s.length;i++)l=r(s[i],l);return","===l.substring(l.length-1)?l.substring(0,l.length-1):l},t.Collection.prototype.ModifyOptionsForCustomURL=function(e){var o={};Object.keys(e||{}).forEach(function(t){Object.prototype.hasOwnProperty.call(e,t)&&(o[t]=e[t])});var n=this.comparator;if(n&&t.StringUtils.isString(n)){for(var r=this._getSortAttrs(n),i=0;i<r.length;i++)0===i?o.sort=r[i]:o.sort+=","+r[i];o.sortDir=this._getSortDirStr()}return this.IsVirtual()&&(o[t.Collection._FETCH_SIZE_PROP]=this._getFetchSize(o)),o},t.Collection.prototype.IsUrlBased=function(o){var n=this.customURL;if(e.isFunction(n))return!0;var r=this.GetCollectionFetchUrl(o);return t.Collection._defined(r)},t.Collection.prototype.GetCollectionFetchUrl=function(e){var o=t.Model.GetPropValue(this,"url");if(this.IsVirtual()){var n=e||{},r=n.all,i=null;if(r)i=this.totalResults||this._getFetchSize(n);else i=this._getFetchSize(n);if(o&&o.indexOf("?")>-1?o+="&":o+="?",o+="limit="+i,r||(t.Collection._defined(n.startIndex)&&(o+="&offset="+n.startIndex),n.startID&&(o+="&fromID="+n.startID),n.since&&(o+="&since="+n.since),n.until&&(o+="&until="+n.until)),n.query){var s=t.Collection._getQueryString(n.query);s&&s.length>0&&(o+="&q="+s)}var l=this.comparator;if(l&&t.StringUtils.isString(l)){var a,c=this._getSortAttrs(l),u=this._getSortDirStr();for(a=0;a<c.length;a++)o+=0===a?"&orderBy="+c[a]+":"+u:","+c[a]+":"+u}o+="&totalResults=true"}return o},t.Collection.prototype._getSortDirStr=function(){return-1===this.sortDirection?"desc":"asc"},t.Collection.prototype.sync=function(e,o,n){return t.sync(e,o,n)},t.Collection._FETCH_SIZE_PROP="fetchSize",t.Model=function(e,o){t.Model._init(this,e,o,null)},t.Object.createSubclass(t.Model,t.Object,"oj.Model"),t.Model.prototype.Init=function(){t.Model.superclass.Init.call(this)},t.Model.prototype.attributes={},t.Model.prototype.defaults={},t.Model.prototype.id=null,t.Model.prototype.idAttribute="id",t.Model.prototype.urlRoot=null,t.Model.prototype.customURL=null,t.Model.prototype.validate=null,t.Model.prototype.validationError=null,t.Model.prototype.omitLanguageHeader=!1,t.Model._idCount=0,t.Model._init=function(o,n,r,i){var s,l,a;if(!t.Model._justExtending){o.Init(),t.Events.Mixin(o),o._clearChanged(),o.previousAttrs={},o.nestedSet=!1,o.index=-1;var c=r||{};for(a in o.attributes={},o.defaults&&!c.ignoreDefaults&&(o.attributes=t.Model._cloneAttributes(e.isFunction(o.defaults)?o.defaults():o.defaults,null)),i)Object.prototype.hasOwnProperty.call(i,a)&&(o[a]=i[a]);if(n)if(s=c.parse,e.isFunction(s)&&(o.parse=s),l=t.Model._cloneAttributes(n,o.attributes),o.attributes=l,null==(l=s?o.parse(l):l)||void 0===l)o.attributes={};else for(a in l)Object.prototype.hasOwnProperty.call(l,a)&&o._setProp(a,l[a],!1,!1,c,!0);o.SetCid(),o.SetCollection(c.collection),c.customURL&&(o.customURL=c.customURL),c.url&&(o.url=c.url),c.urlRoot&&(o.urlRoot=c.urlRoot),o.initialize&&o.initialize(n,c),o.SetupId()}},t.Model.extend=function(o,n){var r;t.Model._justExtending=!0,r=new t.Model,t.Model._justExtending=!1,e.extend(r,this.prototype);var i,s=o||{};return Object.keys(s).forEach(function(t){Object.prototype.hasOwnProperty.call(s,t)&&(r[t]=s[t])}),i=s&&s.constructor&&Object.prototype.hasOwnProperty.call(s,"constructor")?s.constructor:function(e,o){t.Model._init(this,e,o,s)},e.extend(i,this),i.prototype=r,i.extend=t.Model.extend,i.prototype.constructor=i,t.Events.Mixin(i,this),n&&Object.keys(n).forEach(function(t){Object.prototype.hasOwnProperty.call(n,t)&&(i[t]=n[t])}),i},t.Model.prototype.TriggerInternal=function(t,e,o,n,r){},t.Model.prototype.SetCid=function(){this.GetCid()||(this.cid="id"+t.Model._idCount,t.Model._idCount+=1)},t.Model.prototype.GetCid=function(){return this.cid},t.Model.prototype.SetIndex=function(t){this.index=t},t.Model.prototype.GetIndex=function(){return this.index},t.Model.prototype.SetNext=function(t){var e=this.nextModel;return this.nextModel=t,e},t.Model.prototype.GetNext=function(){return this.nextModel},t.Model.prototype.SetPrevious=function(t){var e=this.previousModel;return this.previousModel=t,e},t.Model.prototype.GetPrevious=function(){return this.previousModel},t.Model.prototype.Merge=function(e,o,n){var r,i=!1,s=t.StringUtils.isString(o),l=!1,a=this;return Object.keys(e.attributes||{}).forEach(function(t){Object.prototype.hasOwnProperty.call(e.attributes,t)&&(r=a.attributes[t]!==e.attributes[t],s?t===o&&r&&(i=!0):r&&(i=!0),r&&(l=!0,a.attributes[t]=e.attributes[t],a._addChange(t,e.attributes[t]),a._fireAttrChange(t,a.attributes[t],null,n)))}),this.SetupId(),l&&this._fireChange(null,n),i},t.Model._hasProperties=function(t){var e;if(t&&t instanceof Object)for(e in t)if(Object.prototype.hasOwnProperty.call(t,e))return!0;return!1},t.Model.prototype.SetCollection=function(t){null!=t?(this.collection=t,this.SetupId()):delete this.collection},t.Model.prototype.GetCollection=function(){return this.collection},t.Model.prototype._fireAttrChange=function(e,o,n,r){null!=e&&this.TriggerInternal(r,t.Events.EventType.CHANGE+":"+e,this,o,n)},t.Model.prototype._fireChange=function(e,o){this.TriggerInternal(o,t.Events.EventType.CHANGE,this,e,null)},t.Model.prototype.SetupId=function(){var t=null;if(this.collection&&this.collection.modelId){var o=this.collection.modelId;t=e.isFunction(o)?o.call(this.collection,this.attributes):o}if(!t){var n=this._getIdAttr();t=null!=this.attributes?this.attributes[n]:null}this.id=t},t.Model.prototype._setPropInternal=function(e,o,n){var r=t.Object.__innerEquals(this.attributes[e],o);return!(!n&&r)&&(this.attributes[e]=o,this.SetupId(),!r)},t.Model.prototype._clearChanged=function(){this.changed={}},t.Model.prototype._addChange=function(t,e){this.changed[t]=e},t.Model.prototype._setProp=function(e,o,n,r,i,s){if(null==e)return!0;var l,a={},c=this.nestedSet;if(r?Object.keys(e).forEach(function(t){Object.prototype.hasOwnProperty.call(e,t)&&(a[t]=e[t])}):a[e]=o,l=i||{},!this._checkValid(a,{validate:l.validate},!1))return!1;c||(this._clearChanged(),this.changes=[]),this.nestedSet||s||(this.previousAttrs=t.Model._cloneAttributes(this.attributes,null)),this.nestedSet=!0;var u=this;Object.keys(a).forEach(function(t){Object.prototype.hasOwnProperty.call(a,t)&&(u._setPropInternal(t,a[t],n)?(u._addChange(t,a[t]),u.changes.push(t)):delete a[t])});var h=l.silent;if(Object.keys(a).forEach(function(t){Object.prototype.hasOwnProperty.call(a,t)&&(!h&&(u.changes.length>0||c&&-1===u.changes.indexOf(t))&&(u.pendingChanges=!0,u.pendingOpts=l),u._fireAttrChange(t,a[t],l,h))}),c)return!0;if(!h&&!c)for(;this.pendingChanges;)this.pendingChanges=!1,this._fireChange(this.pendingOpts,h);return this.nestedSet=!1,!0},t.Model.prototype.clear=function(e){var o,n,r={silent:!0},i=e||{};o=i.silent,r.validate=i.validate,this._clearChanged();for(n in this.attributes)if(Object.prototype.hasOwnProperty.call(this.attributes,n)){if(!this._unsetInternal(n,r,!0))return!1;this.TriggerInternal(o,t.Events.EventType.CHANGE+":"+n,this,void 0,null)}return this.attributes={},this.SetupId(),this._fireAttrChange(null,null,null,o),this._fireChange(null,o),this},t.Model._cloneAttributes=function(o,n){var i,s,l=n||{},a=!0;if(("object"===r(l)?Object.keys(l):[]).length>0){for(i in l)Object.prototype.hasOwnProperty.call(l,i)&&Object.prototype.hasOwnProperty.call(o,i)&&void 0===o[i]&&delete o[i];return t.CollectionUtils.copyInto(l,o,void 0,!0,1e4),l}for(i in o)if("function"===(s=e.type(o[i]))||"undefined"===s||"date"===s||"array"===s||"object"===s){a=!1;break}return a?l=JSON.parse(JSON.stringify(o)):"object"===r(o)&&t.CollectionUtils.copyInto(l,o,void 0,!0,1e4),l},t.Model.prototype.clone=function(){var e,o=new this.constructor;for(e in this)Object.prototype.hasOwnProperty.call(this,e)&&this[e]!==this.attributes&&(o[e]=this[e]);return o.attributes=t.Model._cloneAttributes(this.attributes,null),delete o.cid,o.SetCid(),o.SetupId(),o},t.Model.prototype.Match=function(t,e){var o,n=this.GetId();return void 0!==n&&n==t||void 0!==(o=this.cid)&&o==e},t.Model.prototype.set=function(e,o,n){var r,i=n||{},s=!0;if(arguments&&arguments.length>0)if(t.StringUtils.isString(e))i.unset?this._unsetInternal(e,null,!1):this._setProp(e,o,!1,!1,i,!1)||(s=!1);else if((i=o||{}).unset)for(r in e)Object.prototype.hasOwnProperty.call(e,r)&&this._unsetInternal(r,null,!1);else this._setProp(e,null,!0,!0,i,!1)||(s=!1);return!!s&&this},t.Model.prototype.unset=function(t,e){return this._unsetInternal(t,e,!1)},t.Model.prototype._unsetInternal=function(t,e,o){var n=e||{},r=n.silent;if(this.has(t)){if(!this._checkValid({},n,!1))return!1;o||this._clearChanged(),delete this.attributes[t],this._addChange(t,void 0),this._fireAttrChange(t,null,null,r),this._fireChange(null,r)}return this.SetupId(),!0},t.Model.prototype.get=function(t){return this.attributes[t]},t.Model.prototype.has=function(e){return t.Collection._defined(this.attributes[e])},t.Model.prototype.fetch=function(o){var n,r=o||{},i=r.success,s=r.error,l=this;return(n=t.Model._copyOptions(r)).error=function(e,o,n){t.Model._triggerError(l,!1,r,o,n,e),s&&s.apply(l,arguments)},n.success=function(o){n.xhr&&(r.xhr=n.xhr),t.Model._fireSyncEvent(l,o,n,!1),e.isFunction(l.parse)&&l.set(l.parse(o),n),i&&i.call(t.Model.GetContext(n,l),l,o,r)},t.Model._internalSync("read",this,n)},t.Model.prototype._parseImpl=function(t){return t},t.Model.prototype.parse=t.Model.prototype._parseImpl,t.Model.prototype.url=function(){var e,o,n=this._getUrlRoot(),r=this.GetId();if(n)return r?n+"/"+encodeURIComponent(r):n;if(e=this.collection)return o=t.Model.GetPropValue(e,"url"),r&&o?o+("/"===t.Model._getLastChar(o)?"":"/")+encodeURIComponent(this.GetId()):o;throw new t.URLError},t.Model.prototype.keys=function(){var t=[],e=this;return Object.keys(e.attributes||{}).forEach(function(o){Object.prototype.hasOwnProperty.call(e.attributes,o)&&t.push(o)}),t},t.Model.prototype.values=function(){var t=[],e=this;return Object.keys(e.attributes||{}).forEach(function(o){Object.prototype.hasOwnProperty.call(e.attributes,o)&&t.push(e.get(o))}),t},t.Model.prototype.pairs=function(){var t,e=[],o=this;return Object.keys(o.attributes||{}).forEach(function(n){Object.prototype.hasOwnProperty.call(o.attributes,n)&&((t=[]).push(n),t.push(o.get(n)),e.push(t))}),e},t.Model.prototype.omit=function(t){var e,o=[],n={};if(t instanceof Array)o=t;else for(e=0;e<arguments.length;e++)o.push(arguments[e]);var r=this;return Object.keys(r.attributes||{}).forEach(function(t){Object.prototype.hasOwnProperty.call(r.attributes,t)&&-1===o.indexOf(t)&&(n[t]=r.get(t))}),n},t.Model.prototype.pick=function(t){var e,o=[],n={};if(t instanceof Array)o=t;else for(e=0;e<arguments.length;e++)o.push(arguments[e]);for(e=0;e<o.length;e++)Object.prototype.hasOwnProperty.call(this.attributes,o[e])&&(n[o[e]]=this.get(o[e]));return n},t.Model.prototype.invert=function(){var t,e={},o=this;return Object.keys(o.attributes||{}).forEach(function(n){Object.prototype.hasOwnProperty.call(o.attributes,n)&&(t=o.get(n),e[t]=n)}),e},t.Model._getLastChar=function(t){return t.charAt(t.length-1)},t.Model.prototype._saveUrl=function(){var t=this._getUrlRoot();return t||(this.GetCollection()?this.GetCollection().url:null)},t.Model.prototype._getUrlRoot=function(){return t.Model.GetPropValue(this,"urlRoot")},t.Model.prototype._parseSaveImpl=function(t){return t},t.Model.prototype.parseSave=t.Model.prototype._parseSaveImpl,t.Model.prototype.isValid=function(){var t={};return t.validate=this.validate,this._checkValid(this.attributes,t,!1)},t.Model._isValidateSet=function(t,e){var o=t||{};return void 0!==o.validate&&null!==o.validate?o.validate:e},t.Model.prototype._checkValid=function(e,o,n){var r=o||{},i=this.validate;return!(i&&t.Model._isValidateSet(r,n)&&(this.validationError=i.call(this,e,r),this.validationError))||(this.TriggerInternal(!1,t.Events.EventType.INVALID,this,this.validationError,r),!1)},t.Model._processArgs=function(e){var o,n=!1,r={},i={};if(e&&e.length>0){if(e.length>1&&e[e.length-1]&&t.Model._hasProperties(e[e.length-1])&&(n=!0,r=e[e.length-1]||{}),null==e[0])return{attributes:null,options:r};if(t.Model._hasProperties(e[0])||t.Object.isEmpty(e[0]))Object.keys(e[0]).forEach(function(t){Object.prototype.hasOwnProperty.call(e[0],t)&&(i[t]=e[0][t])});else for(o=0;o<e.length;o+=2)(void 0!==e[o]||o<e.length-1||!n&&o===e.length-1)&&(i[e[o]]=e[o+1])}return{attributes:i,options:r}},t.Model._copyOptions=function(t){var e={},o=t||{};return Object.keys(o).forEach(function(t){Object.prototype.hasOwnProperty.call(o,t)&&(e[t]=o[t])}),e},t.Model._triggerError=function(e,o,n,r,i,s){var l=n||{};l.textStatus=r,l.errorThrown=i,e.TriggerInternal(o,t.Events.EventType.ERROR,e,s,l)},t.Model.prototype.save=function(o,n){var r,i,s,l,a,c,u,h,p,d=t.Model._processArgs(arguments);p=void 0===o?void 0:d.attributes;var f=n||{};u=t.Model._copyOptions(d.options);var _=e.extend(!0,{},this.attributes,p);return!!this._checkValid(_,u,!0)&&(u.wait||this.set(p),r=void 0!==u.forceNew&&u.forceNew,l=this,a=u.error,c=u.patch,u.error=function(e,o,n){t.Model._triggerError(l,!1,f,o,n,e),a&&a.apply(l,arguments)},u.saveAttrs=u.wait?this._attrUnion(p):this.attributes,h=this.attributes,this.attributes=u.saveAttrs,u.saveAttrs=this.toJSON(),this.attributes=h,r||this.isNew()?(s=t.Model._getSuccess(u),u.success=function(o){var n;if(u.xhr&&(f.xhr=u.xhr),o&&!t.Object.isEmpty(o)){if(n=e.isFunction(l.parse)?l.parse(o):o,!l._checkValid(n,u,!0))return;l.attributes=e.extend(!0,l.attributes,n),u.wait&&Object.keys(p||{}).forEach(function(t){Object.prototype.hasOwnProperty.call(n,t)&&(p[t]=n[t])}),l.SetupId()}t.Model._fireSyncEvent(l,o,u,!1),u.wait&&l.set(p),s&&s.call(t.Model.GetContext(u,l),l,o,f),l._clearChanged()},u.attrs||(u.attrs=u.saveAttrs),u.parse=!0,c&&(u.saveAttrs=u.attrs),t.Model._internalSync("create",this,u)):(i=u.success,u.success=function(o){var n;u.xhr&&(f.xhr=u.xhr),o&&!t.Object.isEmpty(o)&&(n=e.isFunction(l.parse)?l.parse(o):o,l.attributes=e.extend(!0,l.attributes,n),u.wait&&Object.keys(p||{}).forEach(function(t){Object.prototype.hasOwnProperty.call(n,t)&&(p[t]=n[t])}),l.SetupId()),t.Model._fireSyncEvent(l,o,u,!1),u.wait&&l.set(p),i&&i.call(t.Model.GetContext(u,l),l,o,f),l._clearChanged()},u.attrs||(u.attrs=void 0===p?void 0:c?p:u.saveAttrs),t.Model._internalSync(c?"patch":"update",this,u)))},t.Model.prototype._attrUnion=function(t){var e={},o=this;return Object.keys(o.attributes||{}).forEach(function(t){Object.prototype.hasOwnProperty.call(o.attributes,t)&&(e[t]=o.attributes[t])}),Object.keys(t||{}).forEach(function(o){Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}),e},t.Model.GetPropValue=function(t,o){return t?e.isFunction(t[o])?t[o]():t[o]:e.isFunction(o)?o():o},t.Model.IsComplexValue=function(t){return t&&Object.prototype.hasOwnProperty.call(t,"value")&&Object.prototype.hasOwnProperty.call(t,"comparator")},t.Model.prototype._hasAttrs=function(e){var o;for(o in e)if(Object.prototype.hasOwnProperty.call(e,o)){if(!Object.prototype.hasOwnProperty.call(this.attributes,o))return!1;for(var n=Array.isArray(e[o])?e[o]:[e[o]],r=0;r<n.length;r++)if(t.Model.IsComplexValue(n[r])){var i=n[r].comparator,s=n[r].value;if(t.StringUtils.isString(i))throw new Error("String comparator invalid for local where/findWhere");if(!i(this,o,s))return!1}else if(e[o]!==this.attributes[o])return!1}return!0},t.Model.prototype.matches=function(t){return function(e){for(var o in t)if(e.get(o)!==t[o])return!1;return!0}(this)},t.Model.prototype.Contains=function(t){var e,o=t.constructor===Array?t:[t];for(e=0;e<o.length;e++)if(this._hasAttrs(o[e]))return!0;return!1},t.Model._getSuccess=function(t){return null!=t&&t.success?t.success:null},t.Model.GetContext=function(t,e){return void 0!==t&&void 0!==t.context?t.context:e},t.Model.prototype.isNew=function(){return void 0===this.GetId()},t.Model.prototype._getIdAttr=function(){return this.idAttribute||"id"},t.Model.prototype.GetId=function(){return this.id},t.Model.prototype.changedAttributes=function(e){if(e){var o={},n=this;return Object.keys(e).forEach(function(r){Object.prototype.hasOwnProperty.call(e,r)&&(t.Object.__innerEquals(e[r],n.attributes[r])||(o[r]=e[r]))}),!t.Object.isEmpty(o)&&o}return!t.Object.isEmpty(this.changed)&&this.changed},t.Model.prototype.hasChanged=function(e){return void 0!==e?t.Model._hasProperties(this.changed)&&Object.prototype.hasOwnProperty.call(this.changed,e):t.Model._hasProperties(this.changed)},t.Model.prototype.destroy=function(e){var o,n,r,i=e||{},s=i.wait,l=i.error,a=this;r=t.Model._copyOptions(i),o=t.Model._getSuccess(r);var c=this.GetCollection();return r.success=function(e){if(r.xhr&&(i.xhr=r.xhr),c){var n=t.StringUtils.isString(e)&&!t.StringUtils.isEmpty(e)?JSON.parse(e):e;c._setPagingReturnValues(n,null,e,!0)}s&&a._fireDestroy(!1),t.Model._fireSyncEvent(a,e,r,!1),o&&o.call(t.Model.GetContext(r,a),a,e,i)},r.error=function(e){a.TriggerInternal(!1,t.Events.EventType.ERROR,a,e,i),l&&l.apply(a,arguments)},this.isNew()?(s||this._fireDestroy(!1),o&&o.call(t.Model.GetContext(r,a),a,null,i),!1):(n=t.Model._internalSync("delete",this,r),s||this._fireDestroy(!1),n)},t.Model.prototype._fireRequest=function(e,o,n,r){this.TriggerInternal(r,t.Events.EventType.REQUEST,e,o,n)},t.Model.prototype._fireDestroy=function(e){this.TriggerInternal(e,t.Events.EventType.DESTROY,this,this.collection,null)},t.Model._fireSyncEvent=function(e,o,n,r){e.TriggerInternal(r,t.Events.EventType.SYNC,e,o,n)},t.Model.prototype.toJSON=function(){var t={},e=this;return Object.keys(e.attributes||{}).forEach(function(o){Object.prototype.hasOwnProperty.call(e.attributes,o)&&(Array.isArray(e.attributes[o])?t[o]=e.attributes[o].slice(0):t[o]=e.attributes[o])}),t},t.Model.prototype.previous=function(t){return this.previousAttrs[t]},t.Model.prototype.previousAttributes=function(){return this.previousAttrs},t.Model.prototype.sync=function(e,o,n){return t.sync(e,o,n)},t.Model._internalSync=function(e,o,n){var r=n||{};o.oauth&&(r.oauthHeader=o.oauth.getHeader()),!r.dataType&&o.dataType&&(r.dataType=o.dataType),!r.jsonpCallback&&o.jsonpCallback&&(r.jsonpCallback=o.jsonpCallback),"create"!==e&&"patch"!==e&&"update"!==e||(r.parsedData=o.parseSave.call(o,"patch"===e?r.attrs:r.saveAttrs));var i=null;o instanceof t.Model&&(i=o.GetId());var s={};Object.keys(r||{}).forEach(function(t){s[t]=r[t]});var l=t.Model.SetCustomURLOptions(i,o,r);return Object.keys(l||{}).forEach(function(t){s[t]=l[t]}),r.xhr=o.sync(e,o,s),s.xhr&&(r.xhr=s.xhr),r.xhr},t.Model.SetCustomURLOptions=function(e,o,n){var r=o instanceof t.Collection?o.ModifyOptionsForCustomURL(n):{};return e&&(r.recordID=e),r},t.sync=function(e,o,n){var r,i=n||{};function s(t){return o._fireRequest(o,t,i,i.silent),t}var l,a=i.success,c=i.error;if("create"===e.valueOf())return l=(l=o._saveUrl())||t.Model.GetPropValue(o,"url"),s((r=new t.RestImpl(l,o)).addRecord(i.parsedData,c,i,o));if("read"===e.valueOf())return o instanceof t.Model?(l=i.url?i.url:t.Model.GetPropValue(o,"url"),s((r=new t.RestImpl(l,o)).getRecord(a,c,o.GetId(),i,t.Model.GetContext(i,o)))):(l=o.GetCollectionFetchUrl(i),s((r=new t.RestImpl(l,o)).getRecords(a,c,i,o)));r=new t.RestImpl(t.Model.GetPropValue(o,"url"),o);var u=null;return o instanceof t.Model&&(u=o.GetId()),"update"===e.valueOf()?s(r.updateRecord(a,u,i.parsedData,c,i,o,!1)):"patch"===e.valueOf()?s(r.updateRecord(a,u,i.parsedData,c,i,o,!0)):"delete"===e.valueOf()?s(r.deleteRecord(u,c,i,o)):null},t.Model._urlError=function(t){if(!t.url)throw new Error("The url property or function must be specified")},t.ajax=function(o){return arguments&&arguments.length>0&&t.Model._urlError(arguments[0]),e.ajax.apply(t,arguments)},t.OAuth=function(e,o){t.OAuth._init(this,o||{},e||"Authorization")},t.Object.createSubclass(t.OAuth,t.Object,"oj.OAuth"),t.OAuth.prototype.Init=function(){t.OAuth.superclass.Init.call(this)},t.OAuth.prototype.getHeader=function(){var t={};return this.accessTokenResponse.access_token||this.clientCredentialGrant(),t[this.accessTokenRequest.auth_header]="Bearer "+this.accessTokenResponse.access_token,t},t.OAuth.prototype.isInitialized=function(){return!(!this.accessTokenResponse||!this.accessTokenResponse.access_token)},t.OAuth.prototype.clientCredentialGrant=function(){var o={},n=this;o[n.accessTokenRequest.auth_header]="Basic "+t.OAuth._base64_encode(n.accessTokenRequest.client_id+":"+n.accessTokenRequest.client_secret),e.ajax({type:"POST",async:!1,url:this.accessTokenRequest.bearer_url,data:"grant_type=client_credentials",headers:o,success:function(e){n.accessTokenResponse=t.OAuth._initAccessToken(n.accessTokenResponse,e)},error:function(t){throw new Error(t.responseText)}})},t.OAuth.prototype.setAccessTokenResponse=function(e){this.accessTokenResopnse=t.OAuth._initAccessToken(this.accessTokenResponse,e)},t.OAuth.prototype.getAccessTokenResponse=function(){return this.accessTokenResponse},t.OAuth.prototype.cleanAccessTokenResponse=function(){t.OAuth._cleanAccessToken(this.accessTokenResponse)},t.OAuth.prototype.setAccessTokenRequest=function(e){this.accessTokenRequest=t.OAuth._initAccessToken(this.accessTokenRequest,e)},t.OAuth.prototype.getAccessTokenRequest=function(){return this.accessTokenRequest},t.OAuth.prototype.cleanAccessTokenRequest=function(){t.OAuth._cleanAccessToken(this.accessTokenRequest)},t.OAuth._init=function(e,o,n){var r=e;r.Init(),r.accessTokenRequest={},r.accessTokenResponse={},o.access_token?r.accessTokenResponse=t.OAuth._initAccessToken(r.accessTokenResponse,o):o.client_id&&o.client_secret&&o.bearer_url&&(r.accessTokenResponse=t.OAuth._initAccessToken(r.accessTokenRequest,o)),r.accessTokenRequest.auth_header=n},t.OAuth._initAccessToken=function(t,e){var o=e||{},n=t||{};return Object.keys(o).forEach(function(t){Object.prototype.hasOwnProperty.call(o,t)&&(n[t]=o[t])}),n},t.OAuth._cleanAccessToken=function(t){var e=t||{};Object.keys(e).forEach(function(t){Object.prototype.hasOwnProperty.call(e,t)&&"auth_header"!==t&&(e[t]=null,delete e[t])})},t.OAuth._base64_encode=function(t){var e,o,n,r,i=0,s=0,l="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",a=[];do{e=t.charCodeAt(i),i+=1,o=t.charCodeAt(i),i+=1,n=t.charCodeAt(i),i+=1,e=(r=e<<16|o<<8|n)>>18&63,o=r>>12&63,n=r>>6&63,r&=63,a[s]=l.charAt(e)+l.charAt(o)+l.charAt(n)+l.charAt(r),s+=1}while(i<t.length);return a=a.join(""),((e=t.length%3)?a.slice(0,e-3):a)+"===".slice(e||3)},t.RestImpl=function(t,o){this.rootURL=t,this.model=o,this.customURL=o.customURL,e.support.cors=!0},t.RestImpl._HEADER_PROP="headers",t.RestImpl.addOptions=function(o,n,r){var i=e.extend(!0,o,r);return n&&Object.keys(n||{}).forEach(function(o){Object.prototype.hasOwnProperty.call(n,o)&&"oauthHeader"!==o&&(Object.prototype.hasOwnProperty.call(i,o)||(i[o]=n[o]),o===t.RestImpl._HEADER_PROP&&(i[o]=e.extend(!0,i[o],n[o])))}),n.oauthHeader&&(i[t.RestImpl._HEADER_PROP]||(i[t.RestImpl._HEADER_PROP]={}),Object.keys(n.oauthHeader||{}).forEach(function(e){Object.prototype.hasOwnProperty.call(n.oauthHeader,e)&&(Object.prototype.hasOwnProperty.call(i[t.RestImpl._HEADER_PROP],e)||(i[t.RestImpl._HEADER_PROP][e]=n.oauthHeader[e]))})),i},t.RestImpl.prototype.getRecords=function(e,o,n,r){var i=n||{},s="jsonp"===i.dataType,l=this._getURL("read",this.rootURL,this.customURL,null,r,i),a={crossDomain:i.crossDomain||!s,dataType:this._getDataType(i),jsonpCallback:i.jsonpCallback,context:null!==r?r:this,success:e,error:o};return a=this._addHeaderProp(a),a=t.RestImpl.addOptions(a,i,l),i.xhr=this.ajax(a,r),i.xhr},t.RestImpl.prototype._addHeaderProp=function(e){var o=e||{};return this.model&&this.model.omitLanguageHeader||(o[t.RestImpl._HEADER_PROP]={"Accept-Language":this.getLocale()}),o},t.RestImpl.prototype.getRecord=function(e,o,n,r,i){var s=r||{},l="jsonp"===s.dataType,a=this._getURL("read",this.rootURL,this.customURL,n,i,s),c={crossDomain:s.crossDomain||!l,dataType:this._getDataType(s),jsonpCallback:s.jsonpCallback,context:null!==i?i:this,success:e,error:o};return c=this._addHeaderProp(c),c=t.RestImpl.addOptions(c,s,a),s.xhr=this.ajax(c,i),s.xhr},t.RestImpl.prototype.updateRecord=function(e,o,n,r,i,s,l){var a=i||{},c="jsonp"===a.dataType,u=this._getURL(l?"patch":"update",this.rootURL,this.customURL,o,s,a),h=t.RestImpl._emulateHTTP(a),p={crossDomain:a.crossDomain||!c,contentType:this._getContentType(a),dataType:this._getDataType(a),jsonpCallback:a.jsonpCallback,data:this._getData(JSON.stringify(n),a,u),emulateHTTP:h,emulateJSON:t.RestImpl._emulateJSON(a),success:e,error:r,context:null!==s?s:this};return p=this._addHeaderProp(p),p=t.RestImpl.addOptions(p,a,u),p=t.RestImpl._beforeSendMod(h,p),a.xhr=this.ajax(p,s),a.xhr},t.RestImpl._beforeSendMod=function(t,e){var o=e||{};if(t){var n=o.beforeSend;o.beforeSend=function(t){return t.setRequestHeader("X-HTTP-Method-Override",o._method),n?n.apply(this,arguments):null}}return o},t.RestImpl.prototype._getData=function(e,o,n){if(t.RestImpl._emulateJSON(o)){var r={_method:n._method?n._method:n.type};return e&&(r.model=e),r}return e},t.RestImpl.prototype._getHTTPMethod=function(e,o){if(o.type)return{method:o.type};var n=null;if("create"===e&&(n="POST"),"delete"===e&&(n="DELETE"),"patch"===e&&(n="PATCH"),"update"===e&&(n="PUT"),t.RestImpl._emulateHTTP(o)){var r={method:"POST"};return r._method=n,r}return null===n&&(n="GET"),{method:n}},t.RestImpl._emulateHTTP=function(e){return e.emulateHTTP||t.emulateHTTP},t.RestImpl._emulateJSON=function(e){return e.emulateJSON||t.emulateJSON},t.RestImpl.prototype._getURL=function(o,n,r,i,s,l){var a=this._getHTTPMethod(o,l);if(e.isFunction(r)){var c=r.call(this,o,s,t.Model.SetCustomURLOptions(i,s,l));if(t.StringUtils.isString(c)){var u={url:c,type:a.method};return a._method&&(u._method=a._method),u}if(c)return c.url=Object.prototype.hasOwnProperty.call(c,"url")?c.url:n,Object.prototype.hasOwnProperty.call(c,"type")||(c.type=a.method),Object.prototype.hasOwnProperty.call(c,"data")||a._method&&(c._method=a._method),c}var h={url:t.Model.GetPropValue(null,n),type:a.method};return a._method&&(h._method=a._method),h},t.RestImpl.prototype.deleteRecord=function(e,o,n,r){var i=n||{},s="jsonp"===i.dataType,l=this._getURL("delete",this.rootURL,this.customURL,e,r,i),a=t.RestImpl._emulateHTTP(i),c=t.RestImpl._emulateJSON(i),u={crossDomain:i.crossDomain||!s,success:i.success,error:o,context:null!==r?r:this,emulateHTTP:a,emulateJSON:c},h=this._getData(null,i,l);return h&&(u.data=h),u=t.RestImpl.addOptions(u,i,l),u=t.RestImpl._beforeSendMod(a,u),i.xhr=this.ajax(u,r),i.xhr},t.RestImpl.prototype.addRecord=function(e,o,n,r){var i=n||{},s=JSON.stringify(e),l="jsonp"===i.dataType,a=this._getURL("create",this.rootURL,this.customURL,null,r,i),c=t.RestImpl._emulateHTTP(i),u={crossDomain:i.crossDomain||!l,contentType:i.contentType||"application/json",dataType:this._getDataType(i),jsonpCallback:i.jsonpCallback,data:this._getData(s,i,a),success:i.success,error:o,emulateHTTP:c,emulateJSON:t.RestImpl._emulateJSON(i),context:null!==r?r:this};return u=this._addHeaderProp(u),u=t.RestImpl.addOptions(u,i,a),i.xhr=this.ajax(u,r),i.xhr},t.RestImpl.prototype._getDataType=function(e){return t.RestImpl._emulateJSON(e)&&!t.RestImpl._emulateHTTP(e)?"application/x-www-form-urlencoded":e.dataType||"json"},t.RestImpl.prototype._getContentType=function(e){return t.RestImpl._emulateJSON(e)&&!t.RestImpl._emulateHTTP(e)?"application/x-www-form-urlencoded":e.contentType||"application/json"},t.RestImpl.prototype.getLocale=function(){return o.getLocale()},t.RestImpl.prototype.ajax=function(e,o){if(null===e.url||void 0===e.url)throw new t.URLError;var n=t.ajax(e);return o._addxhr&&o._addxhr(n),n},t.URLError=function(){this.name="URLError",this.message="No URL defined"},t.URLError.prototype=new Error,t.URLError.constructor=t.URLError;var i={};return i.Collection=t.Collection,i.Events=t.Events,i.Model=t.Model,i.OAuth=t.OAuth,i.URLError=t.URLError,i});