/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__decorate as t}from"tslib";import{property as n,subclass as e}from"../../../core/accessorSupport/decorators.js";import{getBandMatrix3 as s}from"./bandIndexUtils.js";import r from"./BaseRasterFunction.js";import i from"./ExtractBandFunctionArguments.js";let a=class extends r{constructor(){super(...arguments),this.functionName="ExtractBand",this.functionArguments=null,this.rasterArgumentNames=["raster"]}_bindSourceRasters(){const{functionArguments:t,sourceRasterInfos:n}=this,e=n[0],{method:s,bandNames:r,bandWavelengths:i,bandIds:a,missingBandAction:o}=t,h=r?.length&&("name"===s||"id"!==s&&!a?.length),c=i?.length&&("wavelength"===s||"id"!==s&&!a?.length),m=1===o,g=h?l(e,r):c?d(e,i,this.functionArguments,m):u(e,a,m);if(null==g){return{success:!1,supportsGPU:!1,error:`extract-band-function: Invalid ${h?"band names":c?"band wavelengths":"band ids"} for the imagery data source`}}this.functionArguments.bandIds=g,this.functionArguments.method="id",this.outputPixelType=this._getOutputPixelType(e.pixelType);const p=e.clone();p.pixelType=this.outputPixelType,p.bandCount=g.length;const{statistics:f,histograms:I}=p;null!=f&&f.length&&(p.statistics=g.map(t=>f[t]||f[f.length-1])),null!=I&&I.length&&(p.histograms=g.map(t=>I[t]||I[I.length-1])),p.multidimensionalInfo&&p.multidimensionalInfo.variables.forEach(t=>{const{statistics:n,histograms:e}=t;null!=n&&n.length&&(t.statistics=g.map(t=>n[t]||n[n.length-1])),null!=e&&e.length&&(t.histograms=g.map(t=>e[t]||e[e.length-1]))});let x=p.keyProperties?.BandProperties;x?.length&&(x=g.map(t=>t>=x.length?x[x.length-1]:x[t]),p.keyProperties={...p.keyProperties,BandProperties:x}),this.rasterInfo=p;return{success:!0,supportsGPU:p.bandCount<=3}}_processPixels(t){const n=t.pixelBlocks?.[0];if(null==n)return null;let{bandIds:e}=this.functionArguments;if(this.rasterInfo.storageInfo.isBsqTile){const{rawInputBandIds:t}=this;e=e.map(n=>t.indexOf(n))}else{const t=n.pixels.length;e=e.map(n=>n>=t?t-1:n)}return n.extractBands(e)}_getWebGLParameters(){let t;if(this.isInputBandIdsSwizzled)t=this.swizzledBandSelection.length?this.swizzledBandSelection:[0,1,2];else{t=[...this.functionArguments.bandIds],0===t.length?t=[0,1,2]:t.length<3&&(t[1]=t[1]??t[0],t[2]=t[2]??t[1]);for(let n=0;n<3;n++)t[n]=Math.min(t[n],2)}return{bandIndexMat3:s(t)}}_getInputBandIds(t){const n=t.length;return this.functionArguments.bandIds.map(t=>t>=n?n-1:t).map(n=>t[n])}_swizzleBandIds(t){const n=this.functionArguments.bandIds.map(n=>t.indexOf(n));return this.isInputBandIdsSwizzled=!0,n[1]??=n[0],n[2]??=n[1],this.swizzledBandSelection=n,!1}};t([n({json:{write:!0,name:"rasterFunction"}})],a.prototype,"functionName",void 0),t([n({type:i,json:{write:!0,name:"rasterFunctionArguments"}})],a.prototype,"functionArguments",void 0),t([n()],a.prototype,"rasterArgumentNames",void 0),a=t([e("esri.layers.raster.functions.ExtractBandFunction")],a);const o=a;function l(t,n){const e=t.bandInfos.map(({name:t})=>t.toLowerCase()),s=[];for(let r=0;r<n.length;r++){const t=n[r].toLowerCase();let i=e.indexOf(t);if(-1===i&&"nearinfrared"===t&&(i=e.findIndex(t=>t.startsWith("nearinfrared_1")),-1===i&&(i=e.findIndex(t=>t.startsWith("nearinfrared")))),-1===i)return null;s.push(i)}return s}function u(t,n,e){const{bandCount:s}=t;return!n?.length||e&&n.some(t=>t<0||t>=s)?null:n}function d(t,n,{wavelengthMatchTolerance:e},s){const{bandInfos:r}=t,i=[];for(let o=0;o<r.length;o++){const{minWavelength:t,maxWavelength:n}=r[o];if(!t||!n)return null;i.push({minWavelength:t,maxWavelength:n})}const a=[];for(let o=0;o<n.length;o++){const t=n[o];let r=!1,l=-1,u=Number.MAX_VALUE;for(let n=0;n<i.length;n++){const e=i[n],s=t>=e.minWavelength&&t<=e.maxWavelength,a=Math.abs(t-(e.minWavelength+e.maxWavelength)/2);s?a<u&&(r=!0,l=n,u=a):!r&&a<u&&(l=n,u=a)}if(!r&&e&&u<e&&(r=!0),!r&&s)return null;a.push(l)}return a}export{o as default};
