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