import*as Host from"../host/host.js";import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";const UIStrings={nodejsDebuggingGuide:"Node.js debugging guide",specifyNetworkEndpointAnd:"Specify network endpoint and DevTools will connect to it automatically. Read {PH1} to learn more.",noConnectionsSpecified:"No connections specified",addConnection:"Add connection",networkAddressEgLocalhost:"Network address (e.g. localhost:9229)"},str_=i18n.i18n.registerUIStrings("node_main/NodeConnectionsPanel.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let nodeConnectionsPanelInstance;export class NodeConnectionsPanel extends UI.Panel.Panel{constructor(){super("node-connection"),this.registerRequiredCSS("node_main/nodeConnectionsPanel.css",{enableLegacyPatching:!1}),this.contentElement.classList.add("node-panel");const e=this.contentElement.createChild("div","node-panel-center");e.createChild("img","node-panel-logo").src="https://nodejs.org/static/images/logos/nodejs-new-pantone-black.svg",Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(Host.InspectorFrontendHostAPI.Events.DevicesDiscoveryConfigChanged,this._devicesDiscoveryConfigChanged,this),this.contentElement.tabIndex=0,this.setDefaultFocusedElement(this.contentElement),Host.InspectorFrontendHost.InspectorFrontendHostInstance.setDevicesUpdatesEnabled(!1),Host.InspectorFrontendHost.InspectorFrontendHostInstance.setDevicesUpdatesEnabled(!0),this._networkDiscoveryView=new NodeConnectionsView((e=>{this._config.networkDiscoveryConfig=e,Host.InspectorFrontendHost.InspectorFrontendHostInstance.setDevicesDiscoveryConfig(this._config)})),this._networkDiscoveryView.show(e)}static instance(e={forceNew:null}){const{forceNew:t}=e;return nodeConnectionsPanelInstance&&!t||(nodeConnectionsPanelInstance=new NodeConnectionsPanel),nodeConnectionsPanelInstance}_devicesDiscoveryConfigChanged(e){this._config=e.data,this._networkDiscoveryView.discoveryConfigChanged(this._config.networkDiscoveryConfig)}}export class NodeConnectionsView extends UI.Widget.VBox{constructor(e){super(),this._callback=e,this.element.classList.add("network-discovery-view");const t=this.element.createChild("div","network-discovery-footer"),n=UI.XLink.XLink.create("https://nodejs.org/en/docs/inspector/",i18nString(UIStrings.nodejsDebuggingGuide));t.appendChild(i18n.i18n.getFormatLocalizedString(str_,UIStrings.specifyNetworkEndpointAnd,{PH1:n})),this._list=new UI.ListWidget.ListWidget(this),this._list.registerRequiredCSS("node_main/nodeConnectionsPanel.css",{enableLegacyPatching:!1}),this._list.element.classList.add("network-discovery-list");const s=document.createElement("div");s.classList.add("network-discovery-list-empty"),s.textContent=i18nString(UIStrings.noConnectionsSpecified),this._list.setEmptyPlaceholder(s),this._list.show(this.element),this._editor=null;const o=UI.UIUtils.createTextButton(i18nString(UIStrings.addConnection),this._addNetworkTargetButtonClicked.bind(this),"add-network-target-button",!0);this.element.appendChild(o),this._networkDiscoveryConfig=[],this.element.classList.add("node-frontend")}_update(){const e=this._networkDiscoveryConfig.map((e=>e.address));this._callback.call(null,e)}_addNetworkTargetButtonClicked(){this._list.addNewItem(this._networkDiscoveryConfig.length,{address:"",port:""})}discoveryConfigChanged(e){this._networkDiscoveryConfig=[],this._list.clear();for(const t of e){const e={address:t,port:""};this._networkDiscoveryConfig.push(e),this._list.appendItem(e,!0)}}renderItem(e,t){const n=document.createElement("div");return n.classList.add("network-discovery-list-item"),n.createChild("div","network-discovery-value network-discovery-address").textContent=e.address,n}removeItemRequested(e,t){this._networkDiscoveryConfig.splice(t,1),this._list.removeItem(t),this._update()}commitEdit(e,t,n){e.address=t.control("address").value.trim(),n&&this._networkDiscoveryConfig.push(e),this._update()}beginEdit(e){const t=this._createEditor();return t.control("address").value=e.address,t}_createEditor(){if(this._editor)return this._editor;const e=new UI.ListWidget.Editor;this._editor=e;const t=e.contentElement().createChild("div","network-discovery-edit-row"),n=e.createInput("address","text",i18nString(UIStrings.networkAddressEgLocalhost),(function(e,t,n){const s=n.value.trim().match(/^([a-zA-Z0-9\.\-_]+):(\d+)$/);if(!s)return{valid:!1,errorMessage:void 0};return{valid:parseInt(s[2],10)<=65535,errorMessage:void 0}}));return t.createChild("div","network-discovery-value network-discovery-address").appendChild(n),e}}