import { Component, OnInit } from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {IUnwrapped} from "../../models/ResponseUnwrap";
import * as api from "../../globals/api";

@Component({
  selector: 'app-decrypt',
  templateUrl: './decrypt.component.html',
  styleUrls: ['./decrypt.component.scss']
})
export class DecryptComponent implements OnInit {


  constructor(private http: HttpClient) { }

  tokenToSend: string = '';
  response: any
  displayedResult: string = ''

  outUnwrap(){

    this.getUnwrap(this.tokenToSend).subscribe(response => {
      this.response = response.data;
      this.displayedResult = response.data.dataToWrap.First_string;
    },
    error => {
      if(error.status > 200 ) {
        this.displayedResult = error.error.errors[0];
      }
    }
    )
  }

  getUnwrap(token: string){
    let body = new URLSearchParams();
    body.set("token", token)
    let headers: HttpHeaders = new HttpHeaders();
    headers = headers.append('X-Vault-Token', api.xVault_Token);
    headers = headers.append('Content-Type', 'application/x-www-form-urlencoded')
    return this.http.post<IUnwrapped>(api.api_endpoint + '/unwrap' , body, {headers})
  }

  ngOnInit(): void {
  }

}
