/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import t from"../../../../../Color.js";import{parseToUnitRgba as e}from"../../../../../colorUtils.js";import{interpolate as r}from"../GeometryUtils.js";import{ops as i,createExpression as l,Interpolate as s,Zoom as a}from"../expression/expression.js";import{arrayType as o,stringType as n,booleanType as u,colorType as p,numberType as d,valueType as f,valueToString as m}from"../expression/types.js";class h{constructor(t,e){let r;switch(this.isDataDriven=!1,this.interpolator=null,t.type){case"number":case"color":r=!0;break;case"array":r="number"===t.value;break;default:r=!1}if((null==e||""===e&&"color"===t.type)&&(e=t.default),Array.isArray(e)&&e.length>0&&i[e[0]]){const r={number:d,color:p,string:n,boolean:u,enum:n};try{const i="array"===t.type?o(r[t.value]||f,t.length):r[t.type],n=l(e,null,i);this.getValue=this._buildExpression(n,t),this.isDataDriven=!0,n instanceof s&&n.input instanceof a&&(this.interpolator=n)}catch(h){console.log(h.message),this.getValue=this._buildSimple(t.default)}return}r&&"interval"===e.type&&(r=!1);const m=e?.stops&&e.stops.length>0;if(m)for(const i of e.stops)i[1]=this._validate(i[1],t);if(this.isDataDriven=!!e&&!!e.property,this.isDataDriven)if(void 0!==e.default&&(e.default=this._validate(e.default,t)),m)switch(e.type){case"identity":this.getValue=this._buildIdentity(e,t);break;case"categorical":this.getValue=this._buildCategorical(e,t);break;default:this.getValue=r?this._buildInterpolate(e,t):this._buildInterval(e,t)}else this.getValue=this._buildIdentity(e,t);else m?this.getValue=r?this._buildZoomInterpolate(e):this._buildZoomInterval(e):(e=this._validate(e,t),this.getValue=this._buildSimple(e))}_validate(t,e){if("number"===e.type){if(null!=e.minimum&&t<e.minimum)return e.minimum;if(null!=e.maximum&&t>e.maximum)return e.maximum}else"color"===e.type?t=h._parseColor(t):"enum"===e.type?"string"==typeof t&&(t=e.values.indexOf(t)):"array"===e.type&&"enum"===e.value?t=t.map(t=>"string"==typeof t?e.values.indexOf(t):t):"string"===e.type&&(t=m(t));return t}_buildSimple(t){return()=>t}_buildExpression(t,e){return(r,i)=>{try{const l=t.evaluate(i,r);return void 0===l?e.default:this._validate(l,e)}catch(l){return console.log(l.message),e.default}}}_buildIdentity(t,e){return(r,i)=>{let l;return i&&(l=i.values[t.property]),void 0!==l&&(l=this._validate(l,e)),null!=l?l:void 0!==t.default?t.default:e.default}}_buildCategorical(t,e){return(r,i)=>{let l;return i&&(l=i.values[t.property]),l=this._categorical(l,t.stops),void 0!==l?l:void 0!==t.default?t.default:e.default}}_buildInterval(t,e){return(r,i)=>{let l;return i&&(l=i.values[t.property]),"number"==typeof l?this._interval(l,t.stops):void 0!==t.default?t.default:e.default}}_buildInterpolate(t,e){return(r,i)=>{let l;return i&&(l=i.values[t.property]),"number"==typeof l?this._interpolate(l,t.stops,t.base||1):void 0!==t.default?t.default:e.default}}_buildZoomInterpolate(t){return e=>this._interpolate(e,t.stops,t.base||1)}_buildZoomInterval(t){return e=>this._interval(e,t.stops)}_categorical(t,e){const r=e.length;for(let i=0;i<r;i++)if(e[i][0]===t)return e[i][1]}_interval(t,e){const r=e.length;let i=0;for(let l=0;l<r&&e[l][0]<=t;l++)i=l;return e[i][1]}_interpolate(t,e,i){let l,s;const a=e.length;for(let r=0;r<a;r++){const i=e[r];if(!(i[0]<=t)){s=i;break}l=i}if(l&&s){const e=s[0]-l[0],a=t-l[0],o=1===i?a/e:(i**a-1)/(i**e-1);if(Array.isArray(l[1])){const t=l[1],e=s[1],i=[];for(let l=0;l<t.length;l++)i.push(r(t[l],e[l],o));return i}return r(l[1],s[1],o)}return l?l[1]:s?s[1]:void 0}static _isEmpty(t){for(const e in t)if(t.hasOwnProperty(e))return!1;return!0}static _parseColor(r){return Array.isArray(r)?r:"string"==typeof r?e(r)??void 0:r instanceof t&&!this._isEmpty(r)?r.toUnitRGBA():void 0}}export{h as default};
