(function(){"use strict";u8.Crm.SkypeForBusiness=function(){this._isCreated=false;this.commandBindings={"SkypeForBusiness/Call":this._onCommandCall.bind(this)};this.create();this._isCreated=true};$.extend(u8.Crm.SkypeForBusiness.prototype,{create:function(){this.settings={};this.settings.fastCopyEnabled=u8.session.webConfigValues.getBoolean("SkypeForBusiness.FastCopyEnabled")},_makeCall:function(e){var t=u8.services.phone.getProxy();if(!t){return}try{if(e){t.makeCall("","",e,"").done(function(e){var t=JSON.parse(e);if(!t.success){u8.services.notification.error("TXT_SkypeForBusiness_4")}})}}catch(e){u8.log.exception("phone","u8.Crm.Phone.makeCall: An attempt to make a call failed with an exception.",e)}},_makeCallForRecord:function(e){this._getContactIdFromDb(e.uid,e.fieldId,function(e){if(e.contactId){this._makeCall(e.contactId)}}.bind(this))},_onCommandCall:function(e,t,n){var i=u8.isValidRecordUid(n.uid)&&(n.fieldId||n.fieldId===0);var o={uid:n.uid,fieldId:n.fieldId,sender:e};var s=u8.tools.isInstanceOfClassOrSubclass(e,"TextInput"),a=u8.Base.Widgets.Tools.closest(e,"ExpandView"),d=this.settings&&this.settings.fastCopyEnabled,r=a&&a.model&&a.model.isModified(),c=s&&e.getValue();var l;var u=this;if(i){this._getContactIdFromDb(n.uid,n.fieldId,function(e){if(!e.contactId){return}l=e.contactId;if(!s||!a){this._makeCall(e.contactId)}else{this._getContactIdFromPhone(h.bind(this))}}.bind(this))}else if(d&&s){this._getContactIdFromPhone(f.bind(this))}function h(t){var n=t.contactId,i=c&&r,s=!d||!t.contactId,a=!c;if(s&&!a&&!i){u._makeCall(l)}else if(!s||!a){u._showCopyOrCallMenu({hideCopy:s,saveDial:i,hideDial:a,contactId:l,contactIdFromPhone:t.contactId,sender:e,call:o})}}function f(t){var n=t.contactId;if(n&&n!=c){this._showCopyOrCallMenu({hideDial:true,contactIdFromPhone:n,sender:e})}}},_getContactIdFromDb:function(e,t,n){u8.request("SkypeForBusinessChannel.GetSkypeForBusinessContactId",{uid:e,fieldId:t},function(e,t){var i=t.response?t.response.skypeForBusinessContactId:null;n({contactId:i})}.bind(this))},_getContactIdFromPhone:function(e){var t=u8.services.phone.getProxy();if(!t){e({});return}t.getOriginalNumber("","","").then(function(t){var n=JSON.parse(t);var i=n.number&&n.number.indexOf("@")>=0;var o=i?n.number:null;e({contactId:o})})},_showCopyOrCallMenu:function(e){var t={CONTACTID:e.contactIdFromPhone};u8.using({texts:["SkypeForBusiness"]},function(){var i=e.saveDial?3:2;t["DIALSKYPEFORBUSINESSCAPTION"]=$t("SkypeForBusiness",i);var o=u8.widget("ContextMenu",{parent:e.sender,model:{menuName:"M_SkypeForBusiness",variables:t},alignTo:e.sender&&e.sender.element,executeOptions:{arguments:{contactId:e.contactId,contactIdFromPhone:e.contactIdFromPhone,save:e.saveDial,textField:e.sender,call:e.call}},onLoad:n.bind(this)});o.show()});function n(t,n){var i=t.model;if(i&&i.rootItem&&i.rootItem.children){var o=i.rootItem.children.find(u8._.eqish("name","A_CopySkypeForBusinessContactId")),s=i.rootItem.children.find(u8._.eqish("name","A_DialSkypeForBusinessContactId"));if(e.hideCopy){o.remove()}if(e.hideDial){s.remove()}i.update(i.rootItem)}}},copyContactId:function(e){if(!this.settings.fastCopyEnabled){return}if(e.textField&&e.contactIdFromPhone){e.textField.setValue(e.contactIdFromPhone)}},dialContact:function(e){if(e.save){var t=u8.Base.Widgets.Tools.closest(e.call.sender,"ExpandView");if(t){t.bind("Save",function(){t.unbind("Save",this.id);this._makeCallForRecord(e.call)}.bind(this),this.id);t.save()}}else{this._makeCall(e.contactId)}}});u8.services.add({name:"skypeForBusiness",scope:"Application",ctor:u8.Crm.SkypeForBusiness})})();