import { Component, inject, ViewChild } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { BaseDialogComponent, getConfig, IconButtonTypes } from '@iqser/common-ui';
import { AddEditEntityComponent } from '@shared/components/add-edit-entity/add-edit-entity.component';

interface DialogData {
    readonly dossierTemplateId: string;
}

@Component({
    templateUrl: './add-entity-dialog.component.html',
    styleUrls: ['./add-entity-dialog.component.scss'],
})
export class AddEntityDialogComponent extends BaseDialogComponent {
    @ViewChild(AddEditEntityComponent, { static: true }) private readonly _addEditEntityComponent: AddEditEntityComponent;
    readonly iconButtonTypes = IconButtonTypes;
    readonly data = inject<DialogData>(MAT_DIALOG_DATA);
    readonly config = getConfig();

    constructor(protected readonly _dialogRef: MatDialogRef<AddEntityDialogComponent>) {
        super(_dialogRef, false);
    }

    get valid(): boolean {
        return this._addEditEntityComponent.valid;
    }

    get changed(): boolean {
        return this._addEditEntityComponent.changed;
    }

    get disabled(): boolean {
        return this._addEditEntityComponent.disabled;
    }

    async save(): Promise<void> {
        try {
            await this._addEditEntityComponent.save();
            this._dialogRef.close(true);
        } catch (e) {
            console.error(e);
        }
    }
}
