import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { ITemplateLanguageModel } from '@fleio-api/message-templates/model/template-language.model';
import { IAction } from '@objects-view/interfaces/actions/action';
import { DialogCallbackAction } from '@objects-view/actions/dialog-callback-action';
import { IBaseDialog } from '@shared/common-dialogs/base-dialog';

@Component({
  selector: 'app-add-new-template-dialog',
  templateUrl: './add-new-template-dialog.component.html',
  styleUrls: ['./add-new-template-dialog.component.scss']
})
export class AddNewTemplateDialogComponent implements IBaseDialog, OnInit {
  addTemplateForm = this.formBuilder.group({
    language: ['', Validators.required],
  });
  dialogTitle = $localize`Create a new ${this.data?.templateName} template`;
  dialogActions: Array<IAction> = [
    new DialogCallbackAction({
      name: $localize`Create`,
      primary: true,
      callback: () => this.create(),
    }),
  ];
  dialogForm = this.addTemplateForm;

  constructor(
    public dialogRef: MatDialogRef<AddNewTemplateDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: { availableLanguages: ITemplateLanguageModel[]; templateName: string },
    private formBuilder: UntypedFormBuilder,
  ) {

  }

  ngOnInit(): void {
  }

  create() {
    this.dialogRef.close(this.addTemplateForm.controls.language.value);
  }
}
