import{watch as o}from"../support/observableUtils.js";import{checkArg as e}from"./checkArg.js";import{_generateChangeEvent as t}from"./collection.js";export function onEach(o,t,n){e("collection",o).isNotMissing(),e("eventType",t).isNotMissing().isNotEmpty(),e("callback",n).isNotMissing();const s=new Map;for(const e of o.toArray())s.set(e,e.on(t,n));const r=o.on("change",(o=>{for(const e of o.added)s.set(e,e.on(t,n));for(const e of o.removed){const o=s.get(e);o&&o.remove(),s.delete(e)}}));return{remove:()=>{r.remove(),s.forEach((o=>o.remove()))}}}export function onCollectionPropertyChange(n,s,r,i=!1){let c;e("obj",n).isNotMissing(),e("property",s).isNotMissing().isNotEmpty(),e("callback",r).isNotMissing();const l=s.split(".");let m=n;for(;m&&"object"==typeof m&&l.length;)m=m[l.shift()];m&&(c=m.on("change",r));const p=o(n,s,((o,e)=>{c&&(c.remove(),c=void 0),o&&(c=o.on("change",r));const n=t(e,o);(n?.added.length||n?.removed.length||n?.moved.length)&&r(n)}),{sync:i});return{remove:()=>{c&&c.remove(),p.remove()}}}export function onOnce(o,e,t){const n=o.on(e,(o=>{t(o),n.remove()}));return n}