"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("../frameloop/sync-time.mjs.js"),e=require("../frameloop/frame.mjs.js"),s=require("../../../../motion-utils/dist/es/warn-once.mjs.js"),i=require("../../../../motion-utils/dist/es/subscription-manager.mjs.js"),n=require("../../../../motion-utils/dist/es/velocity-per-second.mjs.js");class a{constructor(e){let s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};this.canTrackVelocity=null,this.events={},this.updateAndNotify=e=>{const s=t.time.now();if(this.updatedAt!==s&&this.setPrevFrameValue(),this.prev=this.current,this.setCurrent(e),this.current!==this.prev&&(this.events.change?.notify(this.current),this.dependents))for(const t of this.dependents)t.dirty()},this.hasAnimated=!1,this.setCurrent(e),this.owner=s.owner}setCurrent(e){var s;this.current=e,this.updatedAt=t.time.now(),null===this.canTrackVelocity&&void 0!==e&&(this.canTrackVelocity=(s=this.current,!isNaN(parseFloat(s))))}setPrevFrameValue(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.current;this.prevFrameValue=t,this.prevUpdatedAt=this.updatedAt}onChange(t){return"production"!==process.env.NODE_ENV&&s.warnOnce(!1,'value.onChange(callback) is deprecated. Switch to value.on("change", callback).'),this.on("change",t)}on(t,s){this.events[t]||(this.events[t]=new i.SubscriptionManager);const n=this.events[t].add(s);return"change"===t?()=>{n(),e.frame.read((()=>{this.events.change.getSize()||this.stop()}))}:n}clearListeners(){for(const t in this.events)this.events[t].clear()}attach(t,e){this.passiveEffect=t,this.stopPassiveEffect=e}set(t){this.passiveEffect?this.passiveEffect(t,this.updateAndNotify):this.updateAndNotify(t)}setWithVelocity(t,e,s){this.set(e),this.prev=void 0,this.prevFrameValue=t,this.prevUpdatedAt=this.updatedAt-s}jump(t){let e=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];this.updateAndNotify(t),this.prev=t,this.prevUpdatedAt=this.prevFrameValue=void 0,e&&this.stop(),this.stopPassiveEffect&&this.stopPassiveEffect()}dirty(){this.events.change?.notify(this.current)}addDependent(t){this.dependents||(this.dependents=new Set),this.dependents.add(t)}removeDependent(t){this.dependents&&this.dependents.delete(t)}get(){return this.current}getPrevious(){return this.prev}getVelocity(){const e=t.time.now();if(!this.canTrackVelocity||void 0===this.prevFrameValue||e-this.updatedAt>30)return 0;const s=Math.min(this.updatedAt-this.prevUpdatedAt,30);return n.velocityPerSecond(parseFloat(this.current)-parseFloat(this.prevFrameValue),s)}start(t){return this.stop(),new Promise((e=>{this.hasAnimated=!0,this.animation=t(e),this.events.animationStart&&this.events.animationStart.notify()})).then((()=>{this.events.animationComplete&&this.events.animationComplete.notify(),this.clearAnimation()}))}stop(){this.animation&&(this.animation.stop(),this.events.animationCancel&&this.events.animationCancel.notify()),this.clearAnimation()}isAnimating(){return!!this.animation}clearAnimation(){delete this.animation}destroy(){this.dependents?.clear(),this.events.destroy?.notify(),this.clearListeners(),this.stop(),this.stopPassiveEffect&&this.stopPassiveEffect()}}exports.MotionValue=a,exports.motionValue=function(t,e){return new a(t,e)};
//# sourceMappingURL=index.mjs.js.map
