/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{findAssociatedFeatureService as e}from"../../layers/support/associatedFeatureServiceUtils.js";import{getLayerModuleType 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"FeatureLayer"!==a&&(t.layerType=a),t}async function l(e,r,l){let n;if(null==e?.layers||null==e?.tables){const u=await l.fetchServiceMetadata(r,{customParameters:s(e)?.customParameters});n=t(),(e=e||{}).layers=e.layers||u?.layers?.map(a),e.tables=e.tables||u?.tables?.map(a)}return{data:e,preferredHost:n}}function s(e){if(!e)return null;const{layers:r,tables:t}=e;return r?.length?r[0]:t?.length?t[0]:null}function n(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"CatalogLayer":return"CatalogLayer";case"OrientedImageryLayer":return"OrientedImageryLayer";case"SubtypeGroupLayer":case"SubtypeGroupTable":return"SubtypeGroupLayer"}return"FeatureLayer"}async function y(r,t,l){if(!r?.url)return t??{};if(t??={},!t.layers){const e=await l.fetchServiceMetadata(r.url);t.layers=e.layers?.map(a)}const{serverUrl:n,portalItem:u}=await e(r.url,{sceneLayerItem:r,customParameters:s(t)?.customParameters}).catch(()=>({serverUrl:null,portalItem:null}));if(null==n)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 l.fetchServiceMetadata(n,{customParameters:s(e)?.customParameters}).catch(()=>null);t.tables=r?.tables?.map(a)}}if(t.tables)for(const e of t.tables)e.url=`${n}/${e.id}`;return t}export{a as createSublayerData,s as getFirstLayerOrTable,n as getLayerOrTable,c as getNumLayersAndTables,u as getSublayersByLayerType,o as instanceTypeToLayerTypes,i as layerTypeToLayerModuleType,y as populateSceneServiceItemData,l as preprocessFSItemData};
