import { Component, Inject, ViewChild } from '@angular/core';
import { BaseDialogComponent } from '@iqser/common-ui';
import { dossiersServiceProvider } from '@services/entity-services/dossiers.service.provider';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { Dictionary } from '@red/domain';
import { AddEditEntityComponent } from '@shared/components/add-edit-entity/add-edit-entity.component';

@Component({
    templateUrl: './dictionary-details-dialog.component.html',
    providers: [dossiersServiceProvider],
})
export class DictionaryDetailsDialogComponent extends BaseDialogComponent {
    @ViewChild(AddEditEntityComponent, { static: true }) private readonly _addEditEntityComponent: AddEditEntityComponent;

    constructor(
        @Inject(MAT_DIALOG_DATA)
        readonly data: { dictionary: Dictionary; dossierId: string; readOnly: boolean },
        protected readonly _dialogRef: MatDialogRef<DictionaryDetailsDialogComponent>,
    ) {
        super(_dialogRef, true);
    }

    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> {
        await this._addEditEntityComponent.save();
        this._dialogRef.close(true);
    }
}
