// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See https://js.arcgis.com/4.16/esri/copyright.txt for details.
//>>built
define("require exports ../../../core/tsSupport/extendsHelper ../../../Graphic ../../kernel ../support/FeatureSet ../support/IdSet ../support/shared ../support/sqlUtils ../../../core/promiseUtils ../../../core/sql/WhereClause ../../../geometry/SpatialReference".split(" "),function(r,t,u,B,C,x,w,v,q,p,y,D){Object.defineProperty(t,"__esModule",{value:!0});r=function(){function n(){this.sqlRewritable=!1}n.prototype.postInitialization=function(e,a){};return n}();t.AdaptedField=r;var z=function(n){function e(a){var b=
n.call(this)||this;b.field=a;b.sqlRewritable=!0;return b}u(e,n);e.prototype.extractValue=function(a){return a.attributes[this.field.name]};e.prototype.rewriteSql=function(a){return{rewritten:this.sqlRewritable,where:a}};return e}(r);t.OriginalField=z;var A=function(n){function e(a,b,c){var f=n.call(this)||this;f.originalField=a;f.sqlRewritable=!0;f.field=v.cloneField(a);f.field.name=b;f.field.alias=c;return f}u(e,n);e.prototype.rewriteSql=function(a,b){return{rewritten:this.sqlRewritable,where:q.reformulateWithoutField(a,
this.field.name,this.originalField.name,b.getFieldsIndex())}};e.prototype.extractValue=function(a){return a.attributes[this.originalField.name]};return e}(r);t.FieldRename=A;var E=function(n){function e(a,b,c){var f=n.call(this)||this;f.field=a;f.codefield=b;f.lkp=c;f.reverseLkp={};for(var d in c)f.reverseLkp[c[d]]=d;f.sqlRewritable=!0;return f}u(e,n);e.prototype.rewriteSql=function(a,b){var c=this.evaluateNodeToWhereClause(a.parseTree,v.FeatureServiceDatabaseType.Standardised,this.field.name,this.codefield instanceof
y.WhereClause?q.toWhereClause(this.codefield,v.FeatureServiceDatabaseType.Standardised):this.codefield,a.parameters);return 0<=c.indexOf(e.BADNESS)?{rewritten:!1,where:a}:{rewritten:this.sqlRewritable,where:y.WhereClause.create(c,b._parent.getFieldsIndex())}};e.prototype.evaluateNodeToWhereClause=function(a,b,c,f,d){void 0===c&&(c=null);void 0===f&&(f=null);var h;switch(a.type){case "interval":return q.convertIntervalToSql(this.evaluateNodeToWhereClause(a.value,b,c,f,d),a.qualifier,a.op);case "case_expression":h=
" CASE ";"simple"===a.format&&(h+=this.evaluateNodeToWhereClause(a.operand,b,c,e.BADNESS,d));for(f=0;f<a.clauses.length;f++)h+=" WHEN "+this.evaluateNodeToWhereClause(a.clauses[f].operand,b,c,e.BADNESS,d)+" THEN "+this.evaluateNodeToWhereClause(a.clauses[f].value,b,c,e.BADNESS,d);null!==a.else&&(h+=" ELSE "+this.evaluateNodeToWhereClause(a.else,b,c,e.BADNESS,d));return h+" END ";case "param":c=d[a.value.toLowerCase()];if("string"===typeof c)return"'"+d[a.value.toLowerCase()].toString().replace(/'/g,
"''")+"'";if(c instanceof Date)return q.makeDateString(c,b);if(c instanceof Array){a=[];for(f=0;f<c.length;f++)"string"===typeof c[f]?a.push("'"+c[f].toString().replace(/'/g,"''")+"'"):c[f]instanceof Date?a.push(q.makeDateString(c[f],b)):a.push(c[f].toString());return a}return c.toString();case "expr_list":h=[];var k=0;for(a=a.value;k<a.length;k++)h.push(this.evaluateNodeToWhereClause(a[k],b,c,f,d));return h;case "unary_expr":return" ( NOT "+this.evaluateNodeToWhereClause(a.expr,b,c,e.BADNESS,d)+
" ) ";case "binary_expr":switch(a.operator){case "AND":return" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" AND "+this.evaluateNodeToWhereClause(a.right,b,c,f,d)+") ";case "OR":return" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" OR "+this.evaluateNodeToWhereClause(a.right,b,c,f,d)+") ";case "IS":if("null"!==a.right.type)throw Error("Unsupported RHS for IS");return" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" IS NULL )";case "ISNOT":if("null"!==a.right.type)throw Error("Unsupported RHS for IS");
return" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" IS NOT NULL )";case "IN":h=[];if("expr_list"===a.right.type){if("column_ref"===a.left.type&&a.left.column.toUpperCase()===this.field.name.toUpperCase()){h=[];for(var k=!0,m=0,g=a.right.value;m<g.length;m++){var l=g[m];if("string"===l.type)if(void 0!==this.lkp[l.value])h.push(this.lkp[l.value].toString());else{k=!1;break}else{k=!1;break}}if(k)return" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" IN ("+h.join(",")+")) "}h=this.evaluateNodeToWhereClause(a.right,
b,c,f,d);return" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" IN ("+h.join(",")+")) "}h=this.evaluateNodeToWhereClause(a.right,b,c,f,d);return h instanceof Array?" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" IN ("+h.join(",")+")) ":" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" IN ("+h+")) ";case "NOT IN":h=[];if("expr_list"===a.right.type){if("column_ref"===a.left.type&&a.left.column.toUpperCase()===this.field.name.toUpperCase()){h=[];k=!0;m=0;for(g=a.right.value;m<g.length;m++)if(l=
g[m],"string"===l.type)if(void 0!==this.lkp[l.value])h.push(this.lkp[l.value].toString());else{k=!1;break}else{k=!1;break}if(k)return" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" NOT IN ("+h.join(",")+")) "}h=this.evaluateNodeToWhereClause(a.right,b,c,f,d);return" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" NOT IN ("+h.join(",")+")) "}h=this.evaluateNodeToWhereClause(a.right,b,c,f,d);return h instanceof Array?" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" NOT IN ("+h.join(",")+
")) ":" ("+this.evaluateNodeToWhereClause(a.left,b,c,f,d)+" NOT IN ("+h+")) ";case "BETWEEN":return f=this.evaluateNodeToWhereClause(a.right,b,c,e.BADNESS,d)," ("+this.evaluateNodeToWhereClause(a.left,b,c,e.BADNESS,d)+" BETWEEN "+f[0]+" AND "+f[1]+" ) ";case "NOTBETWEEN":return f=this.evaluateNodeToWhereClause(a.right,b,c,e.BADNESS,d)," ("+this.evaluateNodeToWhereClause(a.left,b,c,e.BADNESS,d)+" NOT BETWEEN "+f[0]+" AND "+f[1]+" ) ";case "LIKE":return""!==a.escape?" ("+this.evaluateNodeToWhereClause(a.left,
b,c,e.BADNESS,d)+" LIKE "+this.evaluateNodeToWhereClause(a.right,b,c,e.BADNESS,d)+" ESCAPE '"+a.escape+"') ":" ("+this.evaluateNodeToWhereClause(a.left,b,c,e.BADNESS,d)+" LIKE "+this.evaluateNodeToWhereClause(a.right,b,c,e.BADNESS,d)+") ";case "NOT LIKE":return""!==a.escape?" ("+this.evaluateNodeToWhereClause(a.left,b,c,e.BADNESS,d)+" NOT LIKE "+this.evaluateNodeToWhereClause(a.right,b,c,e.BADNESS,d)+" ESCAPE '"+a.escape+"') ":" ("+this.evaluateNodeToWhereClause(a.left,b,c,e.BADNESS,d)+" NOT LIKE "+
this.evaluateNodeToWhereClause(a.right,b,c,e.BADNESS,d)+") ";case "\x3c\x3e":case "\x3d":if("column_ref"===a.left.type&&"string"===a.right.type){if(a.left.column.toUpperCase()===this.field.name.toUpperCase()&&void 0!==this.lkp[a.right.value.toString()])return" ("+f+" "+a.operator+" "+this.lkp[a.right.value.toString()].toString()+") "}else if("column_ref"===a.right.type&&"string"===a.left.type&&a.right.column.toUpperCase()===this.field.name.toUpperCase())return" ("+this.lkp[a.right.value.toString()].toString()+
" "+a.operator+" "+f+") ";return" ("+this.evaluateNodeToWhereClause(a.left,b,c,e.BADNESS,d)+" "+a.operator+" "+this.evaluateNodeToWhereClause(a.right,b,c,e.BADNESS,d)+") ";case "\x3c":case "\x3e":case "\x3e\x3d":case "\x3c\x3d":case "*":case "-":case "+":case "/":return" ("+this.evaluateNodeToWhereClause(a.left,b,c,e.BADNESS,d)+" "+a.operator+" "+this.evaluateNodeToWhereClause(a.right,b,c,e.BADNESS,d)+") "}throw Error("Not Supported Operator "+a.operator);case "null":return"null";case "bool":return!0===
a.value?"1":"0";case "string":return"'"+a.value.toString().replace(/'/g,"''")+"'";case "timestamp":return q.makeDateString(a.value,b);case "date":return q.makeDateString(a.value,b);case "number":return a.value.toString();case "current_time":return q.makeToday("date"===a.mode,b);case "column_ref":return c&&c.toLowerCase()===a.column.toLowerCase()?"("+f+")":a.column;case "function":return d=this.evaluateNodeToWhereClause(a.args,b,c,e.BADNESS,d),q.translateFunctionToDatabaseSpecific(a.name,d,b)}throw Error("Unsupported sql syntax "+
a.type);};e.prototype.extractValue=function(a){return this.codefield instanceof y.WhereClause?this.reverseLkp[this.codefield.calculateValueCompiled(a)]:this.reverseLkp[a.attributes[this.codefield]]};e.BADNESS="_!!!_BAD_LKP_!!!!";return e}(r);t.StringToCodeAdapted=E;r=function(n){function e(a,b){var c=n.call(this)||this;c.field=a;c.sql=b;return c}u(e,n);e.prototype.rewriteSql=function(a,b){return{rewritten:!0,where:q.reformulateWithoutField(a,this.field.name,q.toWhereClause(this.sql,v.FeatureServiceDatabaseType.Standardised),
b.getFieldsIndex())}};e.prototype.extractValue=function(a){return this.sql.calculateValueCompiled(a)};return e}(r);t.SqlExpressionAdapted=r;x=function(n){function e(a){var b=n.call(this,a)||this;b._calcFunc=null;b.declaredClass="esri.arcade.featureset.actions.Adapted";b.adaptedFields=null;b._extraFilter=null;b._extraFilter=a.extraFilter;b._parent=a.parentfeatureset;b._maxProcessing=30;b.adaptedFields=a.adaptedFields;return b}u(e,n);e.findField=function(a,b){for(var c=0;c<a.length;c++){var f=a[c];
if(f.name.toLowerCase()===b.toString().toLowerCase())return f}return null};e.prototype._initialiseFeatureSet=function(){null!==this._parent?(this.geometryType=this._parent.geometryType,this.objectIdField=this._parent.objectIdField,this.spatialReference=this._parent.spatialReference,this.hasM=this._parent.hasM,this.hasZ=this._parent.hasZ,this.typeIdField=this._parent.typeIdField,this.types=this._parent.types):(this.spatialReference=new D({wkid:4326}),this.objectIdField="",this.geometryType=v.layerGeometryEsriConstants.point,
this.typeIdField="",this.types=null);this.fields=[];for(var a=0,b=this.adaptedFields;a<b.length;a++){var c=b[a];c.postInitialization(this,this._parent);this.fields.push(c.field)}};e.prototype._getSet=function(a){var b=this;return null===this._wset?this._ensureLoaded().then(function(){return b._extraFilter?b._getFilteredSet("",null,null,null,a):b._parent._getSet(a)}).then(function(c){b._checkCancelled(a);b._wset=new w(c._candidates.slice(0),c._known.slice(0),c._ordered,b._clonePageDefinition(c.pagesDefinition));
return b._wset}):p.resolve(this._wset)};e.prototype._isInFeatureSet=function(a){return this._parent._isInFeatureSet(a)};e.prototype._getFeatures=function(a,b,c,f){var d=this,e=[];-1!==b&&void 0===this._featureCache[b]&&e.push(b);var k=this._maxQueryRate();if(!0===this._checkIfNeedToExpandKnownPage(a,k))return this._expandPagedSet(a,k,0,0,f).then(function(){return d._getFeatures(a,b,c,f)});for(var m=0,g=a._lastFetchedIndex;g<a._known.length&&!(m++,m<=c&&(a._lastFetchedIndex+=1),void 0===this._featureCache[a._known[g]]&&
(a._known[g]!==b&&e.push(a._known[g]),e.length>=k-1));g++);if(0===e.length)return p.resolve("success");a=new w([],e,a._ordered,null);var l=Math.min(e.length,c);return this._parent._getFeatures(a,-1,l,f).then(function(){d._checkCancelled(f);for(var a=[],b=0;b<l;b++){var c=d._parent._featureFromCache(e[b]);void 0!==c&&a.push({geometry:c.geometry,attributes:c.attributes,id:e[b]})}for(b=0;b<a.length;b++){for(var c=a[b],h=[],g=0,k=d.adaptedFields;g<k.length;g++){var m=k[g];h[m.field.name]=m.extractValue(c)}d._featureCache[c.id]=
new B({attributes:h,geometry:C.cloneGeometry(c.geometry)})}return"success"})};e.prototype._fetchAndRefineFeatures=function(){return p.reject(Error("Fetch and Refine should not be called in this featureset"))};e.prototype._getFilteredSet=function(a,b,c,f,d){var e=this,k=!1,m=this.reformulateWithoutAdaptions(c),k=m.cannot;c=m.where;var g=!1;if(null!==f){for(var g=!0,m=[],l=0,n=this.adaptedFields;l<n.length;l++){var p=n[l];if(!(p instanceof z)&&!0===f.scanForField(p.field.name))if(p instanceof A)m.push({field:p.field.name,
newfield:p.originalField.name});else{f=null;g=!1;break}}f&&0<m.length&&(f=f.replaceFields(m))}null!==c?null!==this._extraFilter&&(c=q.combine(this._extraFilter,c)):c=this._extraFilter;return this._ensureLoaded().then(function(){return e._parent._getFilteredSet(a,b,c,f,d)}).then(function(a){e._checkCancelled(d);return!0===k?new w(a._candidates.slice(0).concat(a._known.slice(0)),[],!0===g?a._ordered:!1,e._clonePageDefinition(a.pagesDefinition)):new w(a._candidates.slice(0),a._known.slice(0),!0===g?
a._ordered:!1,e._clonePageDefinition(a.pagesDefinition))})};e.prototype.reformulateWithoutAdaptions=function(a){var b={cannot:!1,where:a};if(null!==a)for(var c=0,f=this.adaptedFields;c<f.length;c++){var d=f[c];if(!0===q.scanForField(a,d.field.name))if(d=d.rewriteSql(a,this),!0===d.rewritten)b.where=d.where;else{b.cannot=!0;b.where=null;break}}return b};e.prototype._stat=function(a,b,c,f,d,e,k){var h=this,g=!1,l=this.reformulateWithoutAdaptions(b),g=l.cannot;b=l.where;l=this.reformulateWithoutAdaptions(d);
g=g||l.cannot;d=l.where;null!==d?null!==this._extraFilter&&(d=q.combine(this._extraFilter,d)):d=this._extraFilter;return!0===g?null===d&&""===c&&null===f?this._manualStat(a,b,e,k):p.resolve({calculated:!1}):this._parent._stat(a,b,c,f,d,e,k).then(function(g){return!1===g.calculated?null===d&&""===c&&null===f?h._manualStat(a,b,e,k):{calculated:!1}:g})};e.prototype._canDoAggregates=function(a,b,c,f,d){if(null===this._parent)return p.resolve(!1);for(var e=0;e<a.length;e++)for(var k=0,m=this.adaptedFields;k<
m.length;k++){var g=m[k];if(a[e].toLowerCase()===g.field.name.toLowerCase()&&!(g instanceof z))return p.resolve(!1)}k=[];for(e=0;e<b.length;e++){g=b[e];if(null!==g.workingexpr){m=this.reformulateWithoutAdaptions(g.workingexpr);if(m.cannot)return p.resolve(!1);g=g.clone();g.workingexpr=m.where}k.push(g)}b=this.reformulateWithoutAdaptions(d);if(b.cannot)return p.resolve(!1);d=b.where;null!==d?null!==this._extraFilter&&(d=q.combine(this._extraFilter,d)):d=this._extraFilter;return this._parent._canDoAggregates(a,
k,c,f,d)};e.prototype._getAggregatePagesDataSourceDefinition=function(a,b,c,f,d,e,k){if(null===this._parent)return p.reject(Error("Should never be called"));for(var h=[],g=0;g<b.length;g++){var l=b[g];if(null!==l.workingexpr){var n=this.reformulateWithoutAdaptions(l.workingexpr);if(n.cannot)return p.reject(Error("Should never be called"));l=l.clone();l.workingexpr=n.where}h.push(l)}b=this.reformulateWithoutAdaptions(d);if(b.cannot)return p.reject(Error("Should never be called"));d=b.where;null!==
d?null!==this._extraFilter&&(d=q.combine(this._extraFilter,d)):d=this._extraFilter;return this._parent._getAggregatePagesDataSourceDefinition(a,h,c,f,d,e,k)};return e}(x);t.AdaptedFeatureSet=x});