'use strict';
import { Controller } from '@hotwired/stimulus';
import "@tailwindplus/elements";
export default class default_1 extends Controller {
    constructor() {
        super(...arguments);
        this.observer = null;
    }
    connect() {
        this.observer = null;
        this.hide = this.hide.bind(this);
        this.clearContent = this.clearContent.bind(this);
        this.handleToggle = this.handleToggle.bind(this);
        window.addEventListener("turbo:before-visit", this.hide);
        window.addEventListener("turbo:before-render", this.hide);
        this.element.addEventListener("toggle", this.handleToggle);
        this.element.addEventListener("close", this.clearContent);
        this.dialogTarget.addEventListener("close", this.clearContent);
        if (this.hasDynamicContentTarget) {
            this.observer = new MutationObserver(() => {
                const hasContent = this.dynamicContentTarget.innerHTML.trim().length > 0;
                if (hasContent && !this.dialogTarget.open) {
                    this.element.show();
                }
                else if (!hasContent && this.dialogTarget.open) {
                    this.element.hide();
                }
            });
            this.observer.observe(this.dynamicContentTarget, {
                childList: true,
                characterData: true,
                subtree: true
            });
        }
    }
    disconnect() {
        window.removeEventListener("turbo:before-visit", this.hide);
        window.removeEventListener("turbo:before-render", this.hide);
        this.element.removeEventListener("toggle", this.handleToggle);
        this.element.removeEventListener("close", this.clearContent);
        this.dialogTarget.removeEventListener("close", this.clearContent);
        if (this.observer) {
            this.observer.disconnect();
        }
        this.hide();
    }
    hide() {
        this.element.hide();
    }
    handleToggle(event) {
        if (event.newState === "closed") {
            this.clearContent();
        }
    }
    clearContent() {
        if (this.hasDynamicContentTarget) {
            this.dynamicContentTarget.innerHTML = "";
            this.dynamicContentTarget.removeAttribute('src');
        }
    }
    open(event) {
        if (event.detail.url) {
            this.dynamicContentTarget.setAttribute('src', event.detail.url);
        }
        this.element.show();
    }
}
default_1.targets = ["dialog", "dynamicContent"];
