'use client';
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var lit = require('lit');
var decorators_js = require('lit/decorators.js');
var classMap_js = require('lit/directives/class-map.js');
var sgdsElement = require('../../base/sgds-element.cjs.js');
var animate = require('../../utils/animate.cjs.js');
var animationRegistry = require('../../utils/animation-registry.cjs.js');
var event = require('../../utils/event.cjs.js');
var scroll = require('../../utils/scroll.cjs.js');
var watch = require('../../utils/watch.cjs.js');
var drawer = require('./drawer.cjs.js');
var sgdsCloseButton = require('../CloseButton/sgds-close-button.cjs.js');

/**
 * @summary Drawers slide in from a container to expose additional options and information.
 *
 * @slot default - The drawer's main content
 * @slot title - The title of the drawer
 * @slot description - The description of the drawer
 * @slot footer - The footer of the drawer
 *
 * @event sgds-show - Emitted when the drawer opens.
 * @event sgds-after-show - Emitted after the drawer opens and all animations are complete.
 * @event sgds-hide - Emitted when the drawer closes.
 * @event sgds-after-hide - Emitted after the drawer closes and all animations are complete.
 * @event sgds-initial-focus - Emitted when the drawer opens and is ready to receive focus. Calling
 *   `event.preventDefault()` will prevent focusing and allow you to set it on a different element, such as an input.
 * @event {{ source: 'close-button' | 'keyboard' | 'overlay' }} sgds-request-close - Emitted when the user attempts to
 *   close the drawer by clicking the close button, clicking the overlay, or pressing escape. Calling
 *   `event.preventDefault()` will keep the drawer open. Avoid using this unless closing the drawer will result in
 *   destructive behavior such as data loss.
 *
 */
class SgdsDrawer extends sgdsElement["default"] {
    constructor() {
        super(...arguments);
        /**
         * Indicates whether or not the drawer is open. You can toggle this attribute to show and hide the drawer, or you can
         * use the `show()` and `hide()` methods and this attribute will reflect the drawer's open state.
         */
        this.open = false;
        /**
         * Defines the size of the drawer panel.
         * For drawers placed on the left or right (`start`/`end`), this controls the drawer's width.
         * For drawers placed on the top or bottom, this controls the drawer's height.
         * Accepts `small`, `medium`, or `large`. Defaults to `small`.
         */
        this.size = "sm";
        /** The direction from which the drawer will open. */
        this.placement = "end";
        /**
         * By default, the drawer slides out of its containing block (usually the viewport). To make the drawer slide out of
         * its parent element, set this attribute and add `position: relative` to the parent.
         */
        this.contained = false;
        this.handleDocumentKeyDown = (event) => {
            if (this.open && !this.contained && event.key === "Escape") {
                event.stopPropagation();
                this.requestClose("keyboard");
            }
        };
    }
    firstUpdated(changedProperties) {
        super.firstUpdated(changedProperties);
        if (this.open) {
            this.addOpenListeners();
            if (!this.contained) {
                scroll.lockBodyScrolling(this);
            }
        }
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        scroll.unlockBodyScrolling(this);
    }
    uppercaseFirstLetter(string) {
        return string.charAt(0).toUpperCase() + string.slice(1);
    }
    requestClose(source) {
        const slRequestClose = this.emit("sgds-request-close", {
            cancelable: true,
            detail: { source }
        });
        if (slRequestClose.defaultPrevented) {
            const animation = animationRegistry.getAnimation(this, "drawer.denyClose");
            animate.animateTo(this.panel, animation.keyframes, animation.options);
            return;
        }
        this.hide();
    }
    addOpenListeners() {
        document.addEventListener("keydown", this.handleDocumentKeyDown);
    }
    removeOpenListeners() {
        document.removeEventListener("keydown", this.handleDocumentKeyDown);
    }
    async handleOpenChange() {
        if (this.open) {
            // Show
            this.emit("sgds-show");
            this.addOpenListeners();
            this.originalTrigger = document.activeElement;
            // Lock body scrolling only if the drawer isn't contained
            if (!this.contained) {
                scroll.lockBodyScrolling(this);
            }
            // When the drawer is shown, Safari will attempt to set focus on whatever element has autofocus. This causes the
            // drawer's animation to jitter, so we'll temporarily remove the attribute, call `focus({ preventScroll: true })`
            // ourselves, and add the attribute back afterwards.
            //
            // Related: https://github.com/shoelace-style/shoelace/issues/693
            //
            const autoFocusTarget = this.querySelector("[autofocus]");
            if (autoFocusTarget) {
                autoFocusTarget.removeAttribute("autofocus");
            }
            await Promise.all([animate.stopAnimations(this.drawer), animate.stopAnimations(this.overlay)]);
            this.drawer.hidden = false;
            // Set initial focus
            requestAnimationFrame(() => {
                const slInitialFocus = this.emit("sgds-initial-focus", { cancelable: true });
                if (!slInitialFocus.defaultPrevented) {
                    // Set focus to the autofocus target and restore the attribute
                    if (autoFocusTarget) {
                        autoFocusTarget.focus({ preventScroll: true });
                    }
                    else {
                        this.panel.focus({ preventScroll: true });
                    }
                }
                // Restore the autofocus attribute
                if (autoFocusTarget) {
                    autoFocusTarget.setAttribute("autofocus", "");
                }
            });
            const panelAnimation = animationRegistry.getAnimation(this, `drawer.show${this.uppercaseFirstLetter(this.placement)}`);
            const overlayAnimation = animationRegistry.getAnimation(this, "drawer.overlay.show");
            await Promise.all([
                animate.animateTo(this.panel, panelAnimation.keyframes, panelAnimation.options),
                animate.animateTo(this.overlay, overlayAnimation.keyframes, overlayAnimation.options)
            ]);
            this.emit("sgds-after-show");
        }
        else {
            // Hide
            this.emit("sgds-hide");
            this.removeOpenListeners();
            if (!this.contained) {
                scroll.unlockBodyScrolling(this);
            }
            await Promise.all([animate.stopAnimations(this.drawer), animate.stopAnimations(this.overlay)]);
            const panelAnimation = animationRegistry.getAnimation(this, `drawer.hide${this.uppercaseFirstLetter(this.placement)}`);
            const overlayAnimation = animationRegistry.getAnimation(this, "drawer.overlay.hide");
            // Animate the overlay and the panel at the same time. Because animation durations might be different, we need to
            // hide each one individually when the animation finishes, otherwise the first one that finishes will reappear
            // unexpectedly. We'll unhide them after all animations have completed.
            await Promise.all([
                animate.animateTo(this.overlay, overlayAnimation.keyframes, overlayAnimation.options).then(() => {
                    this.overlay.hidden = true;
                }),
                animate.animateTo(this.panel, panelAnimation.keyframes, panelAnimation.options).then(() => {
                    this.panel.hidden = true;
                })
            ]);
            this.drawer.hidden = true;
            // Now that the dialog is hidden, restore the overlay and panel for next time
            this.overlay.hidden = false;
            this.panel.hidden = false;
            // Restore focus to the original trigger
            const trigger = this.originalTrigger;
            if (typeof (trigger === null || trigger === void 0 ? void 0 : trigger.focus) === "function") {
                setTimeout(() => trigger.focus());
            }
            this.emit("sgds-after-hide");
        }
    }
    handleNoModalChange() {
        if (this.open && !this.contained) {
            scroll.lockBodyScrolling(this);
        }
        if (this.open && this.contained) {
            scroll.unlockBodyScrolling(this);
        }
    }
    /** Shows the drawer. */
    async show() {
        if (this.open) {
            return undefined;
        }
        this.open = true;
        return event.waitForEvent(this, "sgds-after-show");
    }
    /** Hides the drawer */
    async hide() {
        if (!this.open) {
            return undefined;
        }
        this.open = false;
        return event.waitForEvent(this, "sgds-after-hide");
    }
    render() {
        const isHydrated = this.hasUpdated;
        const shouldHide = !this.open && !isHydrated;
        return lit.html `
      <div
        class=${classMap_js.classMap({
            drawer: true,
            "drawer-top": this.placement === "top",
            "drawer-end": this.placement === "end",
            "drawer-bottom": this.placement === "bottom",
            "drawer-start": this.placement === "start",
            "drawer-contained": this.contained,
            "drawer-fixed": !this.contained
        })}
        ?hidden=${shouldHide}
      >
        <div class="drawer-overlay" @click=${() => this.requestClose("overlay")} tabindex="-1"></div>

        <div
          class="drawer-panel"
          role="dialog"
          aria-modal="true"
          aria-hidden=${this.open ? "false" : "true"}
          tabindex="0"
        >
          <header class="drawer-header">
            <slot name="title"></slot>
            <slot name="description"></slot>
            <sgds-close-button
              class="drawer-close"
              aria-label="close drawer"
              @click="${() => this.requestClose("close-button")}"
            ></sgds-close-button>
          </header>
          <slot class="drawer-body"></slot>
          <slot name="footer"></slot>
        </div>
      </div>
    `;
    }
}
SgdsDrawer.styles = [...sgdsElement["default"].styles, drawer["default"]];
/**@internal */
SgdsDrawer.dependencies = {
    "sgds-close-button": sgdsCloseButton.SgdsCloseButton
};
tslib.__decorate([
    decorators_js.query(".drawer")
], SgdsDrawer.prototype, "drawer", void 0);
tslib.__decorate([
    decorators_js.query(".drawer-panel")
], SgdsDrawer.prototype, "panel", void 0);
tslib.__decorate([
    decorators_js.query(".drawer-overlay")
], SgdsDrawer.prototype, "overlay", void 0);
tslib.__decorate([
    decorators_js.property({ type: Boolean, reflect: true })
], SgdsDrawer.prototype, "open", void 0);
tslib.__decorate([
    decorators_js.property({ type: String, reflect: true })
], SgdsDrawer.prototype, "size", void 0);
tslib.__decorate([
    decorators_js.property({ type: String, reflect: true })
], SgdsDrawer.prototype, "placement", void 0);
tslib.__decorate([
    decorators_js.property({ type: Boolean, reflect: true })
], SgdsDrawer.prototype, "contained", void 0);
tslib.__decorate([
    watch.watch("open", { waitUntilFirstUpdate: true })
], SgdsDrawer.prototype, "handleOpenChange", null);
tslib.__decorate([
    watch.watch("contained", { waitUntilFirstUpdate: true })
], SgdsDrawer.prototype, "handleNoModalChange", null);
// Top
animationRegistry.setDefaultAnimation("drawer.showTop", {
    keyframes: [
        { opacity: 0, translate: "0 -100%" },
        { opacity: 1, translate: "0 0" }
    ],
    options: { duration: 250, easing: "ease" }
});
animationRegistry.setDefaultAnimation("drawer.hideTop", {
    keyframes: [
        { opacity: 1, translate: "0 0" },
        { opacity: 0, translate: "0 -100%" }
    ],
    options: { duration: 250, easing: "ease" }
});
// End
animationRegistry.setDefaultAnimation("drawer.showEnd", {
    keyframes: [
        { opacity: 0, translate: "100%" },
        { opacity: 1, translate: "0" }
    ],
    rtlKeyframes: [
        { opacity: 0, translate: "-100%" },
        { opacity: 1, translate: "0" }
    ],
    options: { duration: 250, easing: "ease" }
});
animationRegistry.setDefaultAnimation("drawer.hideEnd", {
    keyframes: [
        { opacity: 1, translate: "0" },
        { opacity: 0, translate: "100%" }
    ],
    rtlKeyframes: [
        { opacity: 1, translate: "0" },
        { opacity: 0, translate: "-100%" }
    ],
    options: { duration: 250, easing: "ease" }
});
// Bottom
animationRegistry.setDefaultAnimation("drawer.showBottom", {
    keyframes: [
        { opacity: 0, translate: "0 100%" },
        { opacity: 1, translate: "0 0" }
    ],
    options: { duration: 250, easing: "ease" }
});
animationRegistry.setDefaultAnimation("drawer.hideBottom", {
    keyframes: [
        { opacity: 1, translate: "0 0" },
        { opacity: 0, translate: "0 100%" }
    ],
    options: { duration: 250, easing: "ease" }
});
// Start
animationRegistry.setDefaultAnimation("drawer.showStart", {
    keyframes: [
        { opacity: 0, translate: "-100%" },
        { opacity: 1, translate: "0" }
    ],
    rtlKeyframes: [
        { opacity: 0, translate: "100%" },
        { opacity: 1, translate: "0" }
    ],
    options: { duration: 250, easing: "ease" }
});
animationRegistry.setDefaultAnimation("drawer.hideStart", {
    keyframes: [
        { opacity: 1, translate: "0" },
        { opacity: 0, translate: "-100%" }
    ],
    rtlKeyframes: [
        { opacity: 1, translate: "0" },
        { opacity: 0, translate: "100%" }
    ],
    options: { duration: 250, easing: "ease" }
});
// Deny close
animationRegistry.setDefaultAnimation("drawer.denyClose", {
    keyframes: [{ scale: 1 }, { scale: 1.01 }, { scale: 1 }],
    options: { duration: 250 }
});
// Overlay
animationRegistry.setDefaultAnimation("drawer.overlay.show", {
    keyframes: [{ opacity: 0 }, { opacity: 1 }],
    options: { duration: 250 }
});
animationRegistry.setDefaultAnimation("drawer.overlay.hide", {
    keyframes: [{ opacity: 1 }, { opacity: 0 }],
    options: { duration: 250 }
});

exports.SgdsDrawer = SgdsDrawer;
exports["default"] = SgdsDrawer;
//# sourceMappingURL=sgds-drawer.cjs.js.map
