(function($){$.widget("lws.lac_model",{options:{mode:'autocomplete',origin:undefined,count:20,},_create:function(){this.originsList=[];this.currentOrigin={};this.dataSource=new Map();this.flatSource=new Map();this.groupSource=new Map();this.showMore=!1;$.ajaxSettings.cache=!1;this._setOrigin(this.options.origin);this._setSource()},_bind:function(fn,me,data){return function(){return fn.apply(me,data)}},_setSource:function(){var domObject=$(this.options.origin.element);this.dataSource=new Map();this.flatSource=new Map();this.groupSource=new Map();this.ajax={};if(domObject[0].nodeName=="SELECT"){for(var i=0;i<domObject[0].options.length;i++){var dataLine={};dataLine.value=domObject[0].options[i].value;dataLine.label=domObject[0].options[i].text;if(domObject[0].options[i].closest('optgroup')!=null){var groupName=domObject[0].options[i].closest('optgroup').label;if(!this.dataSource.has(groupName)){var grp={'label':groupName,'group':new Map()};this.dataSource.set(groupName.toString(),grp);this.groupSource.set(groupName.toString(),grp)}
this.dataSource.get(groupName.toString()).group.set(dataLine.value.toString(),dataLine);this.flatSource.set(dataLine.value.toString(),dataLine)}else{this.dataSource.set(dataLine.value.toString(),dataLine);this.flatSource.set(dataLine.value.toString(),dataLine)}}};if(this.options.origin.options.source!=undefined){this._updateSource(this.dataSource,this.options.origin.options.source)}
if(domObject.data('source')!=undefined&&domObject.data('source')!=''){this._updateSource(this.dataSource,lwsBase64.toObj(domObject.data('source')))}
if(this.options.origin.options.ajax!=undefined){this.showMore=!0;this.ajax.action=this.options.origin.options.ajax}
if(domObject.data('ajax')!=undefined){this.showMore=!0;this.ajax.action=domObject.data('ajax');this._remoteSource('','force');this._sendBack([],'force')}
if(this.options.origin.options.spec!=undefined)
this.ajax.spec=this.options.origin.options.spec;if(domObject.data('sourceurl')!=undefined)
this.ajax.sourceurl=domObject.data('sourceurl');if(this.options.origin.options.minsearch!=undefined)
this.options.minsearch=this.options.origin.options.minsearch;if(domObject.data('minsearch')!=undefined)
this.options.minsearch=domObject.data('minsearch');if(this.options.origin.options.minoption!=undefined)
this.options.minoption=this.options.origin.options.minoption;if(domObject.data('minoption')!=undefined)
this.options.minoption=domObject.data('minoption');if(this.options.origin.options.minlength!=undefined)
this.options.minlength=this.options.origin.options.minlength;if(domObject.data('minlength')!=undefined)
this.options.minlength=domObject.data('minlength');},_setOrigin:function(origin){var uuid=origin.uuid;if(this.originsList[uuid]==''||this.originsList[uuid]==undefined){this.originsList[uuid]={};this.originsList[uuid].resLoad=origin.resLoad;this.originsList[uuid].resChange=origin.resChange;this.originsList[uuid].target=origin;this.originsList[uuid].searched='';this.originsList[uuid].page=0;this.originsList[uuid].count=this.options.count}
this.currentOrigin=this.originsList[uuid]},reInit:function(origin){this._setOrigin(origin);this.currentOrigin.page=0;this._setSource()},showMoreResults:function(origin){this._setOrigin(origin);this.currentOrigin.page+=1;this._remoteSource(this.currentOrigin.searched)},returnLabels:function(initData,origin,mode){this.resCount=0;if(origin!=undefined&&origin!='')
this._setOrigin(origin);let result=[];if(initData!=undefined&&initData!==!1&&initData.length>0&&initData[0]!==null){let notFound=[];result=this._flatSearch(initData,'value',notFound);if(notFound.length>0){if((this.ajax.sourceurl!=undefined&&this.ajax.sourceurl!='')||(this.ajax.action!=undefined&&this.ajax.action!='')){if(initData[0]!=''){this._remoteSource(initData,"init",!0,!0)}}
for(var i=0;i<notFound.length;i++){let res={'value':notFound[i].toString(),'label':notFound[i],'html':''};result.push(res);this._updateSource(this.dataSource,res)}}}
this._sendBack(result,"init")},getLabelFromValue:function(value){var notFound=[];var result=this._flatSearch([value],'value',notFound);return result.length?result[0].label:''},isSubChain:function(searchString,origin){if(origin!=undefined&&origin!='')this._setOrigin(origin);if(this.currentOrigin.searched.toLowerCase().substring(0,searchString.length)==searchString.toLowerCase()&&searchString.length<this.currentOrigin.searched.length)return!0;return!1},getValuesFromLabels:function(labelsData,readOnly){result=[];notFound=[];result=this._flatSearch(labelsData,'label',notFound);if(notFound.length>0&&readOnly!=undefined&&readOnly){for(var i=0;i<notFound.length;i++){res={};res.value=notFound[i];res.label=notFound[i];res.html='';result.push(res);this._updateSource(this.dataSource,res)}}
return result},research:function(searchString,origin,forceAjax){if((searchString==''||searchString==undefined)&&(forceAjax==undefined||forceAjax==!1))return;if(origin!=undefined&&origin!=''){this._setOrigin(origin)}
if(forceAjax==!0){if(this.ajax.action!=undefined){this._remoteSource('')}else{this._sendBack(this.dataSource,"ok")}}else{if(searchString.length<this.options.minlength)return;if(searchString.toLowerCase().substring(0,this.currentOrigin.searched.length)==this.currentOrigin.searched.toLowerCase){resultData=this._recursiveSearch(searchString,this.dataSource,!1);if(resultData.length<this.options.minoption&&(this.ajax.sourceurl!=undefined||this.ajax.action!=undefined)){this.currentOrigin.searched=searchString;this.currentOrigin.page=0;this._remoteSource(searchString)}else{this._sendBack(resultData,"ok")}}else{this.currentOrigin.searched=searchString;this.currentOrigin.page=0;if(searchString.length>this.options.minsearch&&(this.ajax.sourceurl!=undefined||this.ajax.action!=undefined)){this._remoteSource(searchString)}else{resultData=this._recursiveSearch(searchString,this.dataSource,!1);if(resultData.length<this.options.minoption&&(this.ajax.sourceurl!=undefined||this.ajax.action!=undefined)){this._remoteSource(searchString)}else{this._sendBack(resultData,"ok")}}}}},returnSource:function(){this._sendBack(this.dataSource,"ok")},getSource:function(){return this._mapToArray(this.dataSource)},addToSource:function(data){this._updateSource(this.dataSource,data)},clone:function(obj){if(obj==null||typeof(obj)!='object')
return obj;if(obj instanceof Map)
return new Map(obj);var temp=new obj.constructor();for(var key in obj)
temp[key]=this.clone(obj[key]);return temp},_flatSearch:function(searched,from,notFound){var resultData=[];var me=this;$.each(searched,function(i,searchedValue){let found=!1;if('value'==from){let item=me.flatSource.get(searchedValue.toString());if(undefined!=item){resultData.push(me.clone(item));found=!0}}else for(const[j,sourceValue]of me.flatSource){if(searchedValue==sourceValue.label){resultData.push(me.clone(sourceValue));found=!0;break}}
if(!found)
notFound.push(searchedValue);});return resultData},_recursiveSearch:function(searched,dataSource,fromVal){var resultData=new Map();if(dataSource.size==0)return resultData;for(const[key,item]of dataSource){if(item.group!=undefined){retour=this._recursiveSearch(searched,item.group,fromVal);if(retour.size>0){resultData.set(key,this.clone(item));resultData[key].set('group',retour)}}else{if(fromVal==!0){for(var i=0;i<searched.length;i++){if(searched[i]==item.value){resultData.set(key,this.clone(item));this.resCount-=1}}}else{researched=item.label.toLowerCase();if(Array.isArray(searched)){for(let i=0;i<searched.length;i++){if(this.options.mode=='autocomplete'||this.options.mode=='select'){if(researched.substring(0,searched.length)==searched[i].toLowerCase()){resultData.set(key,this.clone(item));this.resCount-=1}}else if(this.options.mode=='research'){if(researched.search(searched.toLowerCase())!=-1){resultData.set(key,this.clone(item));this.resCount-=1}}}}else{if(this.options.mode=='autocomplete'||this.options.mode=='select'){if(researched.substring(0,searched.length)==searched.toLowerCase()){resultData.set(key,this.clone(item))}}else if(this.options.mode=='research'){if(researched.search(searched.toLowerCase())!=-1){resultData.set(key,this.clone(item))}}}}}}
return resultData},_updateSource:function(dataSource,newSource,from,me){if(undefined==me)
me=this;$.each(newSource,function(index,sourceItem){if(sourceItem.group!=undefined){var label=sourceItem.label.toString();var keys=undefined;if(me.groupSource.has(label)){keys=me._updateSource(me.groupSource.get(label).group,sourceItem.group,label,me)}else{var grp={'label':label,'group':new Map()};dataSource.set(sourceItem.label.toString(),grp);me.groupSource.set(sourceItem.label.toString(),grp);keys=me._updateSource(grp.group,sourceItem.group,label,me)}
me.groupSource.get(label).group.forEach(function(v,k,m){dataSource.delete(k)})}else if(sourceItem.value!=undefined){var value=sourceItem.value.toString();if(undefined!==from||!me.flatSource.has(value)){me.flatSource.set(value,sourceItem);dataSource.set(value,sourceItem)}}});return dataSource.keys()},_sendBack:function(resultData,status){if(resultData instanceof Map)
resultData=this._mapToArray(resultData);let result=[resultData,status,this.showMore];if(this.currentOrigin.resChange!=undefined){this.currentOrigin.resChange.fn.apply(this.currentOrigin.target,[result])}},_mapToArray:function(m){var me=this;var a=[];m.forEach(function(obj,k,map){let item={};for(const[key,value]of Object.entries(obj)){if(key!='group')
item[key]=value;else item.group=me._mapToArray(value)}
a.push(item)});return a},_getResultLength:function(items){length=0;for(var key in items){length+=(items[key].group)?Object.keys(items[key].group).length:1}
return length},_remoteSource:function(searched,initStatus,fromVal){if(this.currentOrigin.resLoad!=undefined)
this.currentOrigin.resLoad.fn.apply(this.currentOrigin.target);var currentPage=this.currentOrigin.page;var args={action:this.ajax.action,term:searched,fromValue:fromVal?'yes':'',lac:lws_lac_model_data.number};if(!fromVal||'init'!=initStatus){args.count=this.currentOrigin.count;args.page=currentPage}
if(this.currentOrigin.target.element.data('spec')!=undefined)
args.spec=this.currentOrigin.target.element.data('spec');let ajax_url=(this.ajax.sourceurl!=undefined)?this.ajax.sourceurl:lws_ajax.url;var me=this;$.getJSON(ajax_url,args,function(items){let resultData=[];let message="";if(null==items)message="Autocomplete ressource error.";else if(0===items)message="Autocomplete action not found.";else{me._updateSource(me.dataSource,items);me.showMore=(me._getResultLength(items)==me.currentOrigin.count);if(initStatus=="force"){return}
if(initStatus=="init"){message="init";let notFound=[];resultData=me._flatSearch(searched,'value',notFound);if(notFound.length>0){console.log("some items couldn't be found on datasource");console.log(notFound)}}else{message="ok";me.currentOrigin.searched=searched;if(currentPage>0)
resultData=me.dataSource;else{if('object'==typeof items){for(var prop in items)resultData.push(items[prop]);}}}
me._sendBack(resultData,message)}}).fail(function(d,textStatus,error){me._sendBack([],`Autocomplete loading error, status: ${textStatus}, error: ${error}`)});}})})(jQuery)