/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../ArrayPool","../lang","../ReentrantObjectPool","../scheduling","../SetUtils","../uid","./get","./trackingUtils","./utils"],(function(e,t,r,n,o,l,a,u,c,i){"use strict";var s;!function(e){e[e.Untracked=0]="Untracked",e[e.Tracked=1]="Tracked"}(s||(s={}));let d=function(){function e(){this.uid=a.generateUID(),this.removed=!1,this.type=null,this.oldValue=null,this.callback=null,this.getValue=null,this.target=null,this.path=null,this.equals=null}e.acquireUntracked=function(e,t,n,o,l){return this.pool.acquire(s.Untracked,e,t,n,o,l,r.equals)},e.acquireTracked=function(e,t,r,n){return this.pool.acquire(s.Tracked,e,t,r,null,null,n)};var t=e.prototype;return t.notify=function(e,t){this.type===s.Untracked?this.callback.call(this.target,e,t,this.path,this.target):this.callback.call(null,e,t)},t.acquire=function(e,t,r,n,o,l,u){this.uid=a.generateUID(),this.removed=!1,this.type=e,this.oldValue=t,this.callback=r,this.getValue=n,this.target=o,this.path=l,this.equals=u},t.release=function(){this.target=this.path=this.oldValue=this.callback=this.getValue=null,this.uid=a.generateUID(),this.removed=!0},e}();d.pool=new n.ReentrantObjectPool(d);const h=new t,f=new Set;let v;function g(e){f.delete(e),f.add(e),v||(v=o.schedule(_))}function m(e){if(e.removed)return;const t=e.oldValue,r=e.getValue();e.equals(t,r)||(e.oldValue=r,e.notify(r,t))}function p(e){const t=Array.from(f);for(let r=0;r<t.length;r++){const n=t[r];n.target===e&&(m(n),f.delete(n))}}function k(e){for(const t of f.values())t.target===e&&(t.removed=!0)}function _(){let e=10;for(;v&&e--;){v=null;const e=q(),t=h.acquire();for(const r of e){const e=r.uid;m(r),e===r.uid&&r.removed&&t.push(r)}for(const r of f)r.removed&&(t.push(r),f.delete(r));for(const r of t)d.pool.release(r);h.release(t),h.release(e),y.forEach((e=>e()))}}function q(){const e=h.acquire();e.length=f.size;let t=0;for(const r of f)e[t]=r,++t;return f.clear(),e}const y=new Set;function U(e){return y.add(e),{remove(){y.delete(e)}}}function V(e,t,r){let n=i.parse(e,t,r,((e,t,r)=>{let o,l,a=c.reactionDeferred((()=>u.valueOf(e,t)),((a,u)=>{e.__accessor__.destroyed||o&&o.uid!==l?n.remove():(o||(o=d.acquireUntracked(a,r,u,e,t),l=o.uid),g(o))}));return{remove:i.once((()=>{a.remove(),o&&(o.uid!==l||o.removed||(o.removed=!0,g(o)),o=null),n=a=null}))}}));return n}function b(e,t,n){const o=i.parse(e,t,n,((e,t,n)=>{let l=!1;return c.reaction((()=>u.valueOf(e,t)),((a,u)=>{e.__accessor__.destroyed?o.remove():l||(l=!0,r.equals(u,a)||n.call(e,a,u,t,e),l=!1)}))}));return o}function T(e,t,r,n=!1){return!e.__accessor__||e.__accessor__.destroyed?{remove(){}}:n?b(e,t,r):V(e,t,r)}function w(e,t,r){let n,o,l=c.reactionDeferred(e,((e,a)=>{n&&n.uid!==o?l.remove():(n||(n=d.acquireTracked(e,t,a,r),o=n.uid),g(n))}));return{remove:i.once((()=>{l.remove(),n&&(n.uid!==o||n.removed||(n.removed=!0,g(n)),n=null),l=null}))}}function S(e,t,r){let n=!1;return c.reaction(e,((e,o)=>{n||(n=!0,r(o,e)||t(e,o),n=!1)}))}function D(e,t,n=!1,o=r.equalsShallow){return n?S(e,t,o):w(e,t,o)}function O(e){return l.someSet(f,(t=>t.oldValue===e))}e.afterDispatch=U,e.dispatch=_,e.dispatchTarget=p,e.isValueInUse=O,e.removeTarget=k,e.watch=T,e.watchTracked=D,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
