(function(){"use strict";u8.Crm.Widgets.LinkFieldInput=function(t,e){this.Widget(t,u8.tools.extendOwn(true,{},u8.Crm.Widgets.LinkFieldInput.defaults,e));this._isCreated=false;this._create()};u8.Crm.Widgets.LinkFieldInput.defaults={searchInput:{useProviderContextForGhostText:true}};u8.Crm.Widgets.LinkFieldInput.makeClass({name:"LinkFieldInput",deriveFrom:u8.Base.Widgets.Widget,events:["Create","Action"]});$.extend(u8.Crm.Widgets.LinkFieldInput.prototype,{dispose:function(){if(this._isDisposed){return}this.Widget_dispose();this._searchInput=null;this._textInput=null},getTextInput:function(){return this._textInput},getSearchInput:function(){return this._searchInput},getValue:function(){if(this.mode=="search"){if(this._searchInput){return this._searchInput.getInputText()}if(this._textInput){return this._textInput.getValue()}}return this._value},setValue:function(t){this._value=t;if(this._searchInput){this._searchInput.setText(this._value)}else if(this._textInput){this._textInput.setValue(this._value)}},_create:function(){this.children.clear();var t=this.$(this.element).html("<div />").children()[0];var e=u8.tools.extend(true,{tabIndex:this.tabIndex,textInput:{tabIndex:this.tabIndex}},this.searchInput||{});if(this.actions){u8.tools.extend(e.textInput,{actions:this.actions,onAction:this._onAction.bind(this)})}var i=u8.tools.created.curry(this,{});var s=this.actions&&this.findAsYouType&&this.findAsYouType.actionPropertyName&&this.findAsYouType.selectActionPropertyValue&&this.findAsYouType.quickSearch&&this.findAsYouType.quickSearch.infoAreaId;if(!s||this.disabled){return this._createWithoutFindAsYouType(t,e,i.bind(this))}var n=new u8.Crm.SearchProvider({quickSearch:this.findAsYouType.quickSearch});n.isFunctional(function(s){if(this._isDisposed){return}var u=s&&n.hasOutputFields();if(u){return this._createWithFindAsYouType(t,e,n,i.bind(this))}if(s){if(n.usesNamedFilter()){u8.log.error("LinkFieldInput detected filter "+n.quickSearch.filter+" in FindAsYouType definition")}else if(n.usesNamedQuery()){u8.log.error("LinkFieldInput detected query "+n.quickSearch.query+" without any output fields in FindAsYouType definition")}else{u8.log.error("LinkFieldInput-SearchProvider does not provide any output fields")}}this._createWithoutFindAsYouType(t,e,i.bind(this))}.bind(this))},_createWithoutFindAsYouType:function(t,e,i){u8.tools.extend(true,e,{textInput:{id:this.id+"_input",ghostText:this.ghostText,parent:this,readonly:true,autoDisableActions:false,disabled:true}});this._textInput=new u8.Base.Widgets.TextInput(t,e.textInput);if(this._value){this._textInput.setValue(this._value)}this.$(this._textInput.element).toggleClass("disabled",true);if(i){i()}},_createWithFindAsYouType:function(t,e,i,s){for(var n=0;n<this.actions.length;n++){var u=this.actions[n][this.findAsYouType.actionPropertyName];if(u===this.findAsYouType.selectActionPropertyValue){this._findAsYouTypeSelectAction=this.actions[n]}if(u==this.findAsYouType.deleteActionPropertyValue){this._findAsYouTypeDeleteAction=this.actions[n]}}if(!this._findAsYouTypeSelectAction){u8.log.error("FindAsYouType","Can not find action for find-as-you-type selection")}if(this.findAsYouType.freeInput){this._findAsYouTypeSetAction={_command:"Fields/Set",_fieldsInfo:this._findAsYouTypeSelectAction._fieldsInfo}}$.extend(e,{id:this.id+"_search",ghostText:this.ghostText,searchProvider:i,parent:this,textIsValue:false,onValueChange:this._onSearchInputValueChange.bind(this)});if(this.mode=="search"||this.findAsYouType.freeInput){e.allowNewValues=true}this._searchInput=new u8.Base.Widgets.SearchInput(t,e);if(!this.designer){this.$(this._searchInput.getWidget("input").getInputElement())}this._textInput=this._searchInput.getWidget("input");if(this._value){this.setValue(this._value)}if(s){s()}},_onAction:function(t,e){this.raise("Action",e)},_onSearchInputValueChange:function(t,e){var i=t.getValue();var s=this.findAsYouType&&this.findAsYouType.freeInput;if(!i&&i!==""&&!s){return}if(this.mode=="search"){this._value=i.text;return}var n;var u={};if(i===""){n=this._findAsYouTypeDeleteAction}else if(i.uid){n=this._findAsYouTypeSelectAction;u.uid=i.uid}else if(s){n=this._findAsYouTypeSetAction;u.text=i.text;this._value=i.text}if(!n){return}this._onAction(this,{action:n,data:u});setTimeout(function(){if(!this._isDisposed){this._searchInput.setText(this.getValue())}}.bind(this),100)}})})();