import*as e from"@arcgis/core/geometry/operators/differenceOperator";import{isCodedValueDomain as t}from"../../../utilities/esri.js";import{NumberFormat as n}from"../../../utilities/format/formats.js";import{NumberFormatter as o,format as r}from"../../../utilities/format/number.js";import{getBounds as i,getScreenExtent as s}from"../../../utilities/geometry.js";import{average as c,standardDeviation as a}from"../../../utilities/math.js";import{expandExtentToScale as l,getNextScale as u,getScaleRange as m}from"../../../utilities/scaleRanges.js";import{caseInsensitiveEquals as p}from"../../../utilities/string.js";export const separatorRegex=/(\s|(?<=\w)-|-(?![\d,.]))+/;export async function initializeElasticlunrIndex(e){const t=(await import("elasticlunr")).default;t.tokenizer.setSeperator(e),t.Pipeline.getRegisteredFunction("gcx.trimmer")||t.Pipeline.registerFunction(f,"gcx.trimmer");const n=t();return n.pipeline.reset(),n.pipeline.add(f),n.pipeline.add(t.Pipeline.getRegisteredFunction("stopWordFilter")),n.saveDocument(!1),{index:n,tokenizer:t.tokenizer}}export function getSearchableFields(e){const{outFields:t}=e.featureSettings,n=!t.includes("*");return e.schema.fieldExtensions.initializedItems.filter((e=>e.searchable&&(!n||t.includes(e.field.name)))).map((e=>e.field)).toArray()}export function getWhereClause(e,r,i,s){const c=[],a=new o({locale:s.locale,defaultParseFormat:[n.CURRENCY,n.FIXED_POINT,n.PERCENT,n.NUMBER,n.ROUND_TRIP]});for(const o of i){let i=[];o.domain&&t(o.domain)&&(i=i.concat(o.domain.codedValues));for(const t of e.schema.types.toArray()){const{domains:e}=t;e?.[o.name]?.codedValues&&(i=i.concat(e[o.name].codedValues))}for(const t of r)if(e.schema.types.length>0&&p(o.name,e.schema.typeIdField)){const n=e.schema.types.findIndex((e=>p(e.name,t.toLowerCase())));if(n>-1){const t=e.schema.types.getItemAt(n).id;"string"==typeof t?c.push(`${e.schema.typeIdField} = '${x(t)}'`):c.push(`${e.schema.typeIdField} = ${t}`)}}else if(i.length>0){const e=i.findIndex((e=>e.name.toLowerCase().includes(t.toLowerCase())));e>-1&&("string"==typeof i[e].code?c.push(`${o.name} = '${x(i[e].code)}'`):c.push(`${o.name} = ${i[e].code}`))}else switch(o.type){case"string":c.push(`lower(${o.name}) like '%${x(t.toLowerCase())}%'`);break;case"integer":case"small-integer":case"big-integer":case"single":case"double":{const e=g(t),r=a.parse({format:[n.DEFAULT,n.FIXED_POINT,n.NUMBER]},e);if(isNaN(r))continue;const i=d(r);r<0?c.push(`(${o.name} > ${i.lower} and ${o.name} <= ${i.upper})`):c.push(`(${o.name} >= ${i.lower} and ${o.name} < ${i.upper})`);break}}}return c.join(" or ")}export function normalizeScores(e){const t=e.map((e=>e.score)),n=c(t),o=a(t);for(const t of e)o?(t.score=(t.score-n)/o,t.score=Math.max(-2,t.score),t.score=Math.min(2,t.score),t.score=t.score/4+.5,t.score*=100):t.score=100}export async function expandSearchArea(t,n,o,r){if(!n||!t||!o||t.contains(n))return{};const c=u(o,r),a=m("continent"),p=!c||null!=a&&c>=a?n:l(t,o,c);return{geometry:function(e){const t=e.filter((e=>!!e?.spatialReference));if(!t.length)return!0;const{spatialReference:n}=t[0],{isWebMercator:o,isWGS84:r}=n;if(!o&&!r)return!0;const c=s(t),a=i(n);if(null==a||null==c)return!0;const{xmax:l,xmin:u}=c,{max:m,min:p}=a;return l<=m&&u>=p}([p])?e.execute(p,t):p,scale:c}}function f(e){if(null==e)throw new Error("token should not be undefined");return e.replace(/^[^a-zA-Z0-9\u007F-\uFFFF-]+/,"").replace(/[^a-zA-Z0-9\u007F-\uFFFF-]+$/,"")}function d(e){let t,o=0;const i=r(n.ROUND_TRIP,e),s=h(i);i.includes(".")&&(o=i.length-i.indexOf(".")-1);let c=e;return 0===s&&o>0?t=Math.pow(10,-(o-1)):null!=s&&o>s?(c=Number(e.toFixed(o-s)),t=Math.pow(10,-o)):t=1,t/=2,{lower:(c-t).toFixed(o+1),upper:(c+t).toFixed(o+1)}}function g(e){let t=h(e);const n=r(-123456780.9),o=function(){const e=r(1234567890);return r(-123456780.9).split("").filter((t=>!e.includes(t))).join("")}();if(null!=t&&t>0){const i=new RegExp(`[${n}]`,"g"),s=e.match(i);if(null!=s){const e=s.length;let n=0;for(;t>0&&n<=e;){const i=e-n-1;o.includes(s[i])||(s[i]=r(0).toString(),t--),n++}return s.join("")}}return e}function h(e){const t=r(1234567890),n=new RegExp(`[${t}]`,"g"),o=e.match(n);return o?o.length-14:void 0}function x(e){return e.replace(/([%'])/g,"$1$1")}