// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://@sbaseurl@/jsapi/jsapi/esri/copyright.txt and http://www.arcgis.com/apps/webappbuilder/copyright.txt for details.

//>>built
define("dojo/_base/declare dojo/_base/lang dojo/_base/array dojo/Deferred dojo/promise/all esri/lang jimu/portalUrlUtils ./table/_FeatureTable jimu/utils ./utils jimu/LayerInfos/LayerInfos".split(" "),function(w,l,n,q,x,r,v,y,z,t,A){return w(null,{_activeLayerInfoId:null,_activeRelationshipKey:null,nls:null,config:null,map:null,parent:null,_delayedLayerInfos:[],_layerInfosFromMap:[],featureTableSet:{},relationshipsSet:{},relationshipTableSet:{},currentRelationshipKey:null,relationshipInfoMapping:{},
constructor:function(a){this.widgetId=a&&a.widgetId;this.map=a&&a.map;this.nls=a&&a.nls;this.parent=a&&a.parent;this._delayedLayerInfos=[];this._layerInfosFromMap=[];this.featureTableSet={};this.relationshipsSet={};this.relationshipTableSet={};this.currentRelationshipKey=null;this.relationshipInfoMapping={}},setConfig:function(a){this.config=l.clone(a||{})},setMap:function(a){this.map=a},updateLayerInfoResources:function(a){var b=new q;t.readConfigLayerInfosFromMap(this.map,!1,!0).then(l.hitch(this,
function(c){this._layerInfosFromMap=c;this._processDelayedLayerInfos();a&&(0===this.config.layerInfos.length?(c=t.getConfigInfosFromLayerInfos(c),this.config.layerInfos=n.filter(c,function(e){return e.show})):this.config.layerInfos=n.filter(l.delegate(this.config.layerInfos),l.hitch(this,function(e){var f=this._getLayerInfoById(e.id),d=this.config.syncWithLayers?f.isVisible():e.show;d&&f&&(e.name=f.name||f.title,e.show=d);return d&&f})));b.resolve()}),function(c){b.reject(c)});return b},isEmpty:function(){return this.config&&
this.config.layerInfos&&0>=this.config.layerInfos.length},getConfigInfos:function(){return l.clone(this.config.layerInfos)},addLayerInfo:function(a){0===this._layerInfosFromMap.length?this._delayedLayerInfos.push(a):0<this._layerInfosFromMap.length&&!this._getLayerInfoById(a.id)&&this._layerInfosFromMap.push(a)},addConfigInfo:function(a){this._getConfigInfoById(a.id)||(a=t.getConfigInfoFromLayerInfo(a),this.config.layerInfos.push({id:a.id,name:a.name,layer:{url:a.layer.url,fields:a.layer.fields}}))},
removeLayerInfo:function(a){a=this._getLayerInfoById(a);a=this._layerInfosFromMap.indexOf(a);this._layerInfosFromMap.splice(a,1)},removeConfigInfo:function(a){if(l.getObject("config.layerInfos",!1,this))for(var b=this.config.layerInfos.length,c=0;c<b;c++)if(this.config.layerInfos[c].id===a){this.featureTableSet[a]&&(this.featureTableSet[a].destroy(),delete this.featureTableSet[a]);this.config.layerInfos.splice(c,1);break}},getQueryTable:function(a,b,c,e){var f=new q;this._activeLayerInfoId=a;this.featureTableSet[a]?
f.resolve({isSupportQuery:!0,table:this.featureTableSet[a]}):this._getQueryTableInfo(a).then(l.hitch(this,function(d){if(d){var g=d.layerInfo,h=d.layerObject;d=d.tableInfo;if(this.featureTableSet[a])f.resolve({isSupportQuery:d.isSupportQuery,table:this.featureTableSet[a]});else if(l.getObject("isSupportQuery",!1,d)){var k=this._getConfigInfoById(a);k||(this.addConfigInfo(g),k=this._getConfigInfoById(a));var m=l.getObject("layer.fields",!1,k);h=h&&h.fields;h=t.arcade.appendArcadeExpressionsToFields(h,
g);k.layer.fields=this._clipValidFields(m,h);g=new y({widgetId:this.widgetId,map:this.map,matchingMap:b,hideExportButton:c,allowTextSelection:e,layerInfo:g,configedInfo:k,nls:this.nls,parent:this.parent});g.onFeatureSelectionChange=l.hitch(this,function(){var p={},u;for(u in this.featureTableSet)this.featureTableSet[u]&&(p[u]=this.featureTableSet[u].selectionRows||[]);this.onFeatureSelectionChange(p)});this.featureTableSet[a]=g;f.resolve({isSupportQuery:d.isSupportQuery,table:g})}else f.resolve({isSupportQuery:!1})}else f.resolve(null)}),
function(d){f.reject(d)});return f},getRelationTable:function(a,b,c,e,f){var d=new q,g=this.relationshipsSet[b];this._activeRelationshipKey=b;if(g){var h=this._getLayerInfoById(a);a=this.getRelationShipInfo(g);this.getQueryTable(a.id,c,e,f).then(l.hitch(this,function(k){if(k&&k.table){var m=k.table;z.isEqual(m.relationship,g)||(m.set("prevRelationship",m.relationship),m.set("relationship",g));m.set("relatedOriginalInfo",h)}d.resolve(k)}),l.hitch(function(){d.resolve(null)}))}else d.resolve(null);
return d},removeRelationTable:function(a){this.relationshipTableSet[a]&&(this.relationshipTableSet[a].destroy(),this.relationshipTableSet[a]=null)},getCurrentTable:function(a){return this.featureTableSet[a]||this.relationshipTableSet[a]},collectRelationShips:function(a,b){this._collectRelationShips(a,a.layerObject,b)},getConfigInfoById:function(a){return this._getConfigInfoById(a)},getLayerInfoById:function(a){return this._getLayerInfoById(a)},getVisivleLayerInfoIndexById:function(a){return this._getVisivleLayerInfoIndexById(a)},
getRelationshipsByInfoId:function(a){var b=[],c;for(c in this.relationshipsSet){var e=this.relationshipsSet[c];e._layerInfoId===a&&b.push(e)}return b},getRelationShipInfo:function(a){var b;if(a)for(var c in this.relationshipInfoMapping)a._relKey===c&&(b=this.relationshipInfoMapping[c]);return b},empty:function(){this._delayedLayerInfos=[];this._layerInfosFromMap=[];this.featureTableSet={};for(var a in this.relationshipsSet)this._removeRelationShipInfo(this.relationshipsSet[a]);this.relationshipsSet=
{};this.relationshipTableSet={};this.nls=this.map=this.config=this.currentRelationshipKey=null},_processDelayedLayerInfos:function(){0<this._delayedLayerInfos.length&&(n.forEach(this._delayedLayerInfos,l.hitch(this,function(a){!this._getLayerInfoById(a&&a.id)&&this.map&&this.map.getLayer(a.id)&&this._layerInfosFromMap.push(a)})),this._delayedLayerInfos=[])},_getLayerInfoById:function(a){for(var b=0,c=this._layerInfosFromMap.length;b<c;b++)if(this._layerInfosFromMap[b]&&this._layerInfosFromMap[b].id===
a)return this._layerInfosFromMap[b]},_getVisivleLayerInfoIndexById:function(a){for(var b=-1,c=0,e=this._layerInfosFromMap.length;c<e;c++)if(this._layerInfosFromMap[c]&&this._layerInfosFromMap[c].isShowInMap()&&(b++,this._layerInfosFromMap[c].id===a))return b},_getConfigInfoById:function(a){if(!l.getObject("layerInfos.length",!1,this.config))return null;for(var b=0,c=this.config.layerInfos.length;b<c;b++){var e=this.config.layerInfos[b];if(e&&e.id===a)return e}return null},_getQueryTableInfo:function(a){var b=
new q,c=this._getLayerInfoById(a);if(c){var e=[],f=c.getUrl();e.push(c.getLayerObject());e.push(c.getSupportTableInfo());f&&e.push(c.getRelatedTableInfoArray());x(e).then(l.hitch(this,function(d){if(this._activeLayerInfoId===a&&d){var g=d[0],h=d[1];d=f?d[2]:[];r.isDefined(d)&&r.isDefined(g)&&0<d.length&&this._collectRelationShips(c,g,d);b.resolve({layerInfo:c,layerObject:g,tableInfo:h})}else b.resolve(null)}),function(d){b.reject(d)})}else console.error("no activeLayerInfo!"),b.reject(Error());return b},
_collectRelationShips:function(a,b,c){var e=b.relationships;if(e&&0<e.length&&b&&b.url){var f=b.url.split("/");n.forEach(e,l.hitch(this,function(d){f[f.length-1]=d.relatedTableId;var g=f.join("/"),h=n.filter(c,l.hitch(this,function(m){m=m.getUrl();return r.isDefined(m)&&r.isDefined(g)&&v.removeProtocol(m.toString().toLowerCase())===v.removeProtocol(g.toString().toLowerCase())})),k=a.id+"_"+d.name+"_"+d.id;d._relKey=k;d._layerInfoId=a.id;h&&0<h.length&&this._setRelationShipInfo(d,h[0]);this.relationshipsSet[k]||
(this.relationshipsSet[k]=d,this.relationshipsSet[k].objectIdField=b.objectIdField)}))}},_setRelationShipInfo:function(a,b){a&&"_relKey"in a&&(this.relationshipInfoMapping[a._relKey]=b)},_removeRelationShipInfo:function(a){if(a&&"_relKey"in a)for(var b in this.relationshipInfoMapping)a._relKey===b&&delete this.relationshipInfoMapping[b]},_getLayerInfoLabel:function(a){return a.name||a.title},_getLayerInfoId:function(a){return a&&a.id||""},_clipValidFields:function(a,b){if(!a||!a.length)return b||
[];if(!b||!b.length)return a;for(var c=[],e=[],f=0,d=b.length;f<d;f++){for(var g=b[f],h=0,k=a.length;h<k;h++){var m=a[h];if(m.name===g.name)break}h===k&&e.push(g)}f=0;for(d=a.length;f<d;f++)for(m=a[f],h=0,k=b.length;h<k;h++)if(g=b[h],g.name===m.name){c.push(m);break}c=c.concat(e);0===c.length&&0<a.length&&(a=l.clone(b),n.forEach(a,l.hitch(this,function(p){"esriFieldTypeGeometry"!==p.type&&(p.show=!0,c.push(p))})));return c}})});