import { Component, OnInit } from '@angular/core';
import { UntypedFormBuilder, UntypedFormControl, Validators } from '@angular/forms';
import { MatDialogRef } from '@angular/material/dialog';
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
import { NanoDialogBaseComponent } from 'src/app/shared/dialogs/nano-base-dialog';
import { NanoValidators } from 'src/app/shared/nano-validators';
import { DialogService } from 'src/app/shared/services/dialog.service';

@Component({
  selector: 'app-change-email-dialog',
  templateUrl: './change-email-dialog.component.html',
  styleUrls: ['./change-email-dialog.component.scss'],
})
export class ChangeEmailDialogComponent
  extends NanoDialogBaseComponent<ChangeEmailDialogComponent>
  implements OnInit
{
  inputsValid: boolean = false;

  public emailForm = this.fb.group({
    email: new UntypedFormControl('', [Validators.required, Validators.email]),
    email2: new UntypedFormControl('', [Validators.required, Validators.email]),
  });

  constructor(
    public dialogRef: MatDialogRef<ChangeEmailDialogComponent>,
    public dialogService: DialogService,
    public fb: UntypedFormBuilder
  ) {
    super(dialogRef);
  }

  ngOnInit(): void {
    this.emailForm
      .get('email2')
      .addValidators(NanoValidators.controlEquals(this.emailForm.get('email')));
  }

  changeEmail() {
    this.dialogRef.close(this.emailForm.get('email').value);
  }
}
