// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/dijit/geoenrichment/ReportPlayer/dataProviders/supportClasses/CustomReportsManager",["dojo/_base/declare","dojo/_base/lang","dojo/when","esri/arcgis/Portal","./ReportTemplateData"],function(d,f,e,g,h){var k=d(null,{allUserReports:null,cache:null,constructor:function(){this.cache={}}});d={_reportsCache:null,resetCache:function(){this._reportsCache=new k},_portal:null,_user:null,_getPortal:function(a){var c=this;if(this._portal&&this._portal.url===a.portalUrl)return this._portal;var b=
new g.Portal(a.portalUrl);return e(b.signIn(),function(a){c._user=a;return c._portal=b})},getCustomReports:function(a,c){var b=this;c&&(this._reportsCache.allUserReports=null);return this._reportsCache.allUserReports?this._getCountryReports(this._reportsCache.allUserReports,a):e(this._getPortal(a),function(){return b._portal.queryItems({num:1E3,start:1,q:'type:"Report Template" owner:'+b._user.username+' typekeywords:"esriWebReport"',sortField:"modified",sortOrder:"desc"}).then(function(c){b._reportsCache.allUserReports=
c.results.map(function(a){return b._prepareCustomReportFromItem(a,b._portal)});return b._getCountryReports(b._reportsCache.allUserReports,a)})})},_getCountryReports:function(a,c){return a&&a.filter(function(a){return!a.countries||a.countries.split(",").some(function(a){return c.countryID===a.trim()})})},_prepareCustomReportFromItem:function(a,c){var b=f.mixin({},a.properties||a.details);b.isGraphicReport=!!b.isGraphicReport;b.reportID=a.id;var d={title:a.title,templateData:new h(a,c),modified:a.modified.getTime()};
f.mixin(d,b);return d},getCustomReportByID:function(a){var c=this,b=this._reportsCache.cache[a.reportID];if(b)return b;b=e(this._getPortal(a),function(){return c._user.getItem(a.reportID).then(function(a){return c._prepareCustomReportFromItem(a,c._portal)})});return this._reportsCache.cache[a.reportID]=b}};d.resetCache();return d});