import { CommonModule } from "@angular/common";
import { Component, Inject } from "@angular/core";
import { FormBuilder, ReactiveFormsModule } from "@angular/forms";

import {
  DIALOG_DATA,
  DialogRef,
  AsyncActionsModule,
  ButtonModule,
  DialogModule,
  FormFieldModule,
  IconButtonModule,
  DialogService,
  CalloutModule,
} from "@bitwarden/components";
import { I18nPipe } from "@bitwarden/ui-common";

export interface ApproveSshRequestParams {
  cipherName: string;
  applicationName: string;
  isAgentForwarding: boolean;
  action: 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({
  selector: "app-approve-ssh-request",
  templateUrl: "approve-ssh-request.html",
  imports: [
    DialogModule,
    CommonModule,
    I18nPipe,
    ButtonModule,
    IconButtonModule,
    ReactiveFormsModule,
    AsyncActionsModule,
    FormFieldModule,
    CalloutModule,
  ],
})
export class ApproveSshRequestComponent {
  approveSshRequestForm = this.formBuilder.group({});

  constructor(
    @Inject(DIALOG_DATA) protected params: ApproveSshRequestParams,
    private dialogRef: DialogRef<boolean>,
    private formBuilder: FormBuilder,
  ) {}

  static open(
    dialogService: DialogService,
    cipherName: string,
    applicationName: string,
    isAgentForwarding: boolean,
    namespace: string,
  ) {
    let actioni18nKey = "sshActionLogin";
    if (namespace === "git") {
      actioni18nKey = "sshActionGitSign";
    } else if (namespace != null && namespace != "") {
      actioni18nKey = "sshActionSign";
    }

    return dialogService.open<boolean, ApproveSshRequestParams>(ApproveSshRequestComponent, {
      data: {
        cipherName,
        applicationName,
        isAgentForwarding,
        action: actioni18nKey,
      },
    });
  }

  submit = async () => {
    await this.dialogRef.close(true);
  };
}
