angular.module("app").service("datasource",["message","core",function(e,r){var t=this;this._cache={},this._resolveCache={},this.get=function(a,s,n){if(angular.isString(a))return!n&&this._cache[a]?void setTimeout((function(){s(t._cache[a])})):void $.ajax({type:"POST",dataType:"JSON",url:r.shopURL,data:{kk_get_datasource:a,kk_dropper_admin_pass:r.adminSecret},success:function(r){return r.results?(r.cacheable&&(t._cache[a]=r.results),s(r.results)):(e.error('Error fetching Datasource "'+a+'"',r,!1),void s(null))},error:function(r){e.error('Error fetching Datasource "'+a+'"',r,!1),s(null)}});e.error("Given Datasource was not in String Format:",a),setTimeout((function(){s(null)}))},this.search=function(t,a,s,n){return angular.isString(t)&&angular.isString(a)?$.ajax({type:"POST",url:r.shopURL,data:{kk_get_datasource_search:t,kk_get_datasource_search_query:a,kk_get_datasource_search_page:s||0,kk_dropper_admin_pass:r.adminSecret},success:function(e){if(e&&e.results)return n(e)},error:function(e){}}):(e.error("Given Datasource or query was not in String Format:",t),setTimeout((function(){n(null)})),null)};var sortOptions=function(e,r,t){if(!angular.isArray(e)||!e.length)return[];for(var a=[],s=0;s<e.length;s++){var n=e[s],u=t.find((function(e){return e&&e[r]===n}));if(void 0!==u)a.push(u);else{var o={};o[r]=n,a.push(o)}}return a};this.resolve=function(a,s,n,u){if(!angular.isString(a)||!angular.isString(s))return e.error("Could not resolve Datasource"),void setTimeout((function(){u(null)}));n=n||[];for(var o=a+":"+s,i=[],c=[],l=0;l<n.length;l++){var _=n[l];t._resolveCache[o]&&t._resolveCache[o][_]?c.push(t._resolveCache[o][_]):i.push(_)}i.length?$.ajax({type:"POST",url:r.shopURL,data:{kk_get_datasource_query:a,kk_get_datasource_query_mapping:s,kk_get_datasource_query_keys:i||[],kk_dropper_admin_pass:r.adminSecret},success:function(e){if(e&&e.results){for(var r=0;r<e.results.length;r++){var a=e.results[r];t._resolveCache[o]||(t._resolveCache[o]={}),void 0!==a[s]&&(t._resolveCache[o][a[s]]=a),c.push(a)}return u(sortOptions(n,s,c))}},error:function(e){setTimeout((function(){u(null)}))}}):setTimeout((function(){u(sortOptions(n,s,c))}))}}]);