import*as SDK from"../sdk/sdk.js";export class ExecutionContextSelector{constructor(t,e){e.addFlavorChangeListener(SDK.RuntimeModel.ExecutionContext,this._executionContextChanged,this),e.addFlavorChangeListener(SDK.SDKModel.Target,this._targetChanged,this),t.addModelListener(SDK.RuntimeModel.RuntimeModel,SDK.RuntimeModel.Events.ExecutionContextCreated,this._onExecutionContextCreated,this),t.addModelListener(SDK.RuntimeModel.RuntimeModel,SDK.RuntimeModel.Events.ExecutionContextDestroyed,this._onExecutionContextDestroyed,this),t.addModelListener(SDK.RuntimeModel.RuntimeModel,SDK.RuntimeModel.Events.ExecutionContextOrderChanged,this._onExecutionContextOrderChanged,this),this._targetManager=t,this._context=e,t.observeModels(SDK.RuntimeModel.RuntimeModel,this)}modelAdded(t){queueMicrotask(function(){this._context.flavor(SDK.SDKModel.Target)||this._context.setFlavor(SDK.SDKModel.Target,t.target())}.bind(this))}modelRemoved(t){const e=this._context.flavor(SDK.RuntimeModel.ExecutionContext);e&&e.runtimeModel===t&&this._currentExecutionContextGone();const o=this._targetManager.models(SDK.RuntimeModel.RuntimeModel);this._context.flavor(SDK.SDKModel.Target)===t.target()&&o.length&&this._context.setFlavor(SDK.SDKModel.Target,o[0].target())}_executionContextChanged(t){const e=t.data;e&&(this._context.setFlavor(SDK.SDKModel.Target,e.target()),this._ignoreContextChanged||(this._lastSelectedContextId=this._contextPersistentId(e)))}_contextPersistentId(t){return t.isDefault?t.target().name()+":"+t.frameId:""}_targetChanged(t){const e=t.data,o=this._context.flavor(SDK.RuntimeModel.ExecutionContext);if(!e||o&&o.target()===e)return;const n=e.model(SDK.RuntimeModel.RuntimeModel),i=n?n.executionContexts():[];if(!i.length)return;let s=null;for(let r=0;r<i.length&&!s;++r)this._shouldSwitchToContext(i[r])&&(s=i[r]);for(let r=0;r<i.length&&!s;++r)this._isDefaultContext(i[r])&&(s=i[r]);this._ignoreContextChanged=!0,this._context.setFlavor(SDK.RuntimeModel.ExecutionContext,s||i[0]),this._ignoreContextChanged=!1}_shouldSwitchToContext(t){return!(!this._lastSelectedContextId||this._lastSelectedContextId!==this._contextPersistentId(t))||!(this._lastSelectedContextId||!this._isDefaultContext(t))}_isDefaultContext(t){if(!t.isDefault||!t.frameId)return!1;if(t.target().parentTarget())return!1;const e=t.target().model(SDK.ResourceTreeModel.ResourceTreeModel),o=e&&e.frameForId(t.frameId);return!(!o||!o.isTopFrame())}_onExecutionContextCreated(t){this._switchContextIfNecessary(t.data)}_onExecutionContextDestroyed(t){const e=t.data;this._context.flavor(SDK.RuntimeModel.ExecutionContext)===e&&this._currentExecutionContextGone()}_onExecutionContextOrderChanged(t){const e=t.data.executionContexts();for(let o=0;o<e.length&&!this._switchContextIfNecessary(e[o]);o++);}_switchContextIfNecessary(t){return!(this._context.flavor(SDK.RuntimeModel.ExecutionContext)&&!this._shouldSwitchToContext(t))&&(this._ignoreContextChanged=!0,this._context.setFlavor(SDK.RuntimeModel.ExecutionContext,t),this._ignoreContextChanged=!1,!0)}_currentExecutionContextGone(){const t=this._targetManager.models(SDK.RuntimeModel.RuntimeModel);let e=null;for(let o=0;o<t.length&&!e;++o){const n=t[o].executionContexts();for(const t of n)if(this._isDefaultContext(t)){e=t;break}}if(!e)for(let o=0;o<t.length&&!e;++o){const n=t[o].executionContexts();if(n.length){e=n[0];break}}this._ignoreContextChanged=!0,this._context.setFlavor(SDK.RuntimeModel.ExecutionContext,e),this._ignoreContextChanged=!1}}