// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { Component, OnInit } from "@angular/core";
import { FormGroup, ReactiveFormsModule } from "@angular/forms";
import { ActivatedRoute, Router, RouterLink } from "@angular/router";
import { first } from "rxjs/operators";

import { JslibModule } from "@bitwarden/angular/jslib.module";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { VerifyDeleteRecoverRequest } from "@bitwarden/common/models/request/verify-delete-recover.request";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import {
  AsyncActionsModule,
  ButtonModule,
  CalloutComponent,
  ToastService,
  TypographyModule,
} from "@bitwarden/components";

// FIXME(https://bitwarden.atlassian.net/browse/CL-764): Migrate to OnPush
// eslint-disable-next-line @angular-eslint/prefer-on-push-component-change-detection
@Component({
  selector: "app-verify-recover-delete",
  templateUrl: "verify-recover-delete.component.html",
  imports: [
    ReactiveFormsModule,
    RouterLink,
    JslibModule,
    AsyncActionsModule,
    ButtonModule,
    CalloutComponent,
    TypographyModule,
  ],
})
export class VerifyRecoverDeleteComponent implements OnInit {
  email: string;

  private userId: string;
  private token: string;
  protected formGroup = new FormGroup({});

  constructor(
    private router: Router,
    private apiService: ApiService,
    private i18nService: I18nService,
    private route: ActivatedRoute,
    private toastService: ToastService,
  ) {}

  ngOnInit() {
    // eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe
    this.route.queryParams.pipe(first()).subscribe(async (qParams) => {
      if (qParams.userId != null && qParams.token != null && qParams.email != null) {
        this.userId = qParams.userId;
        this.token = qParams.token;
        this.email = qParams.email;
      } else {
        await this.router.navigate(["/"]);
      }
    });
  }

  submit = async () => {
    const request = new VerifyDeleteRecoverRequest(this.userId, this.token);
    await this.apiService.postAccountRecoverDeleteToken(request);
    this.toastService.showToast({
      variant: "success",
      title: this.i18nService.t("accountDeleted"),
      message: this.i18nService.t("accountDeletedDesc"),
    });
    await this.router.navigate(["/"]);
  };
}
