function(e,t,r){"use strict";var a=e("../../core/renderer"),n=e("../../events/core/events_engine"),i=e("../../core/class"),s=e("../../core/guid"),l=e("../../core/component_registrator"),u=e("./utils.recurrence"),d=e("../../core/utils/dom"),o=e("../../core/utils/type").isDefined,h=e("../../core/utils/extend").extend,c=e("../../core/utils/array").inArray,_=e("../../core/utils/iterator").each,p=e("../editor/editor"),f=e("../check_box"),R=e("../radio_group"),v=e("../switch"),y=e("../number_box"),E=e("../select_box"),m=e("../date_box"),O=e("../../localization/message"),b=e("../../localization/date"),C=e("../../core/utils/date"),x=e("../../events/click"),g="dx-recurrence-editor",D="-label",k="-wrapper",$="dx-recurrence-editor-container",V="dx-recurrence-switch-repeat-end",Y="dx-recurrence-radiogroup-freq",B="dx-recurrence-numberbox-interval",L="dx-recurrence-interval-field",w="dx-recurrence-repeat-end",W="dx-recurrence-repeat-end-field",T="dx-recurrence-repeat-end-container",S="dx-recurrence-radiogroup-repeat-type",M="dx-recurrence-numberbox-repeat-count",U="dx-recurrence-datebox-until-date",A="dx-recurrence-repeat-on",H="dx-recurrence-repeat-on-week",q="dx-recurrence-checkbox-day-of-week",I="dx-recurrence-repeat-on-month",F="dx-recurrence-numberbox-day-of-month",N="dx-recurrence-repeat-on-year",z="dx-recurrence-selectbox-month-of-year",K="dx-field",P="dx-field-label",J="dx-field-value",j=[{text:function(){return O.format("dxScheduler-recurrenceDaily")},value:"DAILY"},{text:function(){return O.format("dxScheduler-recurrenceWeekly")},value:"WEEKLY"},{text:function(){return O.format("dxScheduler-recurrenceMonthly")},value:"MONTHLY"},{text:function(){return O.format("dxScheduler-recurrenceYearly")},value:"YEARLY"}],G=[{text:function(){return O.format("dxScheduler-recurrenceRepeatCount")},value:"count"},{text:function(){return O.format("dxScheduler-recurrenceRepeatOnDate")},value:"until"}],Q=["SU","MO","TU","WE","TH","FR","SA"],X=i.inherit({ctor:function(e){this._recurrenceRule=u.getRecurrenceRule(e).rule},makeRules:function(e){var t=this;this._recurrenceRule=u.getRecurrenceRule(e).rule},makeRule:function(e,t){t?o(e)&&("until"===e&&delete this._recurrenceRule.count,"count"===e&&delete this._recurrenceRule.until,this._recurrenceRule[e]=t):delete this._recurrenceRule[e]},repeatableRule:function(){var e=this._recurrenceRule;return"count"in e?"count":"until"in e?"until":null},recurrenceString:function(){return u.getRecurrenceString(this._recurrenceRule)},rules:function(){return this._recurrenceRule},daysFromByDayRule:function(){return u.daysFromByDayRule(this._recurrenceRule)}}),Z=p.inherit({_getDefaultOptions:function(){return h(this.callBase(),{value:null,startDate:new Date,firstDayOfWeek:void 0})},_getFirstDayOfWeek:function(){return o(this.option("firstDayOfWeek"))?this.option("firstDayOfWeek"):b.firstDayOfWeekIndex()},_createComponent:function(e,t,r){return r=r||{},this._extendConfig(r,{readOnly:this.option("readOnly")}),this.callBase(e,t,r)},_init:function(){this.callBase(),this._recurrenceRule=new X(this.option("value"))},_render:function(){this.callBase(),this.$element().addClass(g),this._$container=a("<div>").addClass($).appendTo(this.$element()),this._renderEditors(),o(this.option("value"))||this._handleDefaults(),this.option("visible",!!this.option("value")),this._renderContainerVisibility(this.option("value"))},_renderContainerVisibility:function(e){e?(this._$container.show(),d.triggerShownEvent(this._$container)):this._$container.hide()},_changeValueByVisibility:function(e){this._renderContainerVisibility(e),e?this.option("value")||this._handleDefaults():(this._recurrenceRule.makeRules(""),this.option("value",""))},_handleDefaults:function(){this._recurrenceRule.makeRule("freq","DAILY"),this._changeEditorValue()},_changeEditorValue:function(){this.option("value",this._recurrenceRule.recurrenceString()||"")},_renderEditors:function(){this._renderFreqEditor(),this._renderIntervalEditor(),this._renderRepeatOnEditor(),this._renderRepeatEndSwitch(),this._renderRepeatEndEditor(),this._renderRepeatEndVisibility(!!this._recurrenceRule.repeatableRule())},_renderFreqEditor:function(){var e=this._recurrenceRule.rules().freq||"DAILY",t=a("<div>").addClass(Y).addClass("dx-field-value");this._freqEditor=this._createComponent(t,R,{field:"freq",items:j,value:e,valueExpr:"value",displayExpr:"text",layout:"horizontal",onValueChanged:this._valueChangedHandler.bind(this)});var r=a("<div>").addClass("dx-field").append(t);this._$container.append(r)},_renderIntervalEditor:function(){var e=this._recurrenceRule.rules().freq||"DAILY",t=a("<div>").addClass(B).addClass("dx-field-value"),r=a("<div>").text(O.format("dxScheduler-recurrenceEvery")+":").addClass(B+"-label").addClass("dx-field-label");this._$intervalTypeLabel=a("<div>").text(O.format("dxScheduler-recurrenceRepeat"+e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).addClass(S+"-label");var n=this._recurrenceRule.rules().interval||1;this._intervalEditor=this._createComponent(t,y,{field:"interval",min:1,value:n,showSpinButtons:!0,useLargeSpinButtons:!1,onValueChanged:this._valueChangedHandler.bind(this)});var i=a("<div>").addClass("dx-field").addClass(L).append(r,t,this._$intervalTypeLabel);this._$container.append(i),this._setAriaDescribedBy(this._intervalEditor,r)},_renderRepeatOnEditor:function(){var e=this._recurrenceRule.rules().freq;if(o(this._$repeatOnEditor)||(this._$repeatOnEditor=a("<div>").addClass(A).addClass("dx-field").appendTo(this._$container)),!e||"DAILY"===e)return this._clearRepeatOnEditor(),void this._clearRepeatOnLabel();o(this._$repeatOnLabel)||this._renderRepeatOnLabel(this._$repeatOnEditor),"WEEKLY"!==e||this._$repeatOnWeek?"MONTHLY"!==e||this._$repeatOnMonth?"YEARLY"!==e||this._$repeatOnYear||this._renderRepeatOnYearEditor():this._renderRepeatOnMonthEditor():this._renderRepeatOnWeekEditor()},_renderRepeatOnLabel:function(e){this._$repeatOnLabel=a("<div>").text(O.format("dxScheduler-recurrenceOn")+":").addClass(A+"-label").addClass("dx-field-label"),e.append(this._$repeatOnLabel)},_clearRepeatOnEditor:function(){o(this._$repeatOnWeek)&&(this._$repeatOnWeek.detach(),this._$repeatOnWeek.remove(),delete this._$repeatOnWeek),o(this._$repeatOnMonth)&&(this._$repeatOnMonth.detach(),this._$repeatOnMonth.remove(),delete this._$repeatOnMonth),o(this._$repeatOnYear)&&(this._$repeatOnYear.detach(),this._$repeatOnYear.remove(),delete this._$repeatOnYear)},_clearRepeatOnLabel:function(){o(this._$repeatOnLabel)&&(this._$repeatOnLabel.detach(),this._$repeatOnLabel.remove(),delete this._$repeatOnLabel)},_renderRepeatOnWeekEditor:function(){this._clearRepeatOnEditor(),this._$repeatOnWeek=a("<div>").addClass(H).addClass("dx-field-value").appendTo(this._$repeatOnEditor);var e=b.getDayNames("short"),t=this._daysOfWeekByRules();this._daysOfWeek=[];for(var r=0;r<7;r++){var n,i=(this._getFirstDayOfWeek()+r)%7,s=e[i].toUpperCase(),l=Q[i],u=a("<div>").addClass(q),d=this._createComponent(u,f,{text:s,value:c(l,t)>-1,onValueChanged:this._repeatByDayValueChangeHandler.bind(this)});this._daysOfWeek[i]=d,this._$repeatOnWeek.append(u)}},_daysOfWeekByRules:function(){var e=this._recurrenceRule.daysFromByDayRule();return e.length||(e=[Q[this.option("startDate").getDay()]]),e},_repeatByDayValueChangeHandler:function(){var e="";_(this._daysOfWeek,function(t,r){if(r.option("value")){var a=Q[t];e=e?e+","+a:a}}),this._recurrenceRule.makeRule("byday",e),this._changeEditorValue()},_renderRepeatOnMonthEditor:function(){this._clearRepeatOnEditor(),this._$repeatOnMonth=a("<div>").addClass(I).addClass("dx-field-value").appendTo(this._$repeatOnEditor),this._renderDayOfMonthEditor(this._$repeatOnMonth)},_renderRepeatOnYearEditor:function(){this._clearRepeatOnEditor(),this._$repeatOnYear=a("<div>").addClass(N).addClass("dx-field-value").appendTo(this._$repeatOnEditor);for(var e=[],t=b.getMonthNames("wide"),r=0;r<12;r++)e[r]={value:String(r+1),text:t[r]};var n=this._monthOfYearByRules(),i=a("<div>").addClass(z).appendTo(this._$repeatOnYear),s=function(e){this._valueChangedHandler.call(this,e);var t=parseInt(e.component.option("value"));if(this._dayEditor&&t){var r=new Date((new Date).getFullYear(),parseInt(t),0).getDate();2===t&&(r=29),this._dayEditor.option("max",r)}};this._monthEditor=this._createComponent(i,E,{field:"bymonth",items:e,value:n,displayExpr:"text",valueExpr:"value",onValueChanged:s.bind(this)}),this._renderDayOfMonthEditor(this._$repeatOnYear)},_monthOfYearByRules:function(){var e=this._recurrenceRule.rules().bymonth;return e||(e=this.option("startDate").getMonth()+1),e},_renderDayOfMonthEditor:function(e){var t=this._dayOfMonthByRules(),r=a("<div>").addClass(F);this._dayEditor=this._createComponent(r,y,{field:"bymonthday",min:1,max:31,showSpinButtons:!0,useLargeSpinButtons:!1,value:t,onValueChanged:this._valueChangedHandler.bind(this)}),e.append(r)},_dayOfMonthByRules:function(){var e=this._recurrenceRule.rules().bymonthday;return e||(e=this.option("startDate").getDate()),e},_setAriaDescribedBy:function(e,t){var r="label-"+new s;e.setAria("describedby",r),e.setAria("id",r,t)},_renderRepeatEndSwitch:function(){var e=this,t=a("<div>").addClass(V).addClass("dx-field-value"),r=a("<div>").text(O.format("dxScheduler-recurrenceEnd")+":").addClass(B+"-label").addClass("dx-field-label");a("<div>").addClass("dx-field").addClass(W).append(r,t).appendTo(this._$container),this._switchEndEditor=this._createComponent(t,v,{value:!!this._recurrenceRule.repeatableRule(),onValueChanged:this._repeatEndSwitchValueChangeHandler.bind(this)}),this._setAriaDescribedBy(this._switchEndEditor,r)},_repeatEndSwitchValueChangeHandler:function(e){var t=e.value;this._renderRepeatEndVisibility(t),this._recurrenceRule.rules().count||this._recurrenceRule.rules().until||!t?t||(this._recurrenceRule.makeRule("count",""),this._recurrenceRule.makeRule("until",""),this._changeEditorValue()):this._handleRepeatEndDefaults()},_renderRepeatEndVisibility:function(e){e?this._$repeatEndEditor.show():this._$repeatEndEditor.hide()},_handleRepeatEndDefaults:function(){this._recurrenceRule.makeRule("count",1),this._changeEditorValue()},_renderRepeatEndEditor:function(e){(e=o(e)?e:this._recurrenceRule.repeatableRule())||(e="count"),o(this._$repeatEndEditor)||(this._$repeatEndEditor=a("<div>").addClass(T).addClass("dx-field").appendTo(this._$container),this._renderRepeatEndTypeEditor())},_renderRepeatEndTypeEditor:function(){var e=this._recurrenceRule.repeatableRule()||"count",t=this;this._$repeatTypeEditor=a("<div>").addClass(S).addClass("dx-field-value").appendTo(this._$repeatEndEditor),this._repeatTypeEditor=this._createComponent(this._$repeatTypeEditor,R,{items:G,value:e,displayExpr:"text",valueExpr:"value",itemTemplate:function(e){return"count"===e.value?t._renderRepeatCountEditor():t._renderRepeatUntilEditor()},layout:"vertical",onValueChanged:this._repeatTypeValueChangedHandler.bind(this)}),this._disableRepeatEndParts(e)},_repeatTypeValueChangedHandler:function(e){var t=e.value;this._disableRepeatEndParts(t),"until"===t?this._recurrenceRule.makeRule(t,this._getUntilValue()):"count"===t&&this._recurrenceRule.makeRule(t,this._repeatCountEditor.option("value")),this._changeEditorValue()},_disableRepeatEndParts:function(e){"until"===e?(this._repeatCountEditor.option("disabled",!0),this._repeatUntilDate.option("disabled",!1)):"count"===e&&(this._repeatCountEditor.option("disabled",!1),this._repeatUntilDate.option("disabled",!0))},_renderRepeatCountEditor:function(){var e=this._recurrenceRule.rules().count||1,t=a("<div>").addClass(w+"-wrapper");return a("<div>").text(O.format("dxScheduler-recurrenceAfter")+":").addClass(w+"-label").appendTo(t),this._$repeatCountEditor=a("<div>").addClass(M).addClass("dx-field-value").appendTo(t),a("<div>").text(O.format("dxScheduler-recurrenceRepeatCount")).addClass(w+"-label").appendTo(t),this._repeatCountEditor=this._createComponent(this._$repeatCountEditor,y,{field:"count",min:1,showSpinButtons:!0,useLargeSpinButtons:!1,value:e,onValueChanged:this._repeatCountValueChangeHandler.bind(this)}),t},_repeatCountValueChangeHandler:function(e){if("count"===this._recurrenceRule.repeatableRule()){var t=e.value;this._recurrenceRule.makeRule("count",t),this._changeEditorValue()}},_formatUntilDate:function(e){return this._recurrenceRule.rules().until&&C.sameDate(this._recurrenceRule.rules().until,e)?e:C.setToDayEnd(e)},_renderRepeatUntilEditor:function(){var e=this._recurrenceRule.rules().until||this._formatUntilDate(new Date),t=a("<div>").addClass(w+"-wrapper");return a("<div>").text(O.format("dxScheduler-recurrenceOn")+":").addClass(w+"-label").appendTo(t),this._$repeatDateEditor=a("<div>").addClass(U).addClass("dx-field-value").appendTo(t),this._repeatUntilDate=this._createComponent(this._$repeatDateEditor,m,{field:"until",value:e,type:"date",onValueChanged:this._repeatUntilValueChangeHandler.bind(this),calendarOptions:{firstDayOfWeek:this._getFirstDayOfWeek()}}),t},_repeatUntilValueChangeHandler:function(e){if("until"===this._recurrenceRule.repeatableRule()){var t=this._formatUntilDate(new Date(e.value));this._repeatUntilDate.option("value",t),this._recurrenceRule.makeRule("until",t),this._changeEditorValue()}},_valueChangedHandler:function(e){var t=e.component.option("value"),r=e.component.option("field");this._recurrenceRule.makeRule(r,t),this._makeRepeatOnRule(r,t),this._changeEditorValue()},_makeRepeatOnRule:function(e,t){"freq"===e&&("DAILY"===t&&(this._recurrenceRule.makeRule("byday",""),this._recurrenceRule.makeRule("bymonth",""),this._recurrenceRule.makeRule("bymonthday","")),"WEEKLY"===t&&(this._recurrenceRule.makeRule("byday",this._daysOfWeekByRules()),this._recurrenceRule.makeRule("bymonth",""),this._recurrenceRule.makeRule("bymonthday","")),"MONTHLY"===t&&(this._recurrenceRule.makeRule("bymonthday",this._dayOfMonthByRules()),this._recurrenceRule.makeRule("bymonth",""),this._recurrenceRule.makeRule("byday","")),"YEARLY"===t&&(this._recurrenceRule.makeRule("bymonthday",this._dayOfMonthByRules()),this._recurrenceRule.makeRule("bymonth",this._monthOfYearByRules()),this._recurrenceRule.makeRule("byday","")))},_optionChanged:function(e){switch(e.name){case"value":this._recurrenceRule.makeRules(e.value),this.option("visible",!!e.value),this._switchEndEditor.option("value",!!this._recurrenceRule.repeatableRule()),this._repeatTypeEditor.option("value",this._recurrenceRule.repeatableRule()||"count"),this._renderRepeatEndEditor(),this._renderRepeatOnEditor(),this._changeEditorsValues(this._recurrenceRule.rules()),this.callBase(e);break;case"startDate":this._clearRepeatOnEditor(),this._renderRepeatOnEditor(),this._makeRepeatOnRule("freq",this._recurrenceRule.rules().freq),o(this._recurrenceRule.recurrenceString())&&this._changeEditorValue();break;case"firstDayOfWeek":this._clearRepeatOnEditor(),this._renderRepeatOnEditor(),this._$repeatDateEditor&&this._repeatUntilDate.option("calendarOptions.firstDayOfWeek",this._getFirstDayOfWeek());break;case"visible":this._changeValueByVisibility(e.value),this.callBase(e);break;default:this.callBase(e)}},_changeEditorsValues:function(e){this._changeCheckBoxesValue(!!e.byday),this._freqEditor.option("value",e.freq),this._changeRepeatTypeLabel(),this._intervalEditor.option("value",e.interval),this._changeRepeatCountValue(),this._changeRepeatUntilValue(),this._changeDayOfMonthValue(),this._changeMonthOfYearValue()},_changeRepeatTypeLabel:function(){var e=this.$element().find("."+S+"-label");if(e.length){var t=this._recurrenceRule.rules().freq||"DAILY";_(e,function(e,r){a(r).text(O.format("dxScheduler-recurrenceRepeat"+t.charAt(0).toUpperCase()+t.substr(1).toLowerCase()))})}},_changeRepeatCountValue:function(){if(this._$repeatCountEditor){var e=this._recurrenceRule.rules().count||1;this._repeatCountEditor.option("value",e)}},_changeRepeatUntilValue:function(){this._$repeatDateEditor&&this._repeatUntilDate.option("value",this._getUntilValue())},_getUntilValue:function(){return this._recurrenceRule.rules().until||this._formatUntilDate(new Date)},_changeCheckBoxesValue:function(e){if(this._$repeatOnWeek&&e){var t=this._daysOfWeekByRules();_(this._daysOfWeek,function(e,r){var a=Q[e];r.option("value",c(a,t)>-1)})}},_changeDayOfMonthValue:function(){if(this._$repeatOnMonth||this._$repeatOnYear){var e=this._dayOfMonthByRules()||1;this._dayEditor.option("value",e)}},_changeMonthOfYearValue:function(){if(this._$repeatOnYear){var e=this._monthOfYearByRules()||1;this._monthEditor.option("value",e)}},toggle:function(){n.trigger(this._switchEditor.$element(),x.name)},setAria:function(){this._switchEditor&&this._switchEditor.setAria(arguments[0],arguments[1])}});l("dxRecurrenceEditor",Z),r.exports=Z}

