/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
const e=new Set(["+","-","*","/","(",")"]);function t(t,r){(t=t.replaceAll(" ","")).startsWith("-")&&(t="0"+t),t.startsWith("+")&&(t=t.slice(1));const l=t.split(""),o=[],s=[];let a="";for(let f=0;f<l.length;f++){const t=l[f];e.has(t)?(a.length&&s.push(n(a,r)),o.push(t),a=""):a=a.concat(t)}return a.length&&s.push(n(a,r)),{ops:o,nums:s}}function n(e,t){return e.toLowerCase().startsWith("b")?t[parseInt(e.slice(1),10)-1]:parseFloat(e)}function r(e,t,n,r){if("number"==typeof n&&"number"==typeof r)return n+r;let l,o,s;"number"==typeof n?(s=r,l=s.length,o=new Float32Array(l),o.fill(n)):(l=n.length,o=n,r.constructor===Number?(s=new Float32Array(l),s.fill(r)):s=r);const a=new Float32Array(l);switch(t){case"+":for(let t=0;t<l;t++)(null==e||e[t])&&(a[t]=o[t]+s[t]);break;case"-":for(let t=0;t<l;t++)(null==e||e[t])&&(a[t]=o[t]-s[t]);break;case"*":for(let t=0;t<l;t++)(null==e||e[t])&&(a[t]=o[t]*s[t]);break;case"/":for(let t=0;t<l;t++)(null==e||e[t])&&s[t]&&(a[t]=o[t]/s[t]);break;case"(":case")":throw new Error("encountered error with custom band index equation")}return a}function l(e,t){e.splice(t,1);let n=0,r=0;do{n=0,r=0;for(let t=0;t<e.length;t++)if("("===e[t])n=t;else if(")"===e[t]){r=t;break}r===n+1&&e.splice(n,2)}while(r===n+1);return e}function o(e){if(1===e.length)return{opIndex:0,numIndex:0};let t=0,n=0;for(let s=0;s<e.length;s++)if("("===e[s])t=s;else if(")"===e[s]){n=s;break}const r=0===n?e:e.slice(t+1,n);let l=-1;for(let s=0;s<r.length;s++)if("*"===r[s]||"/"===r[s]){l=s;break}if(l>-1)n>0&&(l+=t+1);else{for(let e=0;e<r.length;e++)if("+"===r[e]||"-"===r[e]){l=e;break}n>0&&(l+=t+1)}let o=0;for(let s=0;s<l;s++)"("===e[s]&&o++;return{opIndex:l,numIndex:l-o}}function s(e,n,s){let a,{ops:f,nums:i}=t(s,n);if(0===f.length){const e=1===i.length?i[0]:n[0];if(e instanceof Float32Array)return[e];const t=new Float32Array(n[0].length);return"number"==typeof e?t.fill(e):t.set(e),[t]}for(;f.length>0;){const{numIndex:t,opIndex:n}=o(f);if(a=r(e,f[n],i[t],i[t+1]),1===f.length)break;f=l(f,n),i.splice(t,2,a)}return[a]}export{s as calculateCustomBandIndex};
