define(["qlik","qvangular","jquery","core.utils/deferred"],function(t,o,a,i){"use strict";return{isServerSet:function(t){return 0<t.npsod.conn.server.length},isAppSet:function(t){return 0<t.npsod.conn.app.length},doGetActionURL:function(t,e){var n=t;return n.trim(),"/"!=n.slice(-1)&&(n+="/"),n+e},getApps:function(e){var n=this;return n.getLoginNtlm(e.npsod.conn.server).then(function(){return n.getConnections(e.npsod.conn.server,null).then(function(t){return t&&0!=t.length?n.getAppsWithConnection(e.npsod.conn.server,t):[]})})},getAppsWithConnection:function(r,o,t){t||(t=0);var i=this;return a.ajax({url:i.doGetActionURL(r,"api/v1/apps?sort=+name,-created&limit=100&offset="+t),method:"GET",xhrFields:{withCredentials:!0}}).then(function(t){var n=[];if(!t)return n;t.data.items.forEach(function(e){o.some(function(t){return t.appId===e.id})&&n.push({value:e.id,label:50<e.name.length?e.name.slice(0,47)+"...":e.name})});var e=t.data.items.length+t.data.offset;return e<t.data.totalItems?i.getAppsWithConnection(r,o,e).then(function(t){return n.concat(t)}):n})},getConnectionIds:function(t){return this.isAppSet(t)?this.getConnections(t.npsod.conn.server,t.npsod.conn.app).then(function(t){return t.map(function(t){return{value:t.id,label:50<t.name.length?t.name.slice(0,47)+"...":t.name}})}):[]},getConnections:function(o,i,t){t||(t=0);var s=this,e="api/v1/connections?sort=+name,-created&limit=100&offset="+t;return i&&(e+="&appId="+i),a.ajax({url:s.doGetActionURL(o,e),method:"GET",xhrFields:{withCredentials:!0}}).then(function(t){var e=[],n=new RegExp(".+appid="+s.qApp.id+";.+");t.data.items.forEach(function(t){n.test(t.connectionString)&&e.push(t)});var r=t.data.items.length+t.data.offset;return r<t.data.totalItems?s.getConnections(o,i,r).then(function(t){return e.concat(t)}):e})},doGetReportlist:function(r,o,t){t||(t=0);var i=this,e=i.doGetActionURL(r,"api/v1/reports?sort=+title,-created&appId="+o+"&limit=100&offset="+t);return a.ajax({url:e,method:"GET",xhrFields:{withCredentials:!0}}).then(function(t){var e=t.data.items,n=t.data.items.length+t.data.offset;return n<t.data.totalItems?i.doGetReportlist(r,o,n).then(function(t){return e.concat(t)}):e})},doGetExportFormats:function(t,e){var n=this.doGetActionURL(t,"api/v1/reports/"+e);return a.ajax({url:n,method:"GET",xhrFields:{withCredentials:!0}})},getReportsForDropdown:function(t){return this.isServerSet(t)&&this.isAppSet(t)?this.doGetReportlist(t.npsod.conn.server,t.npsod.conn.app).then(function(t){return t.map(function(t){return{value:t.id,label:50<t.title.length?t.title.slice(0,47)+"...":t.title}})}):[]},getExportFormatsForDropdown:function(t){return 0==this.isServerSet(t)||t.npsod.conn.report.length<1?[]:this.doGetExportFormats(t.npsod.conn.server,t.npsod.conn.report).then(function(t){return t.data.outputFormats.map(function(t){return{value:t,label:t.toUpperCase()}})})},getLoginNtlm:function(e,n){n||(n=0);var r=this;return a.ajax({url:r.doGetActionURL(e,"api/v1/login/ntlm"),method:"GET",xhrFields:{withCredentials:!0}}).catch(function(t){if(401===t.status||403===t.status){if(!(3<n))return r.getLoginNtlm(e,++n);o.getService("qvAlertDialog").show({title:"Unauthorized",message:"User could not be authenticated.",closeOnEscape:!1})}throw t})},doGetTasks:function(t,e,n){n||(n=0);var r=i();return e?a.ajax({url:this.doGetActionURL(t,"api/v1/ondemand/requests?sort=-created&appId="+e+"&limit=100&offset="+n),method:"GET",xhrFields:{withCredentials:!0},success:function(t){r.resolve(t)},error:function(t){r.reject(t)}}):r.reject({message:"Must specify app",status:400}),r.promise},doDeleteTask:function(t,e){var n=this.doGetActionURL(t,"api/v1/ondemand/requests/"+e);return a.support.cors=!0,a.ajax({url:n,headers:{"access-control-allow-headers":"content-type"},method:"DELETE",xhrFields:{withCredentials:!0}})},downloadTask:function(t,e){var n=this.doGetActionURL(t,"api/v1/ondemand/requests/"+e+"/result"),r=i();return navigator.vendor&&-1<navigator.vendor.indexOf("Apple")||/(Trident|MSIE)/.test(navigator.userAgent)?(window.open(n),r.resolve()):a("#download").on("load",function(){r.resolve()}).attr("src",n),r.promise}}});