import t from"@arcgis/core/core/Collection.js";import{isLayerExtension as e,isMapExtension as r,isSublayerExtension as i,isTableExtension as n}from"../ItemType.js";import{MapExtension as o}from"../mapping/MapExtension.js";import{isMap as a,isWebMap as u,isWebScene as l}from"../utilities/mapping.js";import{getLayerUrl as s,hasMmapLayer as f}from"./_layers.js";import{isGroupLayerExtension as p}from"./extensions.js";function c(t){if(t.layer)return;const e=function(t){const e=t.toJSON?.();if(e)return n(t),e?.$ref}(t);return e?"string"==typeof e?e:e.path:void 0}function y(t){if(t.sublayer)return t.sublayer.id;const e=function(t){const e=t.toJSON?.();if(e)return e?.$ref}(t);return e?"string"==typeof e?e:e.id:void 0}export function createRef(t){return"string"==typeof t?{id:t,title:t}:"number"==typeof t?{id:t}:e(t)?function(t){const e={id:t.layer?.id,title:t.title,url:s(t.layer)},r=c(t);return r&&(e.path=r),e}(t):i(t)?{id:y(r=t),title:r.title}:t&&"object"==typeof t&&"id"in t?t:void 0;var r}export function isLayerRefEqual(t,e,r){const i=t.id&&e.id&&t.id===e.id,n=t.title===e.title,o=t.url&&e.url&&t.url===e.url,a=t.path&&e.path&&t.path===e.path;if(i&&n||n&&a||n&&o||i&&o)return!0;if(n&&r){return r.some((t=>t.title===e.title&&(f([t])||p(t)&&0===t.layerExtensions.length)))}return!1}export function isSublayerRefEqual(t,e){return t.id===e.id&&t.title===e.title}export function createFlattenedLayerXList(e){if(r(e))return createFlattenedLayerXList(e.layerExtensions);const i=new t;for(const t of e.filter((t=>!!t.layer)).toArray())i.add(t),p(t)&&i.addMany(createFlattenedLayerXList(t.layerExtensions));return i}export function getBackingLayer(t){return e(t)?t.layer:t.sublayer}export function toMapExtension(t){const e=t;return a(e)||u(e)||l(e)?new o({map:e}):t}export function isIterable(t){return!!t?.[Symbol.iterator]}