/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../../Color","../GeometryUtils","../expression/expression","../expression/types"],(function(t,e,i,r){"use strict";return function(){function n(t,e){let n;switch(this.isDataDriven=!1,this.interpolator=null,t.type){case"number":case"color":n=!0;break;case"array":n="number"===t.value;break;default:n=!1}if(null==e&&(e=t.default),Array.isArray(e)&&e.length>0&&i.ops[e[0]]){const n={number:r.NumberType,color:r.ColorType,string:r.StringType,boolean:r.BooleanType,enum:r.StringType};try{const l="array"===t.type?r.arrayType(n[t.value]||r.ValueType,t.length):n[t.type],a=i.createExpression(e,null,l);this.getValue=this._buildExpression(a,t),this.isDataDriven=!0,a instanceof i.Interpolate&&a.input instanceof i.Zoom&&(this.interpolator=a)}catch(a){console.log(a.message),this.getValue=this._buildSimple(t.default)}return}n&&"interval"===e.type&&(n=!1);const l=e&&e.stops&&e.stops.length>0;if(l)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)),l)switch(e.type){case"identity":this.getValue=this._buildIdentity(e,t);break;case"categorical":this.getValue=this._buildCategorical(e,t);break;default:this.getValue=n?this._buildInterpolate(e,t):this._buildInterval(e,t)}else this.getValue=this._buildIdentity(e,t);else l?this.getValue=n?this._buildZoomInterpolate(e):this._buildZoomInterval(e):(e=this._validate(e,t),this.getValue=this._buildSimple(e))}var l=n.prototype;return l._validate=function(t,e){if("number"===e.type){if(t<e.minimum)return e.minimum;if(t>e.maximum)return e.maximum}else"color"===e.type?t=n._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=r.valueToString(t));return t},l._buildSimple=function(t){return()=>t},l._buildExpression=function(t,e){return(i,r)=>{try{const n=t.evaluate(r,i);return void 0===n?e.default:this._validate(n,e)}catch(n){return console.log(n.message),e.default}}},l._buildIdentity=function(t,e){return(i,r)=>{let n;return r&&(n=r.values[t.property]),void 0!==n&&(n=this._validate(n,e)),null!=n?n:void 0!==t.default?t.default:e.default}},l._buildCategorical=function(t,e){return(i,r)=>{let n;return r&&(n=r.values[t.property]),n=this._categorical(n,t.stops),void 0!==n?n:void 0!==t.default?t.default:e.default}},l._buildInterval=function(t,e){return(i,r)=>{let n;return r&&(n=r.values[t.property]),"number"==typeof n?this._interval(n,t.stops):void 0!==t.default?t.default:e.default}},l._buildInterpolate=function(t,e){return(i,r)=>{let n;return r&&(n=r.values[t.property]),"number"==typeof n?this._interpolate(n,t.stops,t.base||1):void 0!==t.default?t.default:e.default}},l._buildZoomInterpolate=function(t){return e=>this._interpolate(e,t.stops,t.base||1)},l._buildZoomInterval=function(t){return e=>this._interval(e,t.stops)},l._categorical=function(t,e){const i=e.length;for(let r=0;r<i;r++)if(e[r][0]===t)return e[r][1]},l._interval=function(t,e){const i=e.length;let r=0;for(let n=0;n<i&&e[n][0]<=t;n++)r=n;return e[r][1]},l._interpolate=function(t,i,r){let n,l;const a=i.length;for(let e=0;e<a;e++){const r=i[e];if(!(r[0]<=t)){l=r;break}n=r}if(n&&l){const i=l[0]-n[0],a=t-n[0],o=1===r?a/i:(r**a-1)/(r**i-1);if(Array.isArray(n[1])){const t=n[1],i=l[1],r=[];for(let n=0;n<t.length;n++)r.push(e.interpolate(t[n],i[n],o));return r}return e.interpolate(n[1],l[1],o)}return n?n[1]:l?l[1]:void 0},n._isEmpty=function(t){for(const e in t)if(t.hasOwnProperty(e))return!1;return!0},n._parseColor=function(e){return Array.isArray(e)?e:("string"==typeof e&&(e=new t(e)),e instanceof t&&!this._isEmpty(e)?t.toUnitRGBA(e):void 0)},n}()}));
