/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
import{Store as e}from"./store.js";import{__storeToDerived as t,__derivedToStore as s}from"./scheduler.js";class i{constructor(e){this.listeners=/* @__PURE__ */new Set,this._subscriptions=[],this.lastSeenDepValues=[],this.getDepVals=()=>{const e=this.options.deps.length,t=new Array(e),s=new Array(e);for(let i=0;i<e;i++){const e=this.options.deps[i];t[i]=e.prevState,s[i]=e.state}return this.lastSeenDepValues=s,{prevDepVals:t,currDepVals:s,prevVal:this.prevState??void 0}},this.recompute=()=>{var e,t;this.prevState=this.state;const s=this.getDepVals();this.state=this.options.fn(s),null==(t=(e=this.options).onUpdate)||t.call(e)},this.checkIfRecalculationNeededDeeply=()=>{for(const r of this.options.deps)r instanceof i&&r.checkIfRecalculationNeededDeeply();let e=!1;const t=this.lastSeenDepValues,{currDepVals:s}=this.getDepVals();for(let i=0;i<s.length;i++)if(s[i]!==t[i]){e=!0;break}e&&this.recompute()},this.mount=()=>(this.registerOnGraph(),this.checkIfRecalculationNeededDeeply(),()=>{this.unregisterFromGraph();for(const e of this._subscriptions)e()}),this.subscribe=e=>{var t,s;this.listeners.add(e);const i=null==(s=(t=this.options).onSubscribe)?void 0:s.call(t,e,this);return()=>{this.listeners.delete(e),null==i||i()}},this.options=e,this.state=e.fn({prevDepVals:void 0,prevVal:void 0,currDepVals:this.getDepVals().currDepVals})}registerOnGraph(r=this.options.deps){for(const o of r)if(o instanceof i)o.registerOnGraph(),this.registerOnGraph(o.options.deps);else if(o instanceof e){let e=t.get(o);e||(e=/* @__PURE__ */new Set,t.set(o,e)),e.add(this);let i=s.get(this);i||(i=/* @__PURE__ */new Set,s.set(this,i)),i.add(o)}}unregisterFromGraph(r=this.options.deps){for(const o of r)if(o instanceof i)this.unregisterFromGraph(o.options.deps);else if(o instanceof e){const e=t.get(o);e&&e.delete(this);const i=s.get(this);i&&i.delete(o)}}}export{i as Derived};