import*as Common from"../common/common.js";import*as Root from"../root/root.js";let contextInstance,ConstructorFn;export class Context{constructor(){this._flavors=new Map,this._eventDispatchers=new Map}static instance(e={forceNew:null}){const{forceNew:t}=e;return contextInstance&&!t||(contextInstance=new Context),contextInstance}setFlavor(e,t){(this._flavors.get(e)||null)!==t&&(t?this._flavors.set(e,t):this._flavors.delete(e),this._dispatchFlavorChange(e,t))}_dispatchFlavorChange(e,t){for(const n of getRegisteredListeners())n.contextTypes().includes(e)&&n.loadListener().then((e=>e.flavorChanged(t)));const s=this._eventDispatchers.get(e);s&&s.dispatchEventToListeners(Events.FlavorChanged,t)}addFlavorChangeListener(e,t,s){let n=this._eventDispatchers.get(e);n||(n=new Common.ObjectWrapper.ObjectWrapper,this._eventDispatchers.set(e,n)),n.addEventListener(Events.FlavorChanged,t,s)}removeFlavorChangeListener(e,t,s){const n=this._eventDispatchers.get(e);n&&(n.removeEventListener(Events.FlavorChanged,t,s),n.hasEventListeners(Events.FlavorChanged)||this._eventDispatchers.delete(e))}flavor(e){return this._flavors.get(e)||null}flavors(){return new Set(this._flavors.keys())}applicableExtensions(e){const t=new Set,s=this.flavors();for(const n of e)Root.Runtime.Runtime.instance().isExtensionApplicableToContextTypes(n,s)&&t.add(n);return t}}const Events={FlavorChanged:Symbol("FlavorChanged")},registeredListeners=[];export function registerListener(e){registeredListeners.push(e)}function getRegisteredListeners(){return registeredListeners}export let ContextFlavorListenerRegistration;