/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{isSome as e}from"../../../core/arrayUtils.js";import t from"../../../core/Error.js";import{throwIfAborted as a}from"../../../core/promiseUtils.js";import{isTiledLayer as i,isBasemap3DSupportedLayer as r,isIntegratedMesh3DTilesLayer as n,isBasemapSupportedTiledLayer as s}from"../../../layers/support/layerUtils.js";import{stringFromViewingMode as l}from"../../../views/ViewingMode.js";import{getTileInfoAndExtentFromWMTSLayer as o,checkIfTileInfoSupportedForView as p}from"../../../views/3d/terrain/terrainUtils.js";import{TilingScheme as c}from"../../../views/3d/terrain/TilingScheme.js";import{isSpatialReferenceSupported as m}from"../../../views/support/spatialReferenceSupport.js";async function f(e,t={}){const{basemap:i,view:r}=e;await i.load(t),w(i),await h(i,r,t),a(t)}async function u(t,r={}){const{basemap:n,view:s}=t;a(r);const l=n.baseLayers.find(e=>"unknown"===e.type)?.loadError;if(null!=l)throw l;if(!s||"spatialReferenceLocked"in s&&!s.spatialReferenceLocked)return;if(await n.load(r),a(r),0===n.baseLayers.length)return;const o=n.baseLayers.at(0);if(!i(o))return;if(n.spatialReference){if(s.spatialReference.equals(n.spatialReference))return;y()}await o.load(r),a(r);const p=(("supportedSpatialReferences"in o?o.supportedSpatialReferences:null)||["tileInfo"in o?o.tileInfo?.spatialReference:null]).filter(e);0!==p.length&&p.every(e=>!s.spatialReference.equals(e))&&y()}function y(){throw new t("basemap-compatibility:incompatible-spatial-reference","Basemap spatial reference is not compatible with the view")}function w(e){if(0===e.baseLayers.length&&0===e.groundLayers.length&&0===e.referenceLayers.length)return;const t=e.baseLayers.concat(e.referenceLayers).toArray().filter(e=>!r(e)).map(e=>b(e));if(t.length)throw t[0];if(e.groundLayers){if(e.groundLayers.filter(e=>!n(e)).map(e=>b(e)).length)throw t[0]}}function b(e){return new t("basemap-compatibility:unsupported-basemap-layer-type","Unsupported basemap layer type ${operationalLayerType}",{layer:e,operationalLayerType:e.operationalLayerType||"unknown"})}async function h(e,a,i){let r=e.baseLayers.at(0);if(e.groundLayers?.length>0)r=e.groundLayers.at(0);else{if(!(e.baseLayers.length>0))return;r=e.baseLayers.at(0)}if(s(r)){try{await r.load(i)}catch(n){const e="basemap-compatibility:unknown-error",a="Unknown basemap compatibility error",{name:i=e,message:r=a,details:s}=n;throw new t(i,r,s)}g(r,a)}}function g(e,a){const i=a.state.viewingMode;if(!i)return;let r,n;if("wmts"===e?.type){const s=o(e,a.spatialReference,i);if(null==s.tileInfo)throw new t("basemapgalleryitem:tiling-scheme-incompatible","Basemap tiling scheme is incompatible with the view");r=s.tileInfo,n=s.fullExtent}else r=e.tileInfo,n=e.fullExtent;if(null==r)return;if(!m(r.spatialReference,i))throw new t(`basemapgalleryitem:spatial-reference-unsupported-${l(i)}`,`Basemap spatial reference is unsupported in ${l(i)} mode`);const s="vector-tile"===e?.type?r.getCompatibleForVTL(256):null;if(1===i){let a=p(r,n,null,i);if(a&&"vector-tile"===e?.type&&null!=n&&s&&!p(s,n,null,i)&&(a=null),a){const e=r.spatialReference.isWebMercator?"web-mercator":"wgs84";throw new t(`basemapgalleryitem:tiling-scheme-unsupported-${e}-global`,"Basemap tiling scheme is unsupported in global mode",{error:a})}}else if(c.checkUnsupported(r))throw new t("basemapgalleryitem:tiling-scheme-unsupported-local","Basemap tiling scheme is unsupported in local mode");const f=a.basemapTerrain?.tilingScheme;if(f&&!f.compatibleWith(r)&&("vector-tile"!==e?.type||!s||!f.compatibleWith(s)))throw new t("basemapgalleryitem:tiling-scheme-incompatible","Basemap tiling scheme is incompatible with the view")}export{u as default2DCompatibility,f as default3DCompatibility};
