/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import{findAssociatedFeatureService as e}from"../../layers/support/associatedFeatureServiceUtils.js";import{getFeatureServiceItemLayerType as r}from"../../layers/support/fetchService.js";import{getPreferredHost as t}from"../../layers/support/layerUtils.js";function a(e){const t={id:e.id,name:e.name},a=r(e.type);return a&&(t.layerType=a),t}async function n(e,r,n){let l;if(null==e?.layers||null==e?.tables){const u=await n.fetchServiceMetadata(r,{customParameters:s(e)?.customParameters});l=t(),(e=e||{}).layers=e.layers||u?.layers?.map(a),e.tables=e.tables||u?.tables?.map(a)}return{data:e,preferredHost:l}}function s(e){if(!e)return null;const{layers:r,tables:t}=e;return r?.length?r[0]:t?.length?t[0]:null}function l(e,r){if(null==r)return null;return[...e.layers||[],...e.tables||[]].find(e=>e.id===r)}function u(e,r){return[...e.layers||[],...e.tables||[]].filter(({layerType:e})=>e?r.includes(e):r.includes("ArcGISFeatureLayer"))}function c(e){return(e?.layers?.length??0)+(e?.tables?.length??0)}function o(e){switch(e){case"catalog":return["CatalogLayer"];case"feature":return["ArcGISFeatureLayer"];case"oriented-imagery":return["OrientedImageryLayer"];case"subtype-group":return["SubtypeGroupLayer","SubtypeGroupTable"]}return null}function i(e){switch(e){case"ArcGISAnnotationLayer":return"AnnotationFeatureLayer";case"ArcGISDimensionLayer":return"DimensionFeatureLayer";case"CatalogLayer":return"CatalogLayer";case"OrientedImageryLayer":return"OrientedImageryLayer";case"SubtypeGroupLayer":case"SubtypeGroupTable":return"SubtypeGroupLayer"}return"FeatureLayer"}async function y(r,t,n){if(!r?.url)return t??{};if(t??={},!t.layers){const e=await n.fetchServiceMetadata(r.url);t.layers=e.layers?.map(a)}const{serverUrl:l,portalItem:u}=await e(r.url,{sceneLayerItem:r,customParameters:s(t)?.customParameters}).catch(()=>({serverUrl:null,portalItem:null}));if(null==l)return t.tables=[],t;if(!t.tables&&u){const e=await u.fetchData().catch(()=>null);if(e?.tables)t.tables=e.tables.map(a);else{const r=await n.fetchServiceMetadata(l,{customParameters:s(e)?.customParameters}).catch(()=>null);t.tables=r?.tables?.map(a)}}if(t.tables)for(const e of t.tables)e.url=`${l}/${e.id}`;return t}export{a as createSublayerData,s as getFirstLayerOrTable,l as getLayerOrTable,c as getNumLayersAndTables,u as getSublayersByLayerType,o as instanceTypeToLayerTypes,i as layerTypeToLayerModuleType,y as populateSceneServiceItemData,n as preprocessFSItemData};
