(function(){"use strict";u8.Crm.Widgets.LyrisSegmentCreator=function(e,t){this.Widget(e,$.extend({},u8.Crm.Widgets.LyrisSegmentCreator.defaults,t));this.commandBindings={CreateSegment:this._onCommandCreateSegment.bind(this)};this._create()};u8.Crm.Widgets.LyrisSegmentCreator.defaults={};u8.Crm.Widgets.LyrisSegmentCreator.makeClass({name:"LyrisSegmentCreator",deriveFrom:u8.Base.Widgets.Widget,events:["Create","SegmentStateChange","SegmentCreate"]});$.extend(u8.Crm.Widgets.LyrisSegmentCreator.prototype,{createSegment:function(){var e=this;u8.services.lyris.createSegment(this.activityUid,function(t){e._onCreateSegmentCompleted(t)})},_create:function(){var e=this;Function.chain({onError:"abort"},this._initViewModel,this._getSegment,this._initTemplate,function(t){e.raise("Create");t()}).call(this)},_initViewModel:function(e){this._viewModel=u8.create("Model",{properties:{segmentDetailsExist:false,segmentDescription:"",segmentName:"",segmentUrl:"",segmentDetailsErrorMessage:"",denyCreateSegment:this.segmentHasBeenCreated,segmentDoesNotExist:!this.segmentHasBeenCreated}});e()},_initTemplate:function(e){this.$(this.element).utml({url:"~/crm/LyrisSegmentCreator.utml.htm",data:{},itemOptions:{parent:this,context:this._viewModel}},e)},_getSegment:function(e){if(!this.segmentHasBeenCreated){e();return}var t=this;u8.services.lyris.getSegment(this.activityUid,function(s){var r;switch(s.result){case u8.Crm.Lyris.GetSegmentResult.success:t._viewModel.set("segmentDetailsExist",true);var i=$t("TXT_Lyris_Integration_97").format({segmentName:s.segment.name});t._viewModel.set("segmentDescription",i);t._viewModel.set("segmentName",s.segment.name);t._viewModel.set("segmentUrl",s.segment.url);break;case u8.Crm.Lyris.GetSegmentResult.errorDuringRetrievingSegments:t._viewModel.set("segmentDetailsExist",false);r=$t("TXT_Lyris_Integration_80").format({lyrisErrorMessage:s.errorMessage});t._viewModel.set("segmentDetailsErrorMessage",r);break;case u8.Crm.Lyris.GetSegmentResult.segmentNotFound:t._viewModel.set("segmentDetailsExist",false);r=$t("TXT_Lyris_Integration_82");t._viewModel.set("segmentDetailsErrorMessage",r);t._viewModel.set("denyCreateSegment",false);t.raise("SegmentStateChange",{segmentExists:false});break}e()})},_onCreateSegmentCompleted:function(e){var t="";var s="";var r=false;var i=false;var n="";var a="";var o="";switch(e.result){case u8.Crm.Lyris.CreateSegmentResult.success:s=$t("TXT_Lyris_Integration_83");r=true;i=true;n=$t("TXT_Lyris_Integration_97").format({segmentName:e.segmentName});a=e.segmentName;o=e.segmentUrl;break;case u8.Crm.Lyris.CreateSegmentResult.couldNotRetrieveActivityExecutionDemographic:t=$t("TXT_Lyris_Integration_79").format({lyrisErrorMessage:e.errorMessage});break;case u8.Crm.Lyris.CreateSegmentResult.couldNotCreateSegment:t=$t("TXT_Lyris_Integration_81").format({lyrisErrorMessage:e.errorMessage});break;case u8.Crm.Lyris.CreateSegmentResult.activityExecutionDemographicIsNotConfigured:t=$t("TXT_Lyris_Integration_84");break;case u8.Crm.Lyris.CreateSegmentResult.activityExecutionDemographicNotMultiSelectList:t=$t("TXT_Lyris_Integration_113");break;case u8.Crm.Lyris.CreateSegmentResult.couldNotUpdateActivityExecutionDemographic:t=$t("TXT_Lyris_Integration_114").format({lyrisErrorMessage:e.errorMessage});break}this._viewModel.set("segmentName",a);this._viewModel.set("segmentUrl",o);this._viewModel.set("segmentDoesNotExist",!r);this._viewModel.set("segmentDescription",n);this._viewModel.set("segmentDetailsExist",i);this._viewModel.set("denyCreateSegment",r);this._viewModel.set("infoMessage",s);this._viewModel.set("errorMessage",t);this.raise("SegmentCreate",{success:r})},_onCommandCreateSegment:function(){this._viewModel.set("segmentDetailsErrorMessage","");this._viewModel.set("denyCreateSegment",true);this._viewModel.set("errorMessage","");this._viewModel.set("infoMessage","");this.createSegment()}})})();