import*as Host from"../host/host.js";import*as Platform from"../platform/platform.js";import*as ARIAUtils from"./ARIAUtils.js";import{html}from"./Fragment.js";import{Tooltip}from"./Tooltip.js";import{addReferrerToURLIfNecessary,copyLinkAddressLabel,MaxLengthForDisplayedURLs,openLinkExternallyLabel}from"./UIUtils.js";import{XElement}from"./XElement.js";export class XLink extends XElement{static create(e,t,n,r){t||(t=e);return html`
        <x-link href='${e}' class='${n=n||""} devtools-link' ${r?"no-click":""}
        >${Platform.StringUtilities.trimMiddle(t,MaxLengthForDisplayedURLs)}</x-link>`}constructor(){super(),this.style.setProperty("display","inline"),ARIAUtils.markAsLink(this),this.tabIndex=0,this.target="_blank",this.rel="noopener",this._href=null,this._clickable=!0,this._onClick=e=>{e.consume(!0),Host.InspectorFrontendHost.InspectorFrontendHostInstance.openInNewTab(this._href)},this._onKeyDown=e=>{isEnterOrSpaceKey(e)&&(e.consume(!0),Host.InspectorFrontendHost.InspectorFrontendHostInstance.openInNewTab(this._href))}}static get observedAttributes(){return XElement.observedAttributes.concat(["href","no-click"])}get href(){return this._href}attributeChangedCallback(e,t,n){if("no-click"===e)return this._clickable=!n,void this._updateClick();if("href"===e){n||(n="");let e=null,t=null;try{t=new URL(addReferrerToURLIfNecessary(n)),e=t.toString()}catch{}return t&&"javascript:"===t.protocol&&(e=null),this._href=e,Tooltip.install(this,n),void this._updateClick()}super.attributeChangedCallback(e,t,n)}_updateClick(){null!==this._href&&this._clickable?(this.addEventListener("click",this._onClick,!1),this.addEventListener("keydown",this._onKeyDown,!1),this.style.setProperty("cursor","pointer")):(this.removeEventListener("click",this._onClick,!1),this.removeEventListener("keydown",this._onKeyDown,!1),this.style.removeProperty("cursor"))}}let contextMenuProviderInstance;export class ContextMenuProvider{static instance(e={forceNew:null}){const{forceNew:t}=e;return contextMenuProviderInstance&&!t||(contextMenuProviderInstance=new ContextMenuProvider),contextMenuProviderInstance}appendApplicableItems(e,t,n){let r=n;for(;r&&!(r instanceof XLink);)r=r.parentNodeOrShadowHost();if(!r||!r._href)return;const o=r;t.revealSection().appendItem(openLinkExternallyLabel(),(()=>{o._href&&Host.InspectorFrontendHost.InspectorFrontendHostInstance.openInNewTab(o._href)})),t.revealSection().appendItem(copyLinkAddressLabel(),(()=>{o._href&&Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText(o._href)}))}}self.customElements.define("x-link",XLink);