/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{createRendererExpression as e}from"../../../../../support/ArcadeExpression.js";import{callExpressionWithCursor as t}from"../../../arcade/callExpressionWithCursor.js";import{FeatureMatcher as r}from"./Matcher.js";import{getCIMSymbol as o,getCIMSymbols as n}from"../../../layers/support/annodime/cimAnnotations.js";import{parseElement as i,pick as s}from"../../../layers/support/annodime/utils.js";import{symbolFields as m}from"../../../layers/support/annodime/virtualFields.js";class a extends r{constructor(e,t,r){super(),this._context=e,this._schema=t,this._expression=r,this._meshWriters=new Map}static async fromAnnotationSchema(t,r){const{spatialReference:o}=t.viewParams.tileInfo,n=await e("$feature.ELEMENT",o);return new a(t,r,n)}hasArcadeDependency(){return!0}doMatch(e){const t=this._getGroupCreator(e),{groupKey:r}=t;return this._meshWriters.get(r)}async fetchResources(e,t){const r=t.getCursor();for(;r.next();){const t=this._getGroupCreator(r),{groupKey:o}=t;let n=this._meshWriters.get(o);n||(n=await t.createGroup(this._context,this._schema,e)),this._meshWriters.set(o,n)}}_getGroupCreator(e){const r=t(this._expression,e,{}),a=i(r);return{get groupKey(){return"Annotation:"+JSON.stringify(s(a,"horizontalAlignment","verticalAlignment","fontFamilyName","fontStyleName","size","underline","bloated","symbolID","flipAngle"))},async createGroup(e,t,r){const{symbolID:i}=a;let s=!1;for(const o of m)s=s||null!=o.getValue(a);const c=l(a);if(null==i||s){let s=null;if(null==i){const{graphic:e}=a;if(null!=e){const t=o(e.symbol,c);s={type:"CIMSymbolReference",primitiveOverrides:[...t.data.primitiveOverrides||[],...e.symbol.primitiveOverrides||[]],symbol:t.data.symbol}}}else{const{symbolCollection:e}=t,r=n(e)[i][c];s=r?.data||null}if(s){for(const e of m)e.addToSymbol(s,a);const t=await r.fetchMeshResourceImmediate({type:"mesh-request",symbolReference:s});return p(e,t?.meshes||[])}return[]}return p(e,t.meshes[i][c])}}}}function p(e,t){const r=t.map(e=>({...e}));return e.createMeshWriters(r)}function l(e){if(e.geometry){if("custom-two-point"===e.geometry.type)return"line";if("custom-multipoint"===e.geometry.type)return"multipoint";if("geometry-bag"===e.geometry.type){if("custom-multipoint"===e.geometry.geometries[0].type)return"multipoint";throw new Error("Unable to determine geometry type from annotation graphic")}if("custom-point"===e.geometry.type)return"point";if("polyline"===e.geometry.type)return"line";if("point"===e.geometry.type)return"point";throw new Error("Unable to determine geometry type from annotation graphic")}if(e.graphic){if("polyline"===e.graphic.geometry.type)return"line";if("point"===e.graphic.geometry.type)return"point";if("multipoint"===e.graphic.geometry.type)return"multipoint";throw new Error("Unable to determine geometry type from annotation graphic")}throw new Error("Unable to determine geometry type from annotation graphic")}export{a as AnnotationMatcher};
