/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../Graphic.js";import t from"../../core/Logger.js";import{getOrCreateMapValue as r}from"../../core/MapUtils.js";import s from"../../geometry/Extent.js";import o from"../../geometry/Mesh.js";import n from"../../geometry/SpatialReference.js";import{ServiceAssetPart as a,ServiceAsset as u}from"../../geometry/support/meshUtils/External.js";import{getFormatIdMimeType as i}from"../../layers/support/infoFor3D.js";import f from"./FeatureSet.js";import{extractMeshFeatureOrigin as m,extractMeshFeatureTransform as c}from"./meshFeatureAttributes.js";const p=()=>t.getLogger("esri.rest.support.meshFeatureSet");function l(t,r,s){const o=s.features;s.features=[],delete s.geometryType;const a=f.fromJSON(s);if(a.geometryType="mesh",!s.assetMaps)return a;const u=g(r,s.assetMaps),i=t.sourceSpatialReference??n.WGS84,m=s.globalIdFieldName,{outFields:c}=t,p=null!=c&&c.length>0?y(c.includes("*")?null:new Set(c)):()=>({});for(const n of o){const t=h(n,m,i,r,u);a.features.push(new e({geometry:t,attributes:p(n)}))}return a}function y(e){return({attributes:t})=>{if(!t)return{};if(!e)return t;for(const r in t)e.has(r)||delete t[r];return t}}function h(e,t,r,n,a){const u=e.attributes[t],i=a.get(u);if(null==i||!e.geometry)return null;const f=m(e.attributes,r,n.transformFieldRoles),p=s.fromJSON(e.geometry);p.spatialReference=r;const l=c(e.attributes,n.transformFieldRoles),y=r.isGeographic?"local":"georeferenced",h=d(i);return h?o.createWithExternalSource(f,h,{extent:p,transform:l,vertexSpace:y,unitConversionDisabled:!0}):o.createIncomplete(f,{extent:p,transform:l,vertexSpace:y})}function g(e,t){const s=new Map;for(const o of t){const t=o.parentGlobalId;if(null==t)continue;const n=o.assetName,a=o.assetType,u=o.assetHash,f=o.assetURL,m=o.conversionStatus,c=o.seqNo,l=i(a,e.supportedFormats);if(!l){p().error("mesh-feature-set:unknown-format",`Service returned an asset of type ${a}, but it does not list it as a supported type`);continue}const y=r(s,t,()=>({files:new Map}));r(y.files,n,()=>({name:n,type:a,mimeType:l,status:S(m),parts:[]})).parts[c]={hash:u,url:f}}return s}function d(e){const t=Array.from(e.files.values()),r=new Array;for(const s of t){if(2!==s.status)return null;const e=new Array;for(const t of s.parts){if(!t)return null;e.push(new a(t.url,t.hash))}r.push(new u(s.name,s.mimeType,e))}return{type:"service",assets:r}}function S(e){switch(e){case"COMPLETED":case"SUBMITTED":return 2;case"INPROGRESS":return 1;default:return 0}}export{g as assetMapFromAssetMapsJSON,h as extractMesh,l as meshFeatureSetFromJSON};
