import { LitElement, css, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { renderIpxBody } from "../atomic-ipx-body/ipx-body.js";
import { ipxBodyStyles } from "../atomic-ipx-body/ipx-body-styles.js";
import { booleanConverter } from "../../../converters/boolean-converter.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { watch } from "../../../decorators/watch.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import { InitializeBindingsMixin } from "../../../mixins/bindings-mixin.js";
import { buildCustomEvent } from "../../../utils/event-utils.js";
import { updateBreakpoints } from "../../../utils/replace-breakpoint-utils.js";
import { once, randomID } from "../../../utils/utils.js";
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
class AtomicIpxModal extends InitializeBindingsMixin(LitElement) {
    initialize() {
        if (this.isOpen) {
            this.watchToggleOpen();
            this.updateHostClasses();
        }
    }
    connectedCallback() {
        super.connectedCallback();
        document.body.addEventListener('touchmove', this.onWindowTouchMove, {
            passive: false
        });
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        document.body.removeEventListener('touchmove', this.onWindowTouchMove);
    }
    watchToggleOpen() {
        const modalOpenedClass = 'atomic-ipx-modal-opened';
        if (this.isOpen) {
            document.body.classList.add(modalOpenedClass);
            this.bindings?.interfaceElement?.classList.add(modalOpenedClass);
            return;
        }
        document.body.classList.remove(modalOpenedClass);
        this.bindings?.interfaceElement?.classList.remove(modalOpenedClass);
    }
    willUpdate() {
        this.hasFooterSlotElements = !!this.querySelector('[slot="footer"]');
    }
    firstUpdated() {
        const id = this.id || randomID('atomic-ipx-modal-');
        this.id = id;
        this.setAttribute('part', 'atomic-ipx-modal');
    }
    updateHostClasses() {
        this.classList.toggle('open', this.isOpen);
    }
    render() {
        this.updateBreakpoints();
        this.updateHostClasses();
        return html`
      <div part="backdrop">
        ${renderIpxBody({
            props: {
                i18n: this.bindings.i18n,
                visibility: this.isOpen ? 'open' : 'closed',
                displayFooterSlot: this.hasFooterSlotElements,
                onAnimationEnd: this.handleAnimationEnded,
                header: html`<slot name="header"></slot>`,
                body: html`<slot name="body"></slot>`,
                footer: html`<slot name="footer"></slot>`
            }
        })}
      </div>
    `;
    }
    constructor(...args){
        super(...args), this.isOpen = false, this.hasFooterSlotElements = true, this.updateBreakpoints = once(()=>updateBreakpoints(this)), this.onWindowTouchMove = (e)=>{
            if (this.isOpen) e.preventDefault();
        }, this.handleAnimationEnded = ()=>{
            this.dispatchEvent(buildCustomEvent('animationEnded'));
        };
    }
}
AtomicIpxModal.styles = [
    ipxBodyStyles,
    css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */atomic-focus-trap{display:contents;height:inherit}:host{box-shadow:0 0 .5rem #00000080;display:none;height:var(--atomic-ipx-height,43.75rem);inset:auto 3rem 4.25rem auto;max-height:calc(100vh - 4.25rem);max-width:calc(100vw - 3rem);position:fixed;width:var(--atomic-ipx-width,31.25rem);z-index:1000}:host [part=backdrop]{height:inherit;pointer-events:none}:host(.open){display:block}:host(.open) [part=backdrop]{height:inherit;max-height:calc(100vh - 4.25rem);pointer-events:auto}`
];
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof AnyBindings ? Object : AnyBindings)
], AtomicIpxModal.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicIpxModal.prototype, "error", void 0);
_ts_decorate([
    property({
        type: Object,
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof HTMLElement ? Object : HTMLElement)
], AtomicIpxModal.prototype, "source", void 0);
_ts_decorate([
    property({
        type: Object,
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof HTMLElement ? Object : HTMLElement)
], AtomicIpxModal.prototype, "container", void 0);
_ts_decorate([
    property({
        type: Boolean,
        reflect: true,
        attribute: 'is-open',
        converter: booleanConverter
    })
], AtomicIpxModal.prototype, "isOpen", void 0);
_ts_decorate([
    state()
], AtomicIpxModal.prototype, "hasFooterSlotElements", void 0);
_ts_decorate([
    watch('isOpen'),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicIpxModal.prototype, "watchToggleOpen", null);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicIpxModal.prototype, "render", null);
AtomicIpxModal = _ts_decorate([
    customElement('atomic-ipx-modal'),
    bindings(),
    withTailwindStyles
], AtomicIpxModal);
export { AtomicIpxModal };
