/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/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/SpatialReference.js";import{createIncomplete as n,createWithExternalSource as a}from"../../geometry/support/meshUtils.js";import{ServiceAssetPart as u,ServiceAsset as i}from"../../geometry/support/meshUtils/External.js";import{getFormatIdMimeType as f}from"../../layers/support/infoFor3D.js";import m from"./FeatureSet.js";import{extractMeshFeatureOrigin as p,extractMeshFeatureTransform as c}from"./meshFeatureAttributes.js";const l=()=>t.getLogger("esri.rest.support.meshFeatureSet");function y(t,r,s){const n=s.features;s.features=[],delete s.geometryType;const a=m.fromJSON(s);if(a.geometryType="mesh",!s.assetMaps)return a;const u=d(r,s.assetMaps),i=t.sourceSpatialReference??o.WGS84,f=s.globalIdFieldName,{outFields:p}=t,c=null!=p&&p.length>0?g(p.includes("*")?null:new Set(p)):()=>({});for(const o of n){const t=h(o,f,i,r,u);a.features.push(new e({geometry:t,attributes:c(o)}))}return a}function g(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,o,u){const i=e.attributes[t],f=u.get(i);if(null==f||!e.geometry)return null;const m=p(e.attributes,r,o.transformFieldRoles),l=s.fromJSON(e.geometry);l.spatialReference=r;const y=c(e.attributes,o.transformFieldRoles),g=r.isGeographic?"local":"georeferenced",h=S(f);return h?a(m,h,{extent:l,transform:y,vertexSpace:g,unitConversionDisabled:!0}):n(m,{transform:y,vertexSpace:g})}function d(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,i=o.assetURL,m=o.conversionStatus,p=o.seqNo,c=f(a,e.supportedFormats);if(!c){l().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:c,status:b(m),parts:[]})).parts[p]={hash:u,url:i}}return s}function S(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 u(t.url,t.hash))}r.push(new i(s.name,s.mimeType,e))}return{type:"service",assets:r}}function b(e){switch(e){case"COMPLETED":case"SUBMITTED":return 2;case"INPROGRESS":return 1;default:return 0}}export{d as assetMapFromAssetMapsJSON,h as extractMesh,y as meshFeatureSetFromJSON};
