import*as Common from"../common/common.js";import*as Host from"../host/host.js";import*as i18n from"../i18n/i18n.js";import*as SourceFrame from"../source_frame/source_frame.js";import*as UI from"../ui/ui.js";const UIStrings={base:"`Base64`",copiedAsBase:"Copied as `Base64`",hexViewer:"`Hex` Viewer",copiedAsHex:"Copied as `Hex`",utf:"`UTF-8`",copiedAsUtf:"Copied as `UTF-8`",binaryViewType:"Binary view type",copyToClipboard:"Copy to clipboard",copyAsBase:"Copy as `Base64`",copyAsHex:"Copy as `Hex`",copyAsUtf:"Copy as `UTF-8`"},str_=i18n.i18n.registerUIStrings("network/BinaryResourceView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class BinaryResourceView extends UI.Widget.VBox{constructor(e,t,i){super(),this.registerRequiredCSS("network/binaryResourceView.css",{enableLegacyPatching:!1}),this._binaryResourceViewFactory=new SourceFrame.BinaryResourceViewFactory.BinaryResourceViewFactory(e,t,i),this._toolbar=new UI.Toolbar.Toolbar("binary-view-toolbar",this.element),this._binaryViewObjects=[new BinaryViewObject("base64",i18nString(UIStrings.base),i18nString(UIStrings.copiedAsBase),this._binaryResourceViewFactory.createBase64View.bind(this._binaryResourceViewFactory),this._binaryResourceViewFactory.base64.bind(this._binaryResourceViewFactory)),new BinaryViewObject("hex",i18nString(UIStrings.hexViewer),i18nString(UIStrings.copiedAsHex),this._binaryResourceViewFactory.createHexView.bind(this._binaryResourceViewFactory),this._binaryResourceViewFactory.hex.bind(this._binaryResourceViewFactory)),new BinaryViewObject("utf8",i18nString(UIStrings.utf),i18nString(UIStrings.copiedAsUtf),this._binaryResourceViewFactory.createUtf8View.bind(this._binaryResourceViewFactory),this._binaryResourceViewFactory.utf8.bind(this._binaryResourceViewFactory))],this._binaryViewTypeSetting=Common.Settings.Settings.instance().createSetting("binaryViewType","hex"),this._binaryViewTypeCombobox=new UI.Toolbar.ToolbarComboBox(this._binaryViewTypeChanged.bind(this),i18nString(UIStrings.binaryViewType));for(const n of this._binaryViewObjects)this._binaryViewTypeCombobox.addOption(this._binaryViewTypeCombobox.createOption(n.label,n.type));this._toolbar.appendToolbarItem(this._binaryViewTypeCombobox);const o=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.copyToClipboard),"largeicon-copy");o.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,(e=>{this._copySelectedViewToClipboard()}),this),this._toolbar.appendToolbarItem(o),this._copiedText=new UI.Toolbar.ToolbarText,this._copiedText.element.classList.add("binary-view-copied-text"),this._toolbar.element.appendChild(this._copiedText.element),this._addFadeoutSettimeoutId=null,this._lastView=null,this._updateView()}_getCurrentViewObject(){const e=this._binaryViewObjects.find((e=>e.type===this._binaryViewTypeSetting.get()));return console.assert(Boolean(e),`No binary view found for binary view type found in setting 'binaryViewType': ${this._binaryViewTypeSetting.get()}`),e||null}async _copySelectedViewToClipboard(){const e=this._getCurrentViewObject();e&&(Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText((await e.content()).content),this._copiedText.setText(e.copiedMessage),this._copiedText.element.classList.remove("fadeout"),this._addFadeoutSettimeoutId&&(clearTimeout(this._addFadeoutSettimeoutId),this._addFadeoutSettimeoutId=null),this._addFadeoutSettimeoutId=window.setTimeout(function(){this._copiedText.element.classList.add("fadeout")}.bind(this),2e3))}wasShown(){this._updateView()}_updateView(){const e=this._getCurrentViewObject();if(!e)return;const t=e.getView();t!==this._lastView&&(this._lastView&&this._lastView.detach(),this._lastView=t,t.show(this.element,this._toolbar.element),this._binaryViewTypeCombobox.selectElement().value=this._binaryViewTypeSetting.get())}_binaryViewTypeChanged(){const e=this._binaryViewTypeCombobox.selectedOption();if(!e)return;const t=e.value;this._binaryViewTypeSetting.get()!==t&&(this._binaryViewTypeSetting.set(t),this._updateView())}addCopyToContextMenu(e,t){const i=e.clipboardSection().appendSubMenuItem(t).footerSection();i.appendItem(i18nString(UIStrings.copyAsBase),(async()=>{const e=await this._binaryResourceViewFactory.base64();Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText(e.content)})),i.appendItem(i18nString(UIStrings.copyAsHex),(async()=>{const e=await this._binaryResourceViewFactory.hex();Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText(e.content)})),i.appendItem(i18nString(UIStrings.copyAsUtf),(async()=>{const e=await this._binaryResourceViewFactory.utf8();Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText(e.content)}))}}export class BinaryViewObject{constructor(e,t,i,o,n){this.type=e,this.label=t,this.copiedMessage=i,this.content=n,this._createViewFn=o,this._view=null}getView(){return this._view||(this._view=this._createViewFn()),this._view}}