/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.24/esri/copyright.txt for details. */ import{_ as e}from"../chunks/tslib.es6.js";import{ensureType as t}from"../symbols.js";import i from"../core/Error.js";import{clone as r}from"../core/lang.js";import s from"../core/Logger.js";import{isNone as l,unwrapOr as o,unwrap as n}from"../core/maybe.js";import{deepMerge as a}from"../core/object.js";import{property as u}from"../core/accessorSupport/decorators/property.js";import{cast as p}from"../core/accessorSupport/decorators/cast.js";import{enumeration as f}from"../core/accessorSupport/decorators/enumeration.js";import{reader as d}from"../core/accessorSupport/decorators/reader.js";import{subclass as c}from"../core/accessorSupport/decorators/subclass.js";import{writer as y}from"../core/accessorSupport/decorators/writer.js";import{diff as m}from"../core/accessorSupport/diffUtils.js";import{ensureType as h,ensureString as b}from"../core/accessorSupport/ensureType.js";import{collectArcadeFieldNames as g,collectField as v}from"../layers/support/fieldUtils.js";import S from"../portal/Portal.js";import V from"./Renderer.js";import{VisualVariablesMixin as q}from"./mixins/VisualVariablesMixin.js";import{rendererBackgroundFillSymbolProperty as I,rendererSymbolProperty as j}from"./support/commonProperties.js";import{LegendOptions as _}from"./support/LegendOptions.js";import w from"./support/UniqueValueInfo.js";import{loadArcade as U}from"../support/arcadeOnDemand.js";import{f as O,t as F}from"../chunks/persistableUrlUtils.js";import{fetchStyle as x}from"../symbols/support/styleUtils.js";import D from"../symbols/WebStyleSymbol.js";var E;const M=s.getLogger("esri.renderers.UniqueValueRenderer"),N=h(w);let R=E=class extends(q(V)){constructor(e){super(e),this._valueInfoMap={},this._isDefaultSymbolDerived=!1,this.type="unique-value",this.backgroundFillSymbol=null,this.field=null,this.field2=null,this.field3=null,this.valueExpression=null,this.valueExpressionTitle=null,this.legendOptions=null,this.defaultLabel=null,this.fieldDelimiter=null,this.portal=null,this.styleOrigin=null,this.diff={uniqueValueInfos(e,t){if(!e&&!t)return;if(!e||!t)return{type:"complete",oldValue:e,newValue:t};let i=!1;const r={type:"collection",added:[],removed:[],changed:[],unchanged:[]};for(let s=0;se.value===t[s].value));l?m(l,t[s])?(r.changed.push({type:"complete",oldValue:l,newValue:t[s]}),i=!0):r.unchanged.push({oldValue:l,newValue:t[s]}):(r.added.push(t[s]),i=!0)}for(let s=0;st.value===e[s].value))||(r.removed.push(e[s]),i=!0)}return i?r:void 0}},this._set("uniqueValueInfos",[])}get _cache(){return{compiledFunc:null}}castField(e){return null==e||"function"==typeof e?e:b(e)}writeField(e,t,r,s){"string"==typeof e?t[r]=e:s&&s.messages?s.messages.push(new i("property:unsupported","UniqueValueRenderer.field set to a function cannot be written to JSON")):M.error(".field: cannot write field to JSON since it's not a string value")}set defaultSymbol(e){this._isDefaultSymbolDerived=!1,this._set("defaultSymbol",e)}readPortal(e,t,i){return i.portal||S.getDefault()}readStyleOrigin(e,t,i){if(t.styleName)return Object.freeze({styleName:t.styleName});if(t.styleUrl){const e=O(t.styleUrl,i);return Object.freeze({styleUrl:e})}}writeStyleOrigin(e,t,i,r){e.styleName?t.styleName=e.styleName:e.styleUrl&&(t.styleUrl=F(e.styleUrl,r))}set uniqueValueInfos(e){this.styleOrigin?M.error("#uniqueValueInfos=","Cannot modify unique value infos of a UniqueValueRenderer created from a web style"):(this._set("uniqueValueInfos",e),this._updateValueInfoMap())}addUniqueValueInfo(e,i){if(this.styleOrigin)return void M.error("#addUniqueValueInfo()","Cannot modify unique value infos of a UniqueValueRenderer created from a web style");let r;r="object"==typeof e?N(e):new w({value:e,symbol:t(i)}),this.uniqueValueInfos.push(r),this._valueInfoMap[r.value]=r}removeUniqueValueInfo(e){if(this.styleOrigin)M.error("#removeUniqueValueInfo()","Cannot modify unique value infos of a UniqueValueRenderer created from a web style");else for(let t=0;te+t.getAttributeHash()),"")}getMeshHash(){return`${JSON.stringify(this.backgroundFillSymbol)}.${JSON.stringify(this.defaultSymbol)}.${this.uniqueValueInfos.reduce(((e,t)=>e+t.getMeshHash()),"")}.${`${this.field}.${this.field2}.${this.field3}.${this.fieldDelimiter}`}.${this.valueExpression}`}clone(){const e=new E({field:this.field,field2:this.field2,field3:this.field3,defaultLabel:this.defaultLabel,defaultSymbol:r(this.defaultSymbol),valueExpression:this.valueExpression,valueExpressionTitle:this.valueExpressionTitle,fieldDelimiter:this.fieldDelimiter,visualVariables:r(this.visualVariables),legendOptions:r(this.legendOptions),authoringInfo:this.authoringInfo&&this.authoringInfo.clone(),backgroundFillSymbol:r(this.backgroundFillSymbol)});this._isDefaultSymbolDerived&&(e._isDefaultSymbolDerived=!0),e._set("portal",this.portal);const t=r(this.uniqueValueInfos);return this.styleOrigin&&(e._set("styleOrigin",Object.freeze(r(this.styleOrigin))),Object.freeze(t)),e._set("uniqueValueInfos",t),e._updateValueInfoMap(),e}get arcadeRequired(){return this.arcadeRequiredForVisualVariables||!!this.valueExpression}async collectRequiredFields(e,t){const i=[this.collectVVRequiredFields(e,t),this.collectSymbolFields(e,t)];await Promise.all(i)}async collectSymbolFields(e,t){const i=[...this.getSymbols().map((i=>i.collectRequiredFields(e,t))),g(e,t,this.valueExpression)];v(e,t,this.field),v(e,t,this.field2),v(e,t,this.field3),await Promise.all(i)}populateFromStyle(){return x(this.styleOrigin,{portal:this.portal}).then((e=>{const t=[];return this._valueInfoMap={},e&&e.data&&Array.isArray(e.data.items)&&e.data.items.forEach((i=>{const r=new D({styleUrl:e.styleUrl,styleName:e.styleName,portal:this.portal,name:i.name});this.defaultSymbol||i.name!==e.data.defaultItem||(this.defaultSymbol=r,this._isDefaultSymbolDerived=!0);const s=new w({value:i.name,symbol:r});t.push(s),this._valueInfoMap[i.name]=s})),this._set("uniqueValueInfos",Object.freeze(t)),!this.defaultSymbol&&this.uniqueValueInfos.length&&(this.defaultSymbol=this.uniqueValueInfos[0].symbol,this._isDefaultSymbolDerived=!0),this}))}_updateValueInfoMap(){this._valueInfoMap={},this.uniqueValueInfos.forEach((e=>this._valueInfoMap[e.value+""]=e))}_getUniqueValueInfo(e,t){return this.valueExpression?this._getUnqiueValueInfoForExpression(e,t):this._getUnqiueValueInfoForFields(e)}_getUnqiueValueInfoForExpression(e,t){const{viewingMode:i,scale:r,spatialReference:s,arcade:l}=o(t,{});let a=this._cache.compiledFunc;const u=n(l).arcadeUtils;if(!a){const e=u.createSyntaxTree(this.valueExpression);a=u.createFunction(e),this._cache.compiledFunc=a}const p=u.executeFunction(a,u.createExecContext(e,u.getViewInfo({viewingMode:i,scale:r,spatialReference:s})));return this._valueInfoMap[p+""]}_getUnqiueValueInfoForFields(e){const t=this.field,i=e.attributes;let r;if("function"!=typeof t&&this.field2){const e=this.field2,s=this.field3,l=[];t&&l.push(i[t]),e&&l.push(i[e]),s&&l.push(i[s]),r=l.join(this.fieldDelimiter||"")}else"function"==typeof t?r=t(e):t&&(r=i[t]);return this._valueInfoMap[r+""]}static fromPortalStyle(e,t){const i=new E(t&&t.properties);i._set("styleOrigin",Object.freeze({styleName:e})),i._set("portal",t&&t.portal||S.getDefault());const r=i.populateFromStyle();return r.catch((t=>{M.error(`#fromPortalStyle('${e}'[, ...])`,"Failed to create unique value renderer from style name",t)})),r}static fromStyleUrl(e,t){const i=new E(t&&t.properties);i._set("styleOrigin",Object.freeze({styleUrl:e}));const r=i.populateFromStyle();return r.catch((t=>{M.error(`#fromStyleUrl('${e}'[, ...])`,"Failed to create unique value renderer from style URL",t)})),r}};e([u({readOnly:!0})],R.prototype,"_cache",null),e([f({uniqueValue:"unique-value"})],R.prototype,"type",void 0),e([u(I)],R.prototype,"backgroundFillSymbol",void 0),e([u({json:{type:String,read:{source:"field1"},write:{target:"field1"}}})],R.prototype,"field",void 0),e([p("field")],R.prototype,"castField",null),e([y("field")],R.prototype,"writeField",null),e([u({type:String,json:{write:!0}})],R.prototype,"field2",void 0),e([u({type:String,json:{write:!0}})],R.prototype,"field3",void 0),e([u({type:String,json:{write:!0}})],R.prototype,"valueExpression",void 0),e([u({type:String,json:{write:!0}})],R.prototype,"valueExpressionTitle",void 0),e([u({type:_,json:{write:!0}})],R.prototype,"legendOptions",void 0),e([u({type:String,json:{write:!0}})],R.prototype,"defaultLabel",void 0),e([u(a({...j},{json:{write:{overridePolicy(){return{enabled:!this._isDefaultSymbolDerived}}},origins:{"web-scene":{write:{overridePolicy(){return{enabled:!this._isDefaultSymbolDerived}}}}}}}))],R.prototype,"defaultSymbol",null),e([u({type:String,json:{write:!0}})],R.prototype,"fieldDelimiter",void 0),e([u({type:S,readOnly:!0})],R.prototype,"portal",void 0),e([d("portal",["styleName"])],R.prototype,"readPortal",null),e([u({readOnly:!0,json:{write:{enabled:!1,overridePolicy:()=>({enabled:!0})}}})],R.prototype,"styleOrigin",void 0),e([d("styleOrigin",["styleName","styleUrl"])],R.prototype,"readStyleOrigin",null),e([y("styleOrigin",{styleName:{type:String},styleUrl:{type:String}})],R.prototype,"writeStyleOrigin",null),e([u({type:[w],json:{write:{overridePolicy(){return this.styleOrigin?{enabled:!1}:{enabled:!0}}}}})],R.prototype,"uniqueValueInfos",null),R=E=e([c("esri.renderers.UniqueValueRenderer")],R);const P=R;export{P as default};