import e from"@arcgis/core/portal/Portal";import i from"@arcgis/core/portal/PortalItem";import{watch as r}from"../support/observableUtils.js";import{isBuildingSceneLayer as a}from"../utilities/_layers.js";import{checkArg as t}from"../utilities/checkArg.js";import{isNumeric as l}from"../utilities/number.js";import{parallelEach as s}from"../utilities/promise.js";import{BuildingSceneSublayer as o}from"./BuildingSceneSublayer.js";import{LayerDefinition as n}from"./LayerDefinition.js";import{ERR_LAYER_TYPE_MISMATCH as y,applyLayerBaseToEsriApiLayer as p,applyOperationalLayerToEsriApiLayer as u,fromEsriApiLayerToLayerBase as d,fromEsriApiLayerToOperationalLayer as f}from"./_layerHelper.js";const c=new WeakMap;export var BuildingSceneLayer;async function m(e,i){e.sublayers&&await s(e.sublayers,(async e=>{const r=i.allSublayers.find((i=>i.id===e.id));r&&await o.applyToEsriApiBuildingSublayer(e,r)}))}!function(s){async function g(l,s,o){if(t("layerJson",l).isNotMissing(),t("layerJson.layerType",l.layerType).satisfies((e=>!e||"BuildingSceneLayer"===e),y),t("layer",s).isNotMissing().satisfies(a,y),p(l,s),u(l,s),l.hasOwnProperty("url")&&(s.url=l.url),l.hasOwnProperty("itemId")&&(s.portalItem?s.portalItem.id=l.itemId:s.portalItem=new i({id:l.itemId,portal:o||e.getDefault()})),l.hasOwnProperty("activeFilterId")&&(s.activeFilterId=l.activeFilterId),l.hasOwnProperty("filters")&&null!=l.filters){const e=(await import("@arcgis/core/layers/support/BuildingFilter")).default;s.filters.addMany(l.filters.map((i=>e.fromJSON(i))).filter((e=>!s.filters.find((i=>i.id===e.id)))))}l.hasOwnProperty("layerDefinition")&&null!=l.layerDefinition&&await n.applyToEsriApiLayer(l.layerDefinition,s),s.loaded?await m(l,s):c.has(s)?c.set(s,{...c.get(s),...l}):(c.set(s,l),r(s,"loaded",(async()=>{await m(c.get(s),s),c.delete(s)}),{once:!0}))}s.esriBuildingSceneLayerDefaults={elevationInfo:{mode:"absolute-height",offset:0,unit:"meters"},listMode:"show",maxScale:0,minScale:0,opacity:1,visible:!0},s.toEsriApiLayer=async function(e,i){t("layerJson",e).isNotMissing(),t("layerJson.layerType",e.layerType).satisfies((e=>!e||"BuildingSceneLayer"===e),y);const r=new((await import("@arcgis/core/layers/BuildingSceneLayer")).default);return await g(e,r,i),r},s.applyToEsriApiLayer=g,s.fromEsriApiBuildingSceneLayer=function(e){t("layer",e).isNotMissing();const i={...d(e,s.esriBuildingSceneLayerDefaults),...f(e),layerType:"BuildingSceneLayer",url:l(e.layerId)?`${e.url}/layers/${e.layerId}`:e.url??void 0};return e.portalItem&&(i.itemId=e.portalItem.id??void 0),i.layerDefinition=n.fromEsriApiLayer(e,s.esriBuildingSceneLayerDefaults),e.activeFilterId&&(i.activeFilterId=e.activeFilterId),e.filters&&(i.filters=e.filters.toArray().map((e=>e.toJSON()))),e.allSublayers&&(i.sublayers=e.allSublayers.toArray().map((e=>o.fromEsriApiBuildingSublayer(e)))),i}}(BuildingSceneLayer||(BuildingSceneLayer={}));