/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../../core/Error.js";import{getDateDiffSQL as t,getDateType as n}from"../../support/utils.js";import{createLayerAdapter as r}from"../../support/adapters/support/layerUtils.js";const a=new Set(["date","number"]),i=["years","months","days","hours","minutes","seconds"];function s(t,r,i,s){const o=[];let m=null;return[r,i].every(e=>{const r=n(t,e);return r&&o.push(r),!!r})?o[0]===o[1]?"field"===o[0]?r===i&&(m=new e(s,"'startTime' and 'endTime' parameters cannot be identical")):m=new e(s,"Invalid combination of 'startTime' and 'endTime' parameters"):a.has(o[0])&&a.has(o[1])&&(m=new e(s,"Invalid combination of 'startTime' and 'endTime' parameters")):m=new e(s,"'startTime' and 'endTime' parameters must be one of these values: a date object, unix epoch time, name of a valid date field or <now>"),m}function o(e){const t=e.map(e=>`$feature["${e}"];`);return t.length?t.join("\n")+"\n":""}function m(e,t,n){let r;return r="number"===t?e:"date"===t?e.getTime():'$feature["'+e+'"]',"var "+n+" = "+r+";"}function u(e,t,r,a){const i=n(e,t),s=n(e,r),u=[m(t,i,"startTime"),m(r,s,"endTime"),`var retVal = null;\n\n    if (startTime != null && endTime != null) {\n      startTime = Date(startTime);\n      endTime = Date(endTime);\n      retVal = DateDiff(endTime, startTime, "${a}");\n    }\n\n    return retVal;`],d=[];return"field"===i&&d.push(t),"field"===s&&d.push(r),o(d)+u.join("\n")}function d(e){const{layer:n,startTime:a,endTime:i}=e,s=r(n),o=e.unit||"days",m=t(s,a,i,o);return{valueExpression:u(s,a,i,o),statisticsQuery:m,histogramQuery:m}}export{d as getAgeExpressions,i as supportedAgeUnits,s as verifyDates};
