import*as Host from"../../core/host/host.js";import*as Platform from"../../core/platform/platform.js";import*as ComponentHelpers from"../components/helpers/helpers.js";import*as LitHtml from"../lit-html/lit-html.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{hrefInternal;clickable;onClick;onKeyDown;static create(e,t,n,r){t||(t=e);return html`
  <x-link href='${e}' tabindex="0" 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.setAttribute("tabindex","0"),this.setAttribute("target","_blank"),this.setAttribute("rel","noopener"),this.hrefInternal=null,this.clickable=!0,this.onClick=e=>{e.consume(!0),this.hrefInternal&&Host.InspectorFrontendHost.InspectorFrontendHostInstance.openInNewTab(this.hrefInternal),this.dispatchEvent(new Event("x-link-invoke"))},this.onKeyDown=e=>{Platform.KeyboardUtilities.isEnterOrSpaceKey(e)&&(e.consume(!0),this.hrefInternal&&Host.InspectorFrontendHost.InspectorFrontendHostInstance.openInNewTab(this.hrefInternal)),this.dispatchEvent(new Event("x-link-invoke"))}}static get observedAttributes(){return XElement.observedAttributes.concat(["href","no-click"])}get href(){return this.hrefInternal}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.hrefInternal=e,Tooltip.install(this,n),void this.updateClick()}super.attributeChangedCallback(e,t,n)}updateClick(){null!==this.hrefInternal&&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)}))}}ComponentHelpers.CustomElements.defineComponent("x-link",XLink);export const sample=LitHtml.html`<p>Hello, <x-link>world!</x-link></p>`;