text: function(t,e,r){"use strict";var i=t("../../core/class"),a=t("../../core/config"),n=t("../../core/utils/iterator"),s=t("../../core/utils/date_serialization"),o=t("./utils.recurrence"),u=t("../../core/utils/date"),c=t("../../core/utils/common"),l=t("../../core/utils/type"),h=t("../../core/utils/array").inArray,d=t("../../core/utils/extend").extend,f=t("../../core/utils/array"),_=t("../../data/query"),m=u.dateToMilliseconds,p=0,g=1,D=i.inherit({ctor:function(t){this._filterRegistry=null,this._dataAccessors=t},isRegistered:function(){return!!this._filterRegistry},clearRegistry:function(){delete this._filterRegistry},make:function(t,e){this._filterRegistry||(this._filterRegistry={}),this._make[t].apply(this,e)},_make:{date:function(t,e,r){var i=r?this._dataAccessors.getter.startDate:this._dataAccessors.expr.startDateExpr,a=r?this._dataAccessors.getter.endDate:this._dataAccessors.expr.endDateExpr,n=this._dataAccessors.expr.recurrenceRuleExpr;this._filterRegistry.date=[[[a,">",t],[i,"<",e]],"or",[n,"startswith","freq"],"or",[[a,t],[i,t]]],n||this._filterRegistry.date.splice(1,2)},user:function(t){this._filterRegistry.user=t}},combine:function(){var t=[];return this._filterRegistry.date&&t.push(this._filterRegistry.date),this._filterRegistry.user&&t.push(this._filterRegistry.user),t},dateFilter:function(){return this._filterRegistry.date}}),y=function(t,e,r,i,a){var n=u.dateTimeFromDecimal(r),s;return t.getHours()>=n.hours&&t.getMinutes()>=n.minutes||e.getHours()===n.hours&&e.getMinutes()>n.minutes||e.getHours()>n.hours||a||i},A=function(t,e,r,i,a,n){var s,o=(24-i+r)*m("hour"),c=e.getTime()-t.getTime(),l=(o-c)/m("hour"),h=t.getHours(),d=t.getMinutes(),f=u.dateTimeFromDecimal(i);return s=h<f.hours||h===f.hours&&d<f.minutes||a&&t<=n,c<o&&h>f.hours&&d>f.minutes&&l<=h-i&&(s=!1),s},v=i.inherit({_createFilter:function(t,e,r,i){this._filterMaker.make("date",[t,e]);var a=this._excessFiltering()?this._dataSource.filter()[1]:this._dataSource.filter();this._filterMaker.make("user",[a]),r&&this._dataSource.filter(this._combineRemoteFilter(i))},_excessFiltering:function(){var t=this._filterMaker.dateFilter(),e=this._dataSource.filter();return e&&(c.equalByValue(e,t)||e.length&&c.equalByValue(e[0],t))},_combineFilter:function(){return this._filterMaker.combine()},_getStoreKey:function(t){var e;return this._dataSource.store().keyOf(t)},_filterAppointmentByResources:function(t,e){var r=!1;function i(){var r,i=this._dataAccessors.getter.resources[o];l.isFunction(i)&&(r=i(t));for(var s=f.wrapToArray(r),u=n.map(e[a].items,function(t){return t.id}),c=0,d=s.length;c<d;c++)if(h(s[c],u)>-1)return!0;return!1}for(var a=0,s=e.length;a<s;a++){var o=e[a].name;if(!(r=i.call(this)))return!1}return r},_filterAppointmentByRRule:function(t,e,r,i,a,n){var s=t.recurrenceRule,u=t.recurrenceException,c=t.allDay,l=!0,h=t.startDate,d=t.endDate;if(c||this._appointmentPartInInterval(h,d,i,a)){var f=this._trimDates(e,r);e=f.min,r=new Date(f.max.getTime()-m("minute"))}return s&&!o.getRecurrenceRule(s).isValid&&(l=d>e&&h<=r),l&&o.getRecurrenceRule(s).isValid&&(l=o.dateInRecurrenceRange({rule:s,exception:u,start:h,end:d,min:e,max:r,firstDayOfWeek:n})),l},_appointmentPartInInterval:function(t,e,r,i){var a=t.getHours(),n=e.getHours();return a<=r&&n<=i&&n>=r||n>=i&&a<=i&&a>=r},_createCombinedFilter:function(t,e){var r=this._dataAccessors,i=t.startDayHour,a=t.endDayHour,n=new Date(t.min),s=new Date(t.max),o=t.resources,u=t.firstDayOfWeek,c=t.recurrenceException,h=this;return[[function(d){var f,_=!0,m=new Date(r.getter.startDate(d)),p=new Date(r.getter.endDate(d)),g=h.appointmentTakesAllDay(d,i,a),D=h.appointmentTakesSeveralDays(d),v=r.getter.allDay(d),R=D||g,S=l.isDefined(r.getter.recurrenceRule);S&&(f=r.getter.recurrenceRule(d)),o&&o.length&&(_=h._filterAppointmentByResources(d,o)),g&&!1===t.allDay&&(_=!1);var k=r.getter.startDateTimeZone(d),x=r.getter.endDateTimeZone(d),T=e(m,k),F=e(p,x);if(_&&S){var M=c?c(d):r.getter.recurrenceException(d);_=h._filterAppointmentByRRule({startDate:T,endDate:F,recurrenceRule:f,recurrenceException:M,allDay:g},n,s,i,a,u)}return _&&F<n&&R&&!v&&(!S||S&&!f)&&(_=!1),_&&void 0!==i&&(_=y(T,F,i,g,D)),_&&void 0!==a&&(_=A(T,F,i,a,g,s)),_&&S&&!f&&F<n&&!v&&(_=!1),_}]]},ctor:function(t,e,r){this.setDataAccessors(e),this.setDataSource(t),this._updatedAppointmentKeys=[],this._filterMaker=new D(e),this._baseAppointmentDuration=r},setDataSource:function(t){this._dataSource=t,this.cleanModelState(),this._initStoreChangeHandlers(),this._filterMaker&&this._filterMaker.clearRegistry()},_initStoreChangeHandlers:function(){this._dataSource&&this._dataSource.store().on("updating",function(t){this._updatedAppointment=t}.bind(this)),this._dataSource&&this._dataSource.store().on("push",function(t){t.forEach(function(t){this._updatedAppointmentKeys.push({key:this._dataSource.store().key(),value:t.key})}.bind(this))}.bind(this))},getUpdatedAppointment:function(){return this._updatedAppointment},getUpdatedAppointmentKeys:function(){return this._updatedAppointmentKeys},cleanModelState:function(){this._updatedAppointment=null,this._updatedAppointmentKeys=[]},setDataAccessors:function(t){this._dataAccessors=t,this._filterMaker=new D(t)},filterByDate:function(t,e,r,i){if(this._dataSource){var a=this._trimDates(t,e);if(this._filterMaker.isRegistered()){if(this._filterMaker.make("date",[a.min,a.max]),this._dataSource.filter()&&this._dataSource.filter().length>1){var n=this._serializeRemoteFilter([this._dataSource.filter()[1]],i);this._filterMaker.make("user",n)}r&&this._dataSource.filter(this._combineRemoteFilter(i))}else this._createFilter(a.min,a.max,r,i)}},_combineRemoteFilter:function(t){var e=this._filterMaker.combine();return this._serializeRemoteFilter(e,t)},_serializeRemoteFilter:function(t,e){if(!Array.isArray(t))return t;t=d([],t);var r=this._dataAccessors.expr.startDateExpr,i=this._dataAccessors.expr.endDateExpr;l.isString(t[0])&&a().forceIsoDateParsing&&t.length>1&&(t[0]!==r&&t[0]!==i||(t[t.length-1]=s.serializeDate(new Date(t[t.length-1]),e)));for(var n=0;n<t.length;n++)t[n]=this._serializeRemoteFilter(t[n],e);return t},filterLoadedAppointments:function(t,e){l.isFunction(e)||(e=function(t){return t});var r=this._createCombinedFilter(t,e);if(this._filterMaker.isRegistered()){var i=this._trimDates(t.min,t.max);this._filterMaker.make("date",[i.min,i.max,!0]);var a=this.customizeDateFilter(this._filterMaker.combine(),e);r.push([a])}return _(this._dataSource.items()).filter(r).toArray()},_trimDates:function(t,e){var r=u.trimTime(new Date(t)),i=u.trimTime(new Date(e));return i.setDate(i.getDate()+1),{min:r,max:i}},hasAllDayAppointments:function(t,e,r){if(!t)return!1;var i=this,a=!1;return n.each(t,function(t,n){if(i.appointmentTakesAllDay(n,e,r))return a=!0,!1}),a},appointmentTakesAllDay:function(t,e,r){var i=this._dataAccessors,a=i.getter.startDate(t),n=i.getter.endDate(t),s;return i.getter.allDay(t)||this._appointmentHasAllDayDuration(a,n,e,r)},_appointmentHasAllDayDuration:function(t,e,r,i){t=new Date(t),e=new Date(e);var a=24,n;return this._getAppointmentDurationInHours(t,e)>=24||this._appointmentHasShortDayDuration(t,e,r,i)},_appointmentHasShortDayDuration:function(t,e,r,i){var a,n;return this._getAppointmentDurationInHours(t,e)>=i-r&&t.getHours()===r&&e.getHours()===i},_getAppointmentDurationInHours:function(t,e){return(e.getTime()-t.getTime())/m("hour")},appointmentTakesSeveralDays:function(t){var e=this._dataAccessors,r=e.getter.startDate(t),i=e.getter.endDate(t),a=u.trimTime(new Date(r)),n=u.trimTime(new Date(i));return a.getTime()!==n.getTime()},customizeDateFilter:function(t,e){var r=d(!0,[],t);return function(t){var i=new Date(this._dataAccessors.getter.startDate(t)),a=new Date(this._dataAccessors.getter.endDate(t));a=this.fixWrongEndDate(t,i,a),t=d(!0,{},t);var n=this._dataAccessors.getter.startDateTimeZone(t),s=this._dataAccessors.getter.endDateTimeZone(t),o=e(i,n),u=e(a,s);return this._dataAccessors.setter.startDate(t,o),this._dataAccessors.setter.endDate(t,u),_([t]).filter(r).toArray().length>0}.bind(this)},fixWrongEndDate:function(t,e,r){return this._isEndDateWrong(t,e,r)&&(r=this._dataAccessors.getter.allDay(t)?u.setToDayEnd(new Date(e)):new Date(e.getTime()+this._baseAppointmentDuration*m("minute")),this._dataAccessors.setter.endDate(t,r)),r},_isEndDateWrong:function(t,e,r){return!r||isNaN(r.getTime())||e.getTime()>=r.getTime()},add:function(t,e){return this._dataSource.store().insert(t).done(function(){this._dataSource.load()}.bind(this))},update:function(t,e){var r=this._getStoreKey(t);return this._dataSource.store().update(r,e).done(function(){this._dataSource.load()}.bind(this))},remove:function(t){var e=this._getStoreKey(t);return this._dataSource.store().remove(e).done(function(){this._dataSource.load()}.bind(this))}});r.exports=v}
  
  