import*as Components from"../components/components.js";import*as Platform from"../platform/platform.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import{CustomPreviewComponent}from"./CustomPreviewComponent.js";import{ObjectPropertiesSection}from"./ObjectPropertiesSection.js";export class ObjectPopoverHelper{constructor(e,t){this._linkifier=e,this._resultHighlightedAsDOM=t}dispose(){this._resultHighlightedAsDOM&&SDK.OverlayModel.OverlayModel.hideDOMNodeHighlight(),this._linkifier&&this._linkifier.dispose()}static async buildObjectPopover(e,t){const o=Platform.StringUtilities.trimEndWithMaxLength(e.description||"",MaxPopoverTextLength);let n=null;if("object"===e.type){let i=null,s=!1;if("node"===e.subtype&&(SDK.OverlayModel.OverlayModel.highlightObjectAsDOMNode(e),s=!0),e.customPreview()){const t=new CustomPreviewComponent(e);t.expandIfPossible(),n=t.element}else{n=document.createElement("div"),n.classList.add("object-popover-content"),UI.Utils.appendStyle(n,"object_ui/objectPopover.css",{enableLegacyPatching:!0});n.createChild("div","monospace object-popover-title").createChild("span").textContent=o,i=new Components.Linkifier.Linkifier;const t=new ObjectPropertiesSection(e,"",i,void 0,void 0,void 0,!0);t.element.classList.add("object-popover-tree"),t.titleLessMode(),n.appendChild(t.element)}return n.dataset.stableNameForTest="object-popover-content",t.setMaxContentSize(new UI.Geometry.Size(300,250)),t.setSizeBehavior(UI.GlassPane.SizeBehavior.SetExactSize),t.contentElement.appendChild(n),new ObjectPopoverHelper(i,s)}n=document.createElement("span"),n.dataset.stableNameForTest="object-popover-content",UI.Utils.appendStyle(n,"object_ui/objectValue.css",{enableLegacyPatching:!0}),UI.Utils.appendStyle(n,"object_ui/objectPopover.css",{enableLegacyPatching:!0});const i=n.createChild("span","monospace object-value-"+e.type);if(i.style.whiteSpace="pre","string"===e.type?UI.UIUtils.createTextChildren(i,`"${o}"`):"function"!==e.type&&(i.textContent=o),"function"!==e.type)return t.contentElement.appendChild(n),new ObjectPopoverHelper(null,!1);ObjectPropertiesSection.formatObjectAsFunction(e,i,!0);const s=await e.debuggerModel().functionDetailsPromise(e);if(!s)return null;const r=document.createElement("div");r.classList.add("object-popover-container");const c=r.createChild("div","function-popover-title source-code");c.createChild("span","function-name").textContent=UI.UIUtils.beautifyFunctionName(s.functionName);const l=s.location,a=c.createChild("div","function-title-link-container"),p=l&&l.script(),d=p&&p.sourceURL;let m=null;return d&&(m=new Components.Linkifier.Linkifier(void 0,void 0,t.positionContent.bind(t)),a.appendChild(m.linkifyRawLocation(l,d))),r.appendChild(n),t.contentElement.appendChild(r),new ObjectPopoverHelper(m,!1)}}const MaxPopoverTextLength=1e4;