export class DialogRef {
    constructor(_dialogID, _dialogEl) {
        this._dialogID = _dialogID;
        this._dialogEl = _dialogEl;
        this._afterOpenedCallback = null;
        this._afterClosedCallback = null;
        this._disableClose = false;
        this._handleCancel = this.onCancel.bind(this);
        this._handleBackdropClick = this.onBackdropClick.bind(this);
        this._dialogEl.addEventListener('cancel', this._handleCancel);
        this._dialogEl.addEventListener('click', this._handleBackdropClick);
    }
    get dialogID() {
        return this._dialogID;
    }
    get data() {
        return this._data;
    }
    get disableClose() {
        return this._disableClose;
    }
    applyConfig(config) {
        if (config.data !== undefined) {
            this._data = config.data;
        }
        if (config.disableClose !== undefined) {
            this._disableClose = config.disableClose;
        }
        if (config.size && config.size !== 'auto') {
            this._dialogEl.classList.add(`ml-dialog--${config.size}`);
        }
        if (config.width) {
            this._dialogEl.style.maxWidth = config.width;
        }
        if (config.panelClass) {
            const classes = Array.isArray(config.panelClass) ? config.panelClass : [config.panelClass];
            this._dialogEl.classList.add(...classes);
        }
        return this;
    }
    open() {
        this._dialogEl.showModal();
        this._afterOpenedCallback?.();
    }
    close(result) {
        this._dialogEl.close();
        this._afterClosedCallback?.(result);
    }
    afterOpened(callback) {
        this._afterOpenedCallback = callback;
    }
    afterClosed(callback) {
        this._afterClosedCallback = callback;
    }
    onCancel(event) {
        if (this._disableClose) {
            event.preventDefault();
        }
    }
    onBackdropClick(event) {
        if (event.target === this._dialogEl && !this._disableClose) {
            this.close();
        }
    }
}
