(function(){"use strict";u8.Crm.Widgets.DayWeekPlanning=function(e,i){this.SearchListDragAndDrop(e,$.extend(true,{},u8.Crm.Widgets.DayWeekPlanning.defaults,i,{dropAreaFactory:this._createCalendar.bind(this),onLoad:this._onLoad.bind(this),onCanDrop:this._onCanDrop.bind(this),onDragLeave:this._onDragOut.bind(this),onDragEnd:this._onDragOut.bind(this),onDragDrop:this._onDragDrop.bind(this),onSearchSelectionChange:this._onSearchSelectionChange.bind(this)}));this._updateHighlightTimesDespammed=this._updateHighlightTimes.despam(1e3)};u8.Crm.Widgets.DayWeekPlanning.makeClass({name:"DayWeekPlanning",deriveFrom:u8.Crm.Widgets.SearchListDragAndDrop});u8.Crm.Widgets.DayWeekPlanning.defaults={calendar:{header:{headerOptions:{headerName:"DayWeekPlanningCalendar",headerGroupName:"Special"}}},draggableDataType:"application/x-vnd-update-day-week-planning-item"};$.extend(u8.Crm.Widgets.DayWeekPlanning.prototype,{dispose:function(){if(this._isDisposed){return}this.SearchListDragAndDrop_dispose()},setHighlightTimes:function(e){if(!this._dropArea){return}this._dropArea.model.setHighlightTimes(e)},getWidget:function(e){if(e=="calendar"){e="dropArea"}return this.SearchListDragAndDrop_getWidget(e)},_onLoad:function(e,i){i.pending=true;Function.chain(this._loadSchemas,this._validateInfoAreaIds.async()).call(this,i.proceed)},_loadSchemas:function(e){u8.using({schema:["FI","KP","PE"].concat(this.infoAreaIds)},e)},_createCalendar:function(e){return u8.widget("Calendar",e,$.extend(true,this.calendar,{parent:this,viewOptions:{Common:{externalDropDataTypes:[this.draggableDataType]}}}))},_validateInfoAreaIds:function(){if(this._isDisposed){return}var e=this.infoAreaIds.filter(this._isInfoAreaValid.bind(this));if(e.length<=0){$error("No valid infoAreaIds defined for DayWeekPlanning.")}if(e.length!=this.infoAreaIds.length){var i=this.infoAreaIds.filter(this._isInfoAreaValid.bind(this).compose(u8._.not));u8.log.warn("The infoAreaIds '"+i.join(",")+"' can not be used for DayWeekPlanning.")}this.infoAreaIds=e},_isInfoAreaValid:function(e){if(!e){return false}if(this._isAccount(e)){return true}var i=u8.services.schema.getInfoArea(e);var t=i.links&&i.links.map(u8._.attr("destinationInfoAreaId"));if(t&&t.some(this._isAccount.bind(this))){return true}return false},_onSearchSelectionChange:function(){this._updateHighlightTimesDespammed()},_updateHighlightTimes:function(e){if(this._isDisposed){return}if(!e){var i=this.getWidget("list");e=i&&i.getSelection({type:"rowsUid"});if(!e||e.length<=0){this.setHighlightTimes(null);return}}var t=this;this._getAccounts(e,function(e){var i={contactTimeTypes:t.contactTimeTypes,uids:e.map(u8._.attr("accountUid"))};u8.services.collaboration.getContactTimes(i,function(e){t.setHighlightTimes(e.contactTimes)})})},_isAccount:function(e){var i=["FI","KP","PE"];if(i.contains(e)){return true}var t=u8.services.schema.getInfoArea(e);if(t.isVirtualInfoArea&&i.contains(t.baseInfoAreaId)){return true}return false},_getAccounts:function(e,i){function t(e){return i&&i(e)}function n(e){return{uid:e,accountUid:e}}if(!e||e.length<=0){return t([])}if(this._isAccount(e[0].infoAreaId)){return t(e.map(n))}u8.services.accounts.getAccounts(e,function(e,i){return t(i.filter(function(e){return u8.isValidRecordUid(e.accountUid)}))})},_onCanDrop:function(e,i){if(i.dropDate){this._dropArea.setVirtualItem(i.dropDate)}i.canDrop=!!i.dropDate},_onDragOut:function(e,i){this._dropArea.setVirtualItem(null)},_onDragDrop:function(e,i){var t=i.dropDate;var n=i.uids;var a=this;function r(e){var i=e.uid;var t=e.accountUid;var n=[t];var r=a.sourceLink&&a.sourceLink[i.infoAreaId];if(typeof r!="undefined"){n.push($.extend({},i,{linkId:r}))}return n}function s(e){a._dropArea.createItems(t,e)}this._getAccounts(n,u8._.map(r).compose(s))}})})();