/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../core/Collection.js";import{makeHandle as r}from"../core/handleUtils.js";import o from"../core/Logger.js";import{getOrCreateMapValue as t}from"../core/MapUtils.js";import n from"../rest/featureService/FeatureService.js";import s from"./VersioningState.js";import a from"./VersionManagementService.js";import{createVersionAdapters as i}from"./versionAdapters/utils.js";async function c(r,o){let t;if("layers"in r){const e=i(r.allTables.concat(r.allLayers).filter(e=>"group"!==e.type).toArray());r.utilityNetworks&&r.utilityNetworks.forEach(r=>{const o=i([r]);e.push(...o)}),t=e}else t=new e(r);const c=new Map;for(const n of t){const r=c.get(n.featureServiceUrl);r?r.push(n):c.set(n.featureServiceUrl,new e([n]))}const l=new e;for(const[e,i]of c){const r=new n({url:e});if(await r.load(),!r.versionManagementServiceUrl)continue;const t=new a({url:r.versionManagementServiceUrl});l.push(new s({versionManagementService:t,versionableItems:i,usePersistentReadSessions:o}))}return l}const l=new Map;async function f(e,n=!1){const s=e.map;s.loaded||await s.load();const a=await c(s,n);return t(l,e,()=>(e.addHandles(r(()=>{l.delete(e),a.forEach(e=>e.destroy())})),a.forEach(e=>e.load().catch(e=>{o.getLogger("esri.versionManagement.VersioningState").error("Failed to load Versioning State",e)})),a))}export{c as createVersioningStates,f as getVersioningStates};
