import{Modal as e}from"./chunk.QVUQ4TNC.js";import{lockBodyScrolling as t,unlockBodyScrolling as i}from"./chunk.RK73WSZS.js";import{dialog_styles_default as o}from"./chunk.INRC77DZ.js";import{SlIconButton as s}from"./chunk.LOVM4XL6.js";import{getAnimation as a,setDefaultAnimation as l}from"./chunk.STXFIMXZ.js";import{waitForEvent as r}from"./chunk.B4BZKR24.js";import{animateTo as n,stopAnimations as d}from"./chunk.W5IOVX7A.js";import{LocalizeController as h}from"./chunk.2A3352EO.js";import{HasSlotController as c}from"./chunk.NYIIDP5N.js";import{watch as p}from"./chunk.C7FWPEOY.js";import{ShoelaceElement as m}from"./chunk.Z7XDKKOD.js";import{__decorateClass as u}from"./chunk.6M63UXML.js";import{classMap as f}from"../../../../lit/directives/class-map.js";import{html as y}from"../../../../lit/index.js";import{ifDefined as g}from"../../../../lit/directives/if-defined.js";import{property as v,query as b}from"../../../../lit/decorators.js";var k=class extends m{constructor(){super(...arguments),this.hasSlotController=new c(this,"footer"),this.localize=new h(this),this.modal=new e(this),this.open=!1,this.label="",this.noHeader=!1,this.handleDocumentKeyDown=e=>{"Escape"===e.key&&this.modal.isActive()&&this.open&&(e.stopPropagation(),this.requestClose("keyboard"))}}firstUpdated(){this.dialog.hidden=!this.open,this.open&&(this.addOpenListeners(),this.modal.activate(),t(this))}disconnectedCallback(){super.disconnectedCallback(),this.modal.deactivate(),i(this)}requestClose(e){if(this.emit("sl-request-close",{cancelable:!0,detail:{source:e}}).defaultPrevented){const e=a(this,"dialog.denyClose",{dir:this.localize.dir()});n(this.panel,e.keyframes,e.options)}else this.hide()}addOpenListeners(){document.addEventListener("keydown",this.handleDocumentKeyDown)}removeOpenListeners(){document.removeEventListener("keydown",this.handleDocumentKeyDown)}async handleOpenChange(){if(this.open){this.emit("sl-show"),this.addOpenListeners(),this.originalTrigger=document.activeElement,this.modal.activate(),t(this);const e=this.querySelector("[autofocus]");e&&e.removeAttribute("autofocus"),await Promise.all([d(this.dialog),d(this.overlay)]),this.dialog.hidden=!1,requestAnimationFrame((()=>{this.emit("sl-initial-focus",{cancelable:!0}).defaultPrevented||(e?e.focus({preventScroll:!0}):this.panel.focus({preventScroll:!0})),e&&e.setAttribute("autofocus","")}));const i=a(this,"dialog.show",{dir:this.localize.dir()}),o=a(this,"dialog.overlay.show",{dir:this.localize.dir()});await Promise.all([n(this.panel,i.keyframes,i.options),n(this.overlay,o.keyframes,o.options)]),this.emit("sl-after-show")}else{this.emit("sl-hide"),this.removeOpenListeners(),this.modal.deactivate(),await Promise.all([d(this.dialog),d(this.overlay)]);const e=a(this,"dialog.hide",{dir:this.localize.dir()}),t=a(this,"dialog.overlay.hide",{dir:this.localize.dir()});await Promise.all([n(this.overlay,t.keyframes,t.options).then((()=>{this.overlay.hidden=!0})),n(this.panel,e.keyframes,e.options).then((()=>{this.panel.hidden=!0}))]),this.dialog.hidden=!0,this.overlay.hidden=!1,this.panel.hidden=!1,i(this);const o=this.originalTrigger;"function"==typeof(null==o?void 0:o.focus)&&setTimeout((()=>o.focus())),this.emit("sl-after-hide")}}async show(){if(!this.open)return this.open=!0,r(this,"sl-after-show")}async hide(){if(this.open)return this.open=!1,r(this,"sl-after-hide")}render(){return y`
      <div
        part="base"
        class=${f({dialog:!0,"dialog--open":this.open,"dialog--has-footer":this.hasSlotController.test("footer")})}
      >
        <div part="overlay" class="dialog__overlay" @click=${()=>this.requestClose("overlay")} tabindex="-1"></div>

        <div
          part="panel"
          class="dialog__panel"
          role="dialog"
          aria-modal="true"
          aria-hidden=${this.open?"false":"true"}
          aria-label=${g(this.noHeader?this.label:void 0)}
          aria-labelledby=${g(this.noHeader?void 0:"title")}
          tabindex="-1"
        >
          ${this.noHeader?"":y`
                <header part="header" class="dialog__header">
                  <h2 part="title" class="dialog__title" id="title">
                    <slot name="label"> ${this.label.length>0?this.label:String.fromCharCode(65279)} </slot>
                  </h2>
                  <div part="header-actions" class="dialog__header-actions">
                    <slot name="header-actions"></slot>
                    <sl-icon-button
                      part="close-button"
                      exportparts="base:close-button__base"
                      class="dialog__close"
                      name="x-lg"
                      label=${this.localize.term("close")}
                      library="system"
                      @click="${()=>this.requestClose("close-button")}"
                    ></sl-icon-button>
                  </div>
                </header>
              `}
          ${""}
          <slot part="body" class="dialog__body" tabindex="-1"></slot>

          <footer part="footer" class="dialog__footer">
            <slot name="footer"></slot>
          </footer>
        </div>
      </div>
    `}};k.styles=o,k.dependencies={"sl-icon-button":s},u([b(".dialog")],k.prototype,"dialog",2),u([b(".dialog__panel")],k.prototype,"panel",2),u([b(".dialog__overlay")],k.prototype,"overlay",2),u([v({type:Boolean,reflect:!0})],k.prototype,"open",2),u([v({reflect:!0})],k.prototype,"label",2),u([v({attribute:"no-header",type:Boolean,reflect:!0})],k.prototype,"noHeader",2),u([p("open",{waitUntilFirstUpdate:!0})],k.prototype,"handleOpenChange",1),l("dialog.show",{keyframes:[{opacity:0,scale:.8},{opacity:1,scale:1}],options:{duration:250,easing:"ease"}}),l("dialog.hide",{keyframes:[{opacity:1,scale:1},{opacity:0,scale:.8}],options:{duration:250,easing:"ease"}}),l("dialog.denyClose",{keyframes:[{scale:1},{scale:1.02},{scale:1}],options:{duration:250}}),l("dialog.overlay.show",{keyframes:[{opacity:0},{opacity:1}],options:{duration:250}}),l("dialog.overlay.hide",{keyframes:[{opacity:1},{opacity:0}],options:{duration:250}});export{k as SlDialog};