import * as Common from "../common/common.js";
import * as i18n from "../i18n/i18n.js";
import * as Platform from "../platform/platform.js";
import * as Protocol from "../../generated/protocol.js";
import { DOMModel } from "./DOMModel.js";
import { Events as NetworkManagerEvents, NetworkManager } from "./NetworkManager.js";
import { Resource } from "./Resource.js";
import { ExecutionContext, RuntimeModel } from "./RuntimeModel.js";
import { Capability } from "./Target.js";
import { SDKModel } from "./SDKModel.js";
import { TargetManager } from "./TargetManager.js";
import { SecurityOriginManager } from "./SecurityOriginManager.js";
export class ResourceTreeModel extends SDKModel {
  agent;
  #securityOriginManager;
  framesInternal;
  #cachedResourcesProcessed;
  #pendingReloadOptions;
  #reloadSuspensionCount;
  isInterstitialShowing;
  mainFrame;
  #pendingBackForwardCacheNotUsedEvents;
  constructor(target) {
    super(target);
    const networkManager = target.model(NetworkManager);
    if (networkManager) {
      networkManager.addEventListener(NetworkManagerEvents.RequestFinished, this.onRequestFinished, this);
      networkManager.addEventListener(NetworkManagerEvents.RequestUpdateDropped, this.onRequestUpdateDropped, this);
    }
    this.agent = target.pageAgent();
    void this.agent.invoke_enable();
    this.#securityOriginManager = target.model(SecurityOriginManager);
    this.#pendingBackForwardCacheNotUsedEvents = /* @__PURE__ */ new Set();
    target.registerPageDispatcher(new PageDispatcher(this));
    this.framesInternal = /* @__PURE__ */ new Map();
    this.#cachedResourcesProcessed = false;
    this.#pendingReloadOptions = null;
    this.#reloadSuspensionCount = 0;
    this.isInterstitialShowing = false;
    this.mainFrame = null;
    void this.agent.invoke_getResourceTree().then((event) => {
      this.processCachedResources(event.getError() ? null : event.frameTree);
    });
  }
  static frameForRequest(request) {
    const networkManager = NetworkManager.forRequest(request);
    const resourceTreeModel = networkManager ? networkManager.target().model(ResourceTreeModel) : null;
    if (!resourceTreeModel) {
      return null;
    }
    return request.frameId ? resourceTreeModel.frameForId(request.frameId) : null;
  }
  static frames() {
    const result = [];
    for (const resourceTreeModel of TargetManager.instance().models(ResourceTreeModel)) {
      result.push(...resourceTreeModel.framesInternal.values());
    }
    return result;
  }
  static resourceForURL(url) {
    for (const resourceTreeModel of TargetManager.instance().models(ResourceTreeModel)) {
      const mainFrame = resourceTreeModel.mainFrame;
      const result = mainFrame ? mainFrame.resourceForURL(url) : null;
      if (result) {
        return result;
      }
    }
    return null;
  }
  static reloadAllPages(bypassCache, scriptToEvaluateOnLoad) {
    for (const resourceTreeModel of TargetManager.instance().models(ResourceTreeModel)) {
      if (!resourceTreeModel.target().parentTarget()) {
        resourceTreeModel.reloadPage(bypassCache, scriptToEvaluateOnLoad);
      }
    }
  }
  domModel() {
    return this.target().model(DOMModel);
  }
  processCachedResources(mainFramePayload) {
    if (mainFramePayload && mainFramePayload.frame.url !== ":") {
      this.dispatchEventToListeners(Events.WillLoadCachedResources);
      this.addFramesRecursively(null, mainFramePayload);
      this.target().setInspectedURL(mainFramePayload.frame.url);
    }
    this.#cachedResourcesProcessed = true;
    const runtimeModel = this.target().model(RuntimeModel);
    if (runtimeModel) {
      runtimeModel.setExecutionContextComparator(this.executionContextComparator.bind(this));
      runtimeModel.fireExecutionContextOrderChanged();
    }
    this.dispatchEventToListeners(Events.CachedResourcesLoaded, this);
  }
  cachedResourcesLoaded() {
    return this.#cachedResourcesProcessed;
  }
  addFrame(frame, _aboutToNavigate) {
    this.framesInternal.set(frame.id, frame);
    if (frame.isMainFrame()) {
      this.mainFrame = frame;
    }
    this.dispatchEventToListeners(Events.FrameAdded, frame);
    this.updateSecurityOrigins();
  }
  frameAttached(frameId, parentFrameId, stackTrace) {
    const sameTargetParentFrame = parentFrameId ? this.framesInternal.get(parentFrameId) || null : null;
    if (!this.#cachedResourcesProcessed && sameTargetParentFrame) {
      return null;
    }
    if (this.framesInternal.has(frameId)) {
      return null;
    }
    const frame = new ResourceTreeFrame(this, sameTargetParentFrame, frameId, null, stackTrace || null);
    if (parentFrameId && !sameTargetParentFrame) {
      frame.crossTargetParentFrameId = parentFrameId;
    }
    if (frame.isMainFrame() && this.mainFrame) {
      this.frameDetached(this.mainFrame.id, false);
    }
    this.addFrame(frame, true);
    return frame;
  }
  frameNavigated(framePayload, type) {
    const sameTargetParentFrame = framePayload.parentId ? this.framesInternal.get(framePayload.parentId) || null : null;
    if (!this.#cachedResourcesProcessed && sameTargetParentFrame) {
      return;
    }
    let frame = this.framesInternal.get(framePayload.id) || null;
    if (!frame) {
      frame = this.frameAttached(framePayload.id, framePayload.parentId || null);
      console.assert(Boolean(frame));
      if (!frame) {
        return;
      }
    }
    this.dispatchEventToListeners(Events.FrameWillNavigate, frame);
    frame.navigate(framePayload);
    if (type) {
      frame.backForwardCacheDetails.restoredFromCache = type === Protocol.Page.NavigationType.BackForwardCacheRestore;
    }
    this.dispatchEventToListeners(Events.FrameNavigated, frame);
    if (frame.isMainFrame()) {
      this.processPendingBackForwardCacheNotUsedEvents(frame);
      this.dispatchEventToListeners(Events.MainFrameNavigated, frame);
      const networkManager = this.target().model(NetworkManager);
      if (networkManager) {
        networkManager.clearRequests();
      }
    }
    const resources = frame.resources();
    for (let i = 0; i < resources.length; ++i) {
      this.dispatchEventToListeners(Events.ResourceAdded, resources[i]);
    }
    if (frame.isMainFrame()) {
      this.target().setInspectedURL(frame.url);
    }
    this.updateSecurityOrigins();
  }
  documentOpened(framePayload) {
    this.frameNavigated(framePayload, void 0);
    const frame = this.framesInternal.get(framePayload.id);
    if (frame && !frame.getResourcesMap().get(framePayload.url)) {
      const frameResource = this.createResourceFromFramePayload(framePayload, framePayload.url, Common.ResourceType.resourceTypes.Document, framePayload.mimeType, null, null);
      frameResource.isGenerated = true;
      frame.addResource(frameResource);
    }
  }
  frameDetached(frameId, isSwap) {
    if (!this.#cachedResourcesProcessed) {
      return;
    }
    const frame = this.framesInternal.get(frameId);
    if (!frame) {
      return;
    }
    const sameTargetParentFrame = frame.sameTargetParentFrame();
    if (sameTargetParentFrame) {
      sameTargetParentFrame.removeChildFrame(frame, isSwap);
    } else {
      frame.remove(isSwap);
    }
    this.updateSecurityOrigins();
  }
  onRequestFinished(event) {
    if (!this.#cachedResourcesProcessed) {
      return;
    }
    const request = event.data;
    if (request.failed || request.resourceType() === Common.ResourceType.resourceTypes.XHR) {
      return;
    }
    const frame = request.frameId ? this.framesInternal.get(request.frameId) : null;
    if (frame) {
      frame.addRequest(request);
    }
  }
  onRequestUpdateDropped(event) {
    if (!this.#cachedResourcesProcessed) {
      return;
    }
    const data = event.data;
    const frameId = data.frameId;
    if (!frameId) {
      return;
    }
    const frame = this.framesInternal.get(frameId);
    if (!frame) {
      return;
    }
    const url = data.url;
    if (frame.getResourcesMap().get(url)) {
      return;
    }
    const resource = new Resource(this, null, url, frame.url, frameId, data.loaderId, Common.ResourceType.resourceTypes[data.resourceType], data.mimeType, data.lastModified, null);
    frame.addResource(resource);
  }
  frameForId(frameId) {
    return this.framesInternal.get(frameId) || null;
  }
  forAllResources(callback) {
    if (this.mainFrame) {
      return this.mainFrame.callForFrameResources(callback);
    }
    return false;
  }
  frames() {
    return [...this.framesInternal.values()];
  }
  resourceForURL(url) {
    return this.mainFrame ? this.mainFrame.resourceForURL(url) : null;
  }
  addFramesRecursively(sameTargetParentFrame, frameTreePayload) {
    const framePayload = frameTreePayload.frame;
    const frame = new ResourceTreeFrame(this, sameTargetParentFrame, framePayload.id, framePayload, null);
    if (!sameTargetParentFrame && framePayload.parentId) {
      frame.crossTargetParentFrameId = framePayload.parentId;
    }
    this.addFrame(frame);
    for (const childFrame of frameTreePayload.childFrames || []) {
      this.addFramesRecursively(frame, childFrame);
    }
    for (let i = 0; i < frameTreePayload.resources.length; ++i) {
      const subresource = frameTreePayload.resources[i];
      const resource = this.createResourceFromFramePayload(framePayload, subresource.url, Common.ResourceType.resourceTypes[subresource.type], subresource.mimeType, subresource.lastModified || null, subresource.contentSize || null);
      frame.addResource(resource);
    }
    if (!frame.getResourcesMap().get(framePayload.url)) {
      const frameResource = this.createResourceFromFramePayload(framePayload, framePayload.url, Common.ResourceType.resourceTypes.Document, framePayload.mimeType, null, null);
      frame.addResource(frameResource);
    }
  }
  createResourceFromFramePayload(frame, url, type, mimeType, lastModifiedTime, contentSize) {
    const lastModified = typeof lastModifiedTime === "number" ? new Date(lastModifiedTime * 1e3) : null;
    return new Resource(this, null, url, frame.url, frame.id, frame.loaderId, type, mimeType, lastModified, contentSize);
  }
  suspendReload() {
    this.#reloadSuspensionCount++;
  }
  resumeReload() {
    this.#reloadSuspensionCount--;
    console.assert(this.#reloadSuspensionCount >= 0, "Unbalanced call to ResourceTreeModel.resumeReload()");
    if (!this.#reloadSuspensionCount && this.#pendingReloadOptions) {
      const { ignoreCache, scriptToEvaluateOnLoad } = this.#pendingReloadOptions;
      this.reloadPage(ignoreCache, scriptToEvaluateOnLoad);
    }
  }
  reloadPage(ignoreCache, scriptToEvaluateOnLoad) {
    if (!this.#pendingReloadOptions) {
      this.dispatchEventToListeners(Events.PageReloadRequested, this);
    }
    if (this.#reloadSuspensionCount) {
      this.#pendingReloadOptions = { ignoreCache, scriptToEvaluateOnLoad };
      return;
    }
    this.#pendingReloadOptions = null;
    const networkManager = this.target().model(NetworkManager);
    if (networkManager) {
      networkManager.clearRequests();
    }
    this.dispatchEventToListeners(Events.WillReloadPage);
    void this.agent.invoke_reload({ ignoreCache, scriptToEvaluateOnLoad });
  }
  navigate(url) {
    return this.agent.invoke_navigate({ url });
  }
  async navigationHistory() {
    const response = await this.agent.invoke_getNavigationHistory();
    if (response.getError()) {
      return null;
    }
    return { currentIndex: response.currentIndex, entries: response.entries };
  }
  navigateToHistoryEntry(entry) {
    void this.agent.invoke_navigateToHistoryEntry({ entryId: entry.id });
  }
  setLifecycleEventsEnabled(enabled) {
    return this.agent.invoke_setLifecycleEventsEnabled({ enabled });
  }
  async fetchAppManifest() {
    const response = await this.agent.invoke_getAppManifest();
    if (response.getError()) {
      return { url: response.url, data: null, errors: [] };
    }
    return { url: response.url, data: response.data || null, errors: response.errors };
  }
  async getInstallabilityErrors() {
    const response = await this.agent.invoke_getInstallabilityErrors();
    return response.installabilityErrors || [];
  }
  async getManifestIcons() {
    const response = await this.agent.invoke_getManifestIcons();
    return { primaryIcon: response.primaryIcon || null };
  }
  async getAppId() {
    return this.agent.invoke_getAppId();
  }
  executionContextComparator(a, b) {
    function framePath(frame) {
      let currentFrame = frame;
      const parents = [];
      while (currentFrame) {
        parents.push(currentFrame);
        currentFrame = currentFrame.sameTargetParentFrame();
      }
      return parents.reverse();
    }
    if (a.target() !== b.target()) {
      return ExecutionContext.comparator(a, b);
    }
    const framesA = a.frameId ? framePath(this.frameForId(a.frameId)) : [];
    const framesB = b.frameId ? framePath(this.frameForId(b.frameId)) : [];
    let frameA;
    let frameB;
    for (let i = 0; ; i++) {
      if (!framesA[i] || !framesB[i] || framesA[i] !== framesB[i]) {
        frameA = framesA[i];
        frameB = framesB[i];
        break;
      }
    }
    if (!frameA && frameB) {
      return -1;
    }
    if (!frameB && frameA) {
      return 1;
    }
    if (frameA && frameB) {
      return frameA.id.localeCompare(frameB.id);
    }
    return ExecutionContext.comparator(a, b);
  }
  getSecurityOriginData() {
    const securityOrigins = /* @__PURE__ */ new Set();
    let mainSecurityOrigin = null;
    let unreachableMainSecurityOrigin = null;
    for (const frame of this.framesInternal.values()) {
      const origin = frame.securityOrigin;
      if (!origin) {
        continue;
      }
      securityOrigins.add(origin);
      if (frame.isMainFrame()) {
        mainSecurityOrigin = origin;
        if (frame.unreachableUrl()) {
          const unreachableParsed = new Common.ParsedURL.ParsedURL(frame.unreachableUrl());
          unreachableMainSecurityOrigin = unreachableParsed.securityOrigin();
        }
      }
    }
    return {
      securityOrigins,
      mainSecurityOrigin,
      unreachableMainSecurityOrigin
    };
  }
  updateSecurityOrigins() {
    const data = this.getSecurityOriginData();
    this.#securityOriginManager.setMainSecurityOrigin(data.mainSecurityOrigin || "", data.unreachableMainSecurityOrigin || "");
    this.#securityOriginManager.updateSecurityOrigins(data.securityOrigins);
  }
  getMainSecurityOrigin() {
    const data = this.getSecurityOriginData();
    return data.mainSecurityOrigin || data.unreachableMainSecurityOrigin;
  }
  onBackForwardCacheNotUsed(event) {
    if (this.mainFrame && this.mainFrame.id === event.frameId && this.mainFrame.loaderId === event.loaderId) {
      this.mainFrame.setBackForwardCacheDetails(event);
      this.dispatchEventToListeners(Events.BackForwardCacheDetailsUpdated, this.mainFrame);
    } else {
      this.#pendingBackForwardCacheNotUsedEvents.add(event);
    }
  }
  processPendingBackForwardCacheNotUsedEvents(frame) {
    if (!frame.isMainFrame()) {
      return;
    }
    for (const event of this.#pendingBackForwardCacheNotUsedEvents) {
      if (frame.id === event.frameId && frame.loaderId === event.loaderId) {
        frame.setBackForwardCacheDetails(event);
        this.#pendingBackForwardCacheNotUsedEvents.delete(event);
        return;
      }
    }
  }
}
export var Events = /* @__PURE__ */ ((Events2) => {
  Events2["FrameAdded"] = "FrameAdded";
  Events2["FrameNavigated"] = "FrameNavigated";
  Events2["FrameDetached"] = "FrameDetached";
  Events2["FrameResized"] = "FrameResized";
  Events2["FrameWillNavigate"] = "FrameWillNavigate";
  Events2["MainFrameNavigated"] = "MainFrameNavigated";
  Events2["ResourceAdded"] = "ResourceAdded";
  Events2["WillLoadCachedResources"] = "WillLoadCachedResources";
  Events2["CachedResourcesLoaded"] = "CachedResourcesLoaded";
  Events2["DOMContentLoaded"] = "DOMContentLoaded";
  Events2["LifecycleEvent"] = "LifecycleEvent";
  Events2["Load"] = "Load";
  Events2["PageReloadRequested"] = "PageReloadRequested";
  Events2["WillReloadPage"] = "WillReloadPage";
  Events2["InterstitialShown"] = "InterstitialShown";
  Events2["InterstitialHidden"] = "InterstitialHidden";
  Events2["BackForwardCacheDetailsUpdated"] = "BackForwardCacheDetailsUpdated";
  return Events2;
})(Events || {});
export class ResourceTreeFrame {
  #model;
  #sameTargetParentFrameInternal;
  #idInternal;
  crossTargetParentFrameId;
  #loaderIdInternal;
  #nameInternal;
  #urlInternal;
  #domainAndRegistryInternal;
  #securityOriginInternal;
  #unreachableUrlInternal;
  #adFrameStatusInternal;
  #secureContextType;
  #crossOriginIsolatedContextType;
  #gatedAPIFeatures;
  #creationStackTrace;
  #creationStackTraceTarget;
  #childFramesInternal;
  resourcesMap;
  backForwardCacheDetails = {
    restoredFromCache: void 0,
    explanations: [],
    explanationsTree: void 0
  };
  constructor(model, parentFrame, frameId, payload, creationStackTrace) {
    this.#model = model;
    this.#sameTargetParentFrameInternal = parentFrame;
    this.#idInternal = frameId;
    this.crossTargetParentFrameId = null;
    this.#loaderIdInternal = payload && payload.loaderId || "";
    this.#nameInternal = payload && payload.name;
    this.#urlInternal = payload && payload.url || Platform.DevToolsPath.EmptyUrlString;
    this.#domainAndRegistryInternal = payload && payload.domainAndRegistry || "";
    this.#securityOriginInternal = payload && payload.securityOrigin;
    this.#unreachableUrlInternal = payload && payload.unreachableUrl || Platform.DevToolsPath.EmptyUrlString;
    this.#adFrameStatusInternal = payload?.adFrameStatus;
    this.#secureContextType = payload && payload.secureContextType;
    this.#crossOriginIsolatedContextType = payload && payload.crossOriginIsolatedContextType;
    this.#gatedAPIFeatures = payload && payload.gatedAPIFeatures;
    this.#creationStackTrace = creationStackTrace;
    this.#creationStackTraceTarget = null;
    this.#childFramesInternal = /* @__PURE__ */ new Set();
    this.resourcesMap = /* @__PURE__ */ new Map();
    if (this.#sameTargetParentFrameInternal) {
      this.#sameTargetParentFrameInternal.#childFramesInternal.add(this);
    }
  }
  isSecureContext() {
    return this.#secureContextType !== null && this.#secureContextType.startsWith("Secure");
  }
  getSecureContextType() {
    return this.#secureContextType;
  }
  isCrossOriginIsolated() {
    return this.#crossOriginIsolatedContextType !== null && this.#crossOriginIsolatedContextType.startsWith("Isolated");
  }
  getCrossOriginIsolatedContextType() {
    return this.#crossOriginIsolatedContextType;
  }
  getGatedAPIFeatures() {
    return this.#gatedAPIFeatures;
  }
  getCreationStackTraceData() {
    return {
      creationStackTrace: this.#creationStackTrace,
      creationStackTraceTarget: this.#creationStackTraceTarget || this.resourceTreeModel().target()
    };
  }
  navigate(framePayload) {
    this.#loaderIdInternal = framePayload.loaderId;
    this.#nameInternal = framePayload.name;
    this.#urlInternal = framePayload.url;
    this.#domainAndRegistryInternal = framePayload.domainAndRegistry;
    this.#securityOriginInternal = framePayload.securityOrigin;
    this.#unreachableUrlInternal = framePayload.unreachableUrl || Platform.DevToolsPath.EmptyUrlString;
    this.#adFrameStatusInternal = framePayload?.adFrameStatus;
    this.#secureContextType = framePayload.secureContextType;
    this.#crossOriginIsolatedContextType = framePayload.crossOriginIsolatedContextType;
    this.#gatedAPIFeatures = framePayload.gatedAPIFeatures;
    this.backForwardCacheDetails = {
      restoredFromCache: void 0,
      explanations: [],
      explanationsTree: void 0
    };
    const mainResource = this.resourcesMap.get(this.#urlInternal);
    this.resourcesMap.clear();
    this.removeChildFrames();
    if (mainResource && mainResource.loaderId === this.#loaderIdInternal) {
      this.addResource(mainResource);
    }
  }
  resourceTreeModel() {
    return this.#model;
  }
  get id() {
    return this.#idInternal;
  }
  get name() {
    return this.#nameInternal || "";
  }
  get url() {
    return this.#urlInternal;
  }
  domainAndRegistry() {
    return this.#domainAndRegistryInternal;
  }
  get securityOrigin() {
    return this.#securityOriginInternal;
  }
  unreachableUrl() {
    return this.#unreachableUrlInternal;
  }
  get loaderId() {
    return this.#loaderIdInternal;
  }
  adFrameType() {
    return this.#adFrameStatusInternal?.adFrameType || Protocol.Page.AdFrameType.None;
  }
  adFrameStatus() {
    return this.#adFrameStatusInternal;
  }
  get childFrames() {
    return [...this.#childFramesInternal];
  }
  sameTargetParentFrame() {
    return this.#sameTargetParentFrameInternal;
  }
  crossTargetParentFrame() {
    if (!this.crossTargetParentFrameId) {
      return null;
    }
    const parentTarget = this.#model.target().parentTarget();
    if (!parentTarget) {
      return null;
    }
    const parentModel = parentTarget.model(ResourceTreeModel);
    if (!parentModel) {
      return null;
    }
    return parentModel.framesInternal.get(this.crossTargetParentFrameId) || null;
  }
  parentFrame() {
    return this.sameTargetParentFrame() || this.crossTargetParentFrame();
  }
  isMainFrame() {
    return !this.#sameTargetParentFrameInternal;
  }
  isTopFrame() {
    return !this.#model.target().parentTarget() && !this.#sameTargetParentFrameInternal && !this.crossTargetParentFrameId;
  }
  removeChildFrame(frame, isSwap) {
    this.#childFramesInternal.delete(frame);
    frame.remove(isSwap);
  }
  removeChildFrames() {
    const frames = this.#childFramesInternal;
    this.#childFramesInternal = /* @__PURE__ */ new Set();
    for (const frame of frames) {
      frame.remove(false);
    }
  }
  remove(isSwap) {
    this.removeChildFrames();
    this.#model.framesInternal.delete(this.id);
    this.#model.dispatchEventToListeners("FrameDetached" /* FrameDetached */, { frame: this, isSwap });
  }
  addResource(resource) {
    if (this.resourcesMap.get(resource.url) === resource) {
      return;
    }
    this.resourcesMap.set(resource.url, resource);
    this.#model.dispatchEventToListeners("ResourceAdded" /* ResourceAdded */, resource);
  }
  addRequest(request) {
    let resource = this.resourcesMap.get(request.url());
    if (resource && resource.request === request) {
      return;
    }
    resource = new Resource(this.#model, request, request.url(), request.documentURL, request.frameId, request.loaderId, request.resourceType(), request.mimeType, null, null);
    this.resourcesMap.set(resource.url, resource);
    this.#model.dispatchEventToListeners("ResourceAdded" /* ResourceAdded */, resource);
  }
  resources() {
    return Array.from(this.resourcesMap.values());
  }
  resourceForURL(url) {
    const resource = this.resourcesMap.get(url);
    if (resource) {
      return resource;
    }
    for (const frame of this.#childFramesInternal) {
      const resource2 = frame.resourceForURL(url);
      if (resource2) {
        return resource2;
      }
    }
    return null;
  }
  callForFrameResources(callback) {
    for (const resource of this.resourcesMap.values()) {
      if (callback(resource)) {
        return true;
      }
    }
    for (const frame of this.#childFramesInternal) {
      if (frame.callForFrameResources(callback)) {
        return true;
      }
    }
    return false;
  }
  displayName() {
    if (this.isTopFrame()) {
      return i18n.i18n.lockedString("top");
    }
    const subtitle = new Common.ParsedURL.ParsedURL(this.#urlInternal).displayName;
    if (subtitle) {
      if (!this.#nameInternal) {
        return subtitle;
      }
      return this.#nameInternal + " (" + subtitle + ")";
    }
    return i18n.i18n.lockedString("iframe");
  }
  async getOwnerDeferredDOMNode() {
    const parentFrame = this.parentFrame();
    if (!parentFrame) {
      return null;
    }
    return parentFrame.resourceTreeModel().domModel().getOwnerNodeForFrame(this.#idInternal);
  }
  async getOwnerDOMNodeOrDocument() {
    const deferredNode = await this.getOwnerDeferredDOMNode();
    if (deferredNode) {
      return deferredNode.resolvePromise();
    }
    if (this.isTopFrame()) {
      return this.resourceTreeModel().domModel().requestDocument();
    }
    return null;
  }
  async highlight() {
    const parentFrame = this.parentFrame();
    const parentTarget = this.resourceTreeModel().target().parentTarget();
    const highlightFrameOwner = async (domModel) => {
      const deferredNode = await domModel.getOwnerNodeForFrame(this.#idInternal);
      if (deferredNode) {
        domModel.overlayModel().highlightInOverlay({ deferredNode, selectorList: "" }, "all", true);
      }
    };
    if (parentFrame) {
      return highlightFrameOwner(parentFrame.resourceTreeModel().domModel());
    }
    if (parentTarget) {
      const domModel = parentTarget.model(DOMModel);
      if (domModel) {
        return highlightFrameOwner(domModel);
      }
    }
    const document = await this.resourceTreeModel().domModel().requestDocument();
    if (document) {
      this.resourceTreeModel().domModel().overlayModel().highlightInOverlay({ node: document, selectorList: "" }, "all", true);
    }
  }
  async getPermissionsPolicyState() {
    const response = await this.resourceTreeModel().target().pageAgent().invoke_getPermissionsPolicyState({ frameId: this.#idInternal });
    if (response.getError()) {
      return null;
    }
    return response.states;
  }
  async getOriginTrials() {
    const response = await this.resourceTreeModel().target().pageAgent().invoke_getOriginTrials({ frameId: this.#idInternal });
    if (response.getError()) {
      return [];
    }
    return response.originTrials;
  }
  setCreationStackTrace(creationStackTraceData) {
    this.#creationStackTrace = creationStackTraceData.creationStackTrace;
    this.#creationStackTraceTarget = creationStackTraceData.creationStackTraceTarget;
  }
  setBackForwardCacheDetails(event) {
    this.backForwardCacheDetails.restoredFromCache = false;
    this.backForwardCacheDetails.explanations = event.notRestoredExplanations;
    this.backForwardCacheDetails.explanationsTree = event.notRestoredExplanationsTree;
  }
  getResourcesMap() {
    return this.resourcesMap;
  }
}
export class PageDispatcher {
  #resourceTreeModel;
  constructor(resourceTreeModel) {
    this.#resourceTreeModel = resourceTreeModel;
  }
  backForwardCacheNotUsed(params) {
    this.#resourceTreeModel.onBackForwardCacheNotUsed(params);
  }
  domContentEventFired({ timestamp }) {
    this.#resourceTreeModel.dispatchEventToListeners("DOMContentLoaded" /* DOMContentLoaded */, timestamp);
  }
  loadEventFired({ timestamp }) {
    this.#resourceTreeModel.dispatchEventToListeners("Load" /* Load */, { resourceTreeModel: this.#resourceTreeModel, loadTime: timestamp });
  }
  lifecycleEvent({ frameId, name }) {
    this.#resourceTreeModel.dispatchEventToListeners("LifecycleEvent" /* LifecycleEvent */, { frameId, name });
  }
  frameAttached({ frameId, parentFrameId, stack }) {
    this.#resourceTreeModel.frameAttached(frameId, parentFrameId, stack);
  }
  frameNavigated({ frame, type }) {
    this.#resourceTreeModel.frameNavigated(frame, type);
  }
  documentOpened({ frame }) {
    this.#resourceTreeModel.documentOpened(frame);
  }
  frameDetached({ frameId, reason }) {
    this.#resourceTreeModel.frameDetached(frameId, reason === Protocol.Page.FrameDetachedEventReason.Swap);
  }
  frameStartedLoading({}) {
  }
  frameStoppedLoading({}) {
  }
  frameRequestedNavigation({}) {
  }
  frameScheduledNavigation({}) {
  }
  frameClearedScheduledNavigation({}) {
  }
  navigatedWithinDocument({}) {
  }
  frameResized() {
    this.#resourceTreeModel.dispatchEventToListeners("FrameResized" /* FrameResized */);
  }
  javascriptDialogOpening({ hasBrowserHandler }) {
    if (!hasBrowserHandler) {
      void this.#resourceTreeModel.agent.invoke_handleJavaScriptDialog({ accept: false });
    }
  }
  javascriptDialogClosed({}) {
  }
  screencastFrame({}) {
  }
  screencastVisibilityChanged({}) {
  }
  interstitialShown() {
    this.#resourceTreeModel.isInterstitialShowing = true;
    this.#resourceTreeModel.dispatchEventToListeners("InterstitialShown" /* InterstitialShown */);
  }
  interstitialHidden() {
    this.#resourceTreeModel.isInterstitialShowing = false;
    this.#resourceTreeModel.dispatchEventToListeners("InterstitialHidden" /* InterstitialHidden */);
  }
  windowOpen({}) {
  }
  compilationCacheProduced({}) {
  }
  fileChooserOpened({}) {
  }
  downloadWillBegin({}) {
  }
  downloadProgress() {
  }
  prerenderAttemptCompleted({}) {
  }
}
SDKModel.register(ResourceTreeModel, { capabilities: Capability.DOM, autostart: true, early: true });
//# sourceMappingURL=ResourceTreeModel.js.map
