// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { Component, Inject, OnInit } from "@angular/core";

import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { DialogRef, DIALOG_DATA, ToastService } from "@bitwarden/components";

export interface AccessTokenDetails {
  subTitle: string;
  expirationDate?: Date;
  accessToken: string;
}

// FIXME(https://bitwarden.atlassian.net/browse/CL-764): Migrate to OnPush
// eslint-disable-next-line @angular-eslint/prefer-on-push-component-change-detection
@Component({
  templateUrl: "./access-token-dialog.component.html",
  standalone: false,
})
export class AccessTokenDialogComponent implements OnInit {
  constructor(
    public dialogRef: DialogRef,
    @Inject(DIALOG_DATA) public data: AccessTokenDetails,
    private platformUtilsService: PlatformUtilsService,
    private toastService: ToastService,
    private i18nService: I18nService,
  ) {}

  ngOnInit(): void {
    // TODO remove null checks once strictNullChecks in TypeScript is turned on.
    if (!this.data.subTitle || !this.data.accessToken) {
      void this.dialogRef.close();
      throw new Error("The access token dialog was not called with the appropriate values.");
    }
  }

  copyAccessToken(): void {
    this.platformUtilsService.copyToClipboard(this.data.accessToken);
    this.toastService.showToast({
      variant: "success",
      title: null,
      message: this.i18nService.t("accessTokenCreatedAndCopied"),
    });
    void this.dialogRef.close();
  }
}
