var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") 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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { MelodicComponent, Service } from '@melodicdev/core';
import { dialogTemplate } from './dialog.template';
import { dialogStyles } from './dialog.styles';
import { newID } from '../../../functions/new-id.function';
import { DialogService } from './dialog.service';
let DialogComponent = class DialogComponent {
    constructor() {
        this._dialogID = newID();
        this._registered = false;
    }
    onCreate() {
        this.registerDialog();
    }
    registerDialog() {
        if (this._registered)
            return;
        const dialogEl = this.elementRef.shadowRoot?.querySelector('dialog');
        if (!dialogEl)
            return;
        this._dialogEl = dialogEl;
        this._dialogID = this.createDialogID();
        this._dialogEl.id = this._dialogID;
        this._dialogRef = this._dialogService.addDialog(this._dialogID, this._dialogEl);
        this._registered = true;
    }
    onDestroy() {
        this._dialogService.removeDialog(this._dialogID);
    }
    open() {
        this.registerDialog();
        this._dialogRef.open();
    }
    close(result) {
        this._dialogRef.close(result);
    }
    createDialogID() {
        return (this.elementRef
            .getAttributeNames()
            .find((attr) => attr.startsWith('#'))
            ?.slice(1) ?? this._dialogID);
    }
};
__decorate([
    Service(DialogService),
    __metadata("design:type", DialogService)
], DialogComponent.prototype, "_dialogService", void 0);
DialogComponent = __decorate([
    MelodicComponent({
        selector: 'ml-dialog',
        template: dialogTemplate,
        styles: dialogStyles,
        attributes: []
    })
], DialogComponent);
export { DialogComponent };
