import e from"@arcgis/core/portal/Portal";import r from"@arcgis/core/portal/PortalItem";import{isTileLayer as a}from"../utilities/_layers.js";import{checkArg as i}from"../utilities/checkArg.js";import{Layer as t}from"./Layer.js";import{ERR_LAYER_TYPE_MISMATCH as s,applyLayerBaseToEsriApiLayer as l,applyBasemapLayerToEsriApiLayer as o,applyOperationalLayerToEsriApiLayer as y,fromEsriApiLayerToLayerBase as n,fromEsriApiLayerToOperationalLayer as c}from"./_layerHelper.js";export var TiledMapServiceLayer;async function p(e,r){if(e.layers&&r.sublayers)for(const a of e.layers){if(void 0===a.id||null===a.id)continue;const e=r.allSublayers.find((e=>e.id===a.id));e&&await t.applyToEsriApiSublayer(a,e)}}!function(t){async function m(t,n,c){if(i("layerJson",t).isNotMissing(),i("layerJson.layerType",t.layerType).satisfies((e=>!e||"ArcGISTiledMapServiceLayer"===e),s),i("layer",n).isNotMissing().satisfies(a,s),l(t,n),o(t,n),y(t,n),t.hasOwnProperty("customParameters")&&(n.customParameters=t.customParameters),t.hasOwnProperty("url")&&(n.url=t.url),t.hasOwnProperty("itemId")&&(n.portalItem?n.portalItem.id=t.itemId:n.portalItem=new r({id:t.itemId,portal:c||e.getDefault()})),n.loaded)await p(t,n);else{const e=n.load;let r=!1;n.load=async a=>(await e(a),r||(r=!0,await p(t,n)),n)}}t.esriTileLayerDefaults={legendEnabled:!0,visible:!0,opacity:1,minScale:0,maxScale:0},t.toEsriApiLayer=async function(e){i("layerJson",e).isNotMissing(),i("layerJson.layerType",e.layerType).satisfies((e=>!e||"ArcGISTiledMapServiceLayer"===e),s);const r={};e?.url&&(r.url=e.url);const a=new((await import("@arcgis/core/layers/TileLayer")).default)(r);return await m(e,a),a},t.applyToEsriApiLayer=m,t.fromEsriApiTileLayer=function(e){i("layer",e).isNotMissing();const r={...n(e,t.esriTileLayerDefaults),...c(e),customParameters:e.customParameters,layerType:"ArcGISTiledMapServiceLayer",url:e.url??void 0};return e.portalItem&&(r.itemId=e.portalItem.id??void 0),r}}(TiledMapServiceLayer||(TiledMapServiceLayer={}));