/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../../core/Error.js";import{WhereClauseCache as i}from"../../../core/sql/WhereClauseCache.js";import{kebabDict as r}from"../../support/fieldType.js";const t=new i(50,500),n="unsupported-query",s=" as ",o=new Set(["esriFieldTypeOID","esriFieldTypeSmallInteger","esriFieldTypeBigInteger","esriFieldTypeInteger","esriFieldTypeSingle","esriFieldTypeDouble","esriFieldTypeLong"]),a=new Set(["esriFieldTypeDate","esriFieldTypeDateOnly","esriFieldTypeTimeOnly","esriFieldTypeTimestampOffset"]),l=new Set(["esriFieldTypeString","esriFieldTypeGUID","esriFieldTypeGlobalID",...o,...a]);function d(i,r,s={}){const o=c(r,i);if(!o){const s=t.getError(r,i);throw new e(n,"invalid SQL expression",{expression:r,error:s})}const a=s.expressionName||"expression";if(s.validateStandardized&&!o.isStandardized)throw new e(n,`${a} is not standard`,{expression:r});if(s.validateAggregate&&!o.isAggregate)throw new e(n,`${a} does not contain a valid aggregate function`,{expression:r});return o.fieldNames}function p(e,i,r,t){if(!r)return!0;const n="where clause";return g(e,i,d(e,r,{validateStandardized:!0,expressionName:n}),{expressionName:n,query:t}),!0}function f(i,r,t,s,o){if(!t)return!0;const a="having clause",l=d(i,t,{validateAggregate:!0,expressionName:a});g(i,r,l,{expressionName:a,query:o});const p=c(t,i),f=p?.getExpressions().every(e=>{const{aggregateType:r,field:t}=e,n=i.get(t)?.name;return s.some(e=>{const{onStatisticField:t,statisticType:s}=e,o=i.get(t)?.name;return o===n&&s.toLowerCase().trim()===r})});if(!f)throw new e(n,"expressions in having clause should also exist in outStatistics",{having:t});return!0}function c(e,i){return e?t.get(e,i):null}function u(e){return/\((.*?)\)/.test(e)?e:e.split(s)[0]}function y(e){return e.split(s)[1]}function m(e,i,r){if(!F(i))return[];const t=new Set,n=i=>{if(i&&"*"!==i)for(const r of w(e,i))t.add(r)};for(const s of i)n(s.onStatisticField);for(const s of r??[])n(s);return Array.from(t)}function g(i,r,t,s={}){const o=new Map;if(T(o,i,r,s.allowedFieldTypes??l,t),o.size){const i=s.expressionName??"expression";throw new e(n,`${i} contains invalid or missing fields`,{errors:Array.from(o.values()),query:s.query})}}function T(e,i,r,t,n){const s=n.includes("*")?[...r,...n.filter(e=>"*"!==e)]:n;for(const a of s){if(i.get(a))x(e,i,r,t,a);else try{for(const n of w(i,a))x(e,i,r,t,n)}catch(o){e.set(a,{type:"expression-error",expression:a,error:o})}}}function w(e,i){const r=e.get(i);if(r)return[r.name];return d(e,u(i),{validateStandardized:!0})}function x(e,i,t,n,s){const o=i.get(s);o?t.has(o.name)?"all"!==n&&!1===n?.has(o.type)&&e.set(s,{type:"invalid-type",fieldName:o.name,fieldType:r.fromJSON(o.type),allowedFieldTypes:Array.from(n,e=>r.fromJSON(e))}):e.set(s,{type:"missing-field",fieldName:o.name}):e.set(s,{type:"invalid-field",fieldName:s})}function F(e){return null!=e&&e.every(e=>"exceedslimit"!==e.statisticType)}export{a as allDateAndTimeFieldTypes,y as getAliasFromFieldName,u as getExpressionFromFieldName,m as getFieldNamesFromStatistics,c as getWhereClause,F as isFieldStatisticDefinition,o as numericFieldTypes,g as validateFields,f as validateHaving,p as validateWhere};
