import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{ObjectPropertiesSection}from"./ObjectPropertiesSection.js";const UIStrings={showAsJavascriptObject:"Show as JavaScript object"},str_=i18n.i18n.registerUIStrings("object_ui/CustomPreviewComponent.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class CustomPreviewSection{constructor(e){this._sectionElement=document.createElement("span"),this._sectionElement.classList.add("custom-expandable-section"),this._object=e,this._expanded=!1,this._cachedContent=null;const t=e.customPreview();if(!t)return;let n;try{n=JSON.parse(t.header)}catch(s){return void Common.Console.Console.instance().error("Broken formatter: header is invalid json "+s)}this._header=this._renderJSONMLTag(n),this._header.nodeType!==Node.TEXT_NODE?(t.bodyGetterId&&(this._header instanceof Element&&this._header.classList.add("custom-expandable-section-header"),this._header.addEventListener("click",this._onClick.bind(this),!1),this._expandIcon=UI.Icon.Icon.create("smallicon-triangle-right","custom-expand-icon"),this._header.insertBefore(this._expandIcon,this._header.firstChild)),this._sectionElement.appendChild(this._header)):Common.Console.Console.instance().error("Broken formatter: header should be an element node.")}element(){return this._sectionElement}_renderJSONMLTag(e){if(!Array.isArray(e))return document.createTextNode(String(e));const t=e;return"object"===t[0]?this._layoutObjectTag(t):this._renderElement(t)}_renderElement(e){const t=e.shift();if(!CustomPreviewSection._allowedTags.has(t))return Common.Console.Console.instance().error("Broken formatter: element "+t+" is not allowed!"),document.createElement("span");const n=document.createElement(t);if("object"===typeof e[0]&&!Array.isArray(e[0])){const t=e.shift();for(const e in t){const s=t[e];"style"===e&&"string"===typeof s&&n.setAttribute(e,s)}}return this._appendJsonMLTags(n,e),n}_layoutObjectTag(e){e.shift();const t=e.shift(),n=this._object.runtimeModel().createRemoteObject(t);if(n.customPreview())return new CustomPreviewSection(n).element();const s=ObjectPropertiesSection.defaultObjectPresentation(n);return s.classList.toggle("custom-expandable-section-standard-section",n.hasChildren),s}_appendJsonMLTags(e,t){for(let n=0;n<t.length;++n)e.appendChild(this._renderJSONMLTag(t[n]))}_onClick(e){e.consume(!0),this._cachedContent?this._toggleExpand():this._loadBody()}_toggleExpand(){this._expanded=!this._expanded,this._header instanceof Element&&this._header.classList.toggle("expanded",this._expanded),this._cachedContent instanceof Element&&this._cachedContent.classList.toggle("hidden",!this._expanded),this._expandIcon&&(this._expanded?this._expandIcon.setIconType("smallicon-triangle-down"):this._expandIcon.setIconType("smallicon-triangle-right"))}async _loadBody(){const e=this._object.customPreview();if(e&&e.bodyGetterId){const t=await this._object.callFunctionJSON((e=>e()),[{objectId:e.bodyGetterId}]);if(!t)return;this._cachedContent=this._renderJSONMLTag(t),this._sectionElement.appendChild(this._cachedContent),this._toggleExpand()}}}CustomPreviewSection._allowedTags=new Set(["span","div","ol","li","table","tr","td"]);export class CustomPreviewComponent{constructor(e){this._object=e,this._customPreviewSection=new CustomPreviewSection(e),this.element=document.createElement("span"),this.element.classList.add("source-code");const t=UI.Utils.createShadowRootWithCoreStyles(this.element,{cssFile:"object_ui/customPreviewComponent.css",enableLegacyPatching:!1,delegatesFocus:void 0});this.element.addEventListener("contextmenu",this._contextMenuEventFired.bind(this),!1),t.appendChild(this._customPreviewSection.element())}expandIfPossible(){const e=this._object.customPreview();e&&e.bodyGetterId&&this._customPreviewSection&&this._customPreviewSection._loadBody()}_contextMenuEventFired(e){const t=new UI.ContextMenu.ContextMenu(e);this._customPreviewSection&&t.revealSection().appendItem(i18nString(UIStrings.showAsJavascriptObject),this._disassemble.bind(this)),t.appendApplicableItems(this._object),t.show()}_disassemble(){this.element.shadowRoot&&(this.element.shadowRoot.textContent="",this._customPreviewSection=null,this.element.shadowRoot.appendChild(ObjectPropertiesSection.defaultObjectPresentation(this._object)))}}