/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../core/Collection.js";import{sync as r}from"../../core/reactiveUtils.js";import{autorun as t}from"../../core/accessorSupport/trackingUtils.js";const n=new e,o=new WeakMap;function f(e){i(e)&&n.push(new WeakRef(e))}function s(e){const r=n.find(r=>r.deref()===e);r&&n.remove(r)}function i(e){return null!=e&&"object"==typeof e&&"refreshInterval"in e&&"refresh"in e}function c(e,r){return Number.isFinite(e)&&Number.isFinite(r)?r<=0?e:c(r,e%r):0}let l=0,a=0;function u(){const e=Date.now();let r=!1;for(const t of n){const n=t.deref();if(n){if(n.refreshInterval){e-(o.get(n)??0)+5>=6e4*n.refreshInterval&&(o.set(n,e),n.refresh(e))}}else r=!0}if(r)for(let t=n.length-1;t>=0;t--){n.at(t).deref()||n.removeAt(t)}}t(()=>{const e=Date.now();let r=0;for(const t of n){const n=t.deref();n&&(r=c(Math.round(6e4*n.refreshInterval),r),n.refreshInterval?o.get(n)||o.set(n,e):o.delete(n))}if(r!==a){if(a=r,clearInterval(l),0===a)return void(l=0);l=setInterval(u,a)}},r);const h={get hasRefreshTimer(){return l>0},get tickInterval(){return a},forceRefresh(){u()},hasLayer:e=>i(e)&&n.some(r=>r.deref()===e),clear(){for(const e of n){const r=e.deref();r&&o.delete(r)}n.removeAll()}};export{f as registerLayer,h as test,s as unregisterLayer};
