import*as i18n from"../i18n/i18n.js";import*as ARIAUtils from"./ARIAUtils.js";import{Keys}from"./KeyboardShortcut.js";import{createTextButton}from"./UIUtils.js";import{createShadowRootWithCoreStyles}from"./utils/create-shadow-root-with-core-styles.js";const UIStrings={dontShowAgain:"Don't show again",learnMore:"Learn more",close:"Close"},str_=i18n.i18n.registerUIStrings("ui/Infobar.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class Infobar{constructor(t,e,i,s){if(this.element=document.createElement("div"),this.element.classList.add("flex-none"),this._shadowRoot=createShadowRootWithCoreStyles(this.element,{cssFile:"ui/infobar.css",enableLegacyPatching:!0,delegatesFocus:void 0}),this._contentElement=this._shadowRoot.createChild("div","infobar infobar-"+t),this._mainRow=this._contentElement.createChild("div","infobar-main-row"),this._detailsRows=this._contentElement.createChild("div","infobar-details-rows hidden"),this._hasDetails=!1,this._infoContainer=this._mainRow.createChild("div","infobar-info-container"),this._infoMessage=this._infoContainer.createChild("div","infobar-info-message"),this._infoMessage.createChild("div",t+"-icon icon"),this._infoText=this._infoMessage.createChild("div","infobar-info-text"),this._infoText.textContent=e,ARIAUtils.markAsAlert(this._infoText),this._actionContainer=this._infoContainer.createChild("div","infobar-info-actions"),i)for(const n of i){const t=this._actionCallbackFactory(n);let e="infobar-button";n.highlight&&(e+=" primary-button");const i=createTextButton(n.text,t,e);this._actionContainer.appendChild(i)}if(this._disableSetting=s||null,s){const t=createTextButton(i18nString(UIStrings.dontShowAgain),this._onDisable.bind(this),"infobar-button");this._actionContainer.appendChild(t)}this._closeContainer=this._mainRow.createChild("div","infobar-close-container"),this._toggleElement=createTextButton(i18nString(UIStrings.learnMore),this._onToggleDetails.bind(this),"link-style devtools-link hidden"),this._closeContainer.appendChild(this._toggleElement),this._closeButton=this._closeContainer.createChild("div","close-button","dt-close-button"),this._closeButton.setTabbable(!0),ARIAUtils.setDescription(this._closeButton,i18nString(UIStrings.close)),self.onInvokeElement(this._closeButton,this.dispose.bind(this)),t!==Type.Issue&&(this._contentElement.tabIndex=0),ARIAUtils.setAccessibleName(this._contentElement,e),this._contentElement.addEventListener("keydown",(t=>t.keyCode===Keys.Esc.code?(this.dispose(),void t.consume()):t.target===this._contentElement&&"Enter"===t.key&&this._hasDetails?(this._onToggleDetails(),void t.consume()):void 0)),this._closeCallback=null}static create(t,e,i,s){return s&&s.get()?null:new Infobar(t,e,i,s)}dispose(){this.element.remove(),this._onResize(),this._closeCallback&&this._closeCallback.call(null)}setText(t){this._infoText.textContent=t,this._onResize()}setCloseCallback(t){this._closeCallback=t}setParentView(t){this._parentView=t}_actionCallbackFactory(t){return t.delegate?t.dismiss?(()=>{t.delegate&&t.delegate(),this.dispose()}).bind(this):t.delegate:t.dismiss?this.dispose.bind(this):()=>{}}_onResize(){this._parentView&&this._parentView.doResize()}_onDisable(){this._disableSetting&&this._disableSetting.set(!0),this.dispose()}_onToggleDetails(){this._detailsRows.classList.remove("hidden"),this._toggleElement.remove(),this._onResize()}createDetailsRowMessage(t){this._hasDetails=!0,this._toggleElement.classList.remove("hidden");const e=this._detailsRows.createChild("div","infobar-details-row").createChild("span","infobar-row-message");return e.textContent=t||"",e}}export let InfobarAction;export const Type={Warning:"warning",Info:"info",Issue:"issue"};