import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { BlobService } from 'src/app/shared/services/blob.service';

@Component({
  selector: 'app-blob-viewer',
  templateUrl: './blob-viewer.component.html',
  styleUrls: ['./blob-viewer.component.scss'],
})
export class BlobViewerComponent implements OnInit, OnDestroy {
  public TYPE_IMAGE = 'image';

  @Input() blob;
  @Input() type;

  public imgURL;
  private blobUrl;

  constructor(private sanitizer: DomSanitizer, private blobService: BlobService) {}

  ngOnInit(): void {
    if (this.type == this.TYPE_IMAGE) {
      this.blobUrl = this.blobService.createObjectURL(this.blob);
      this.imgURL = this.sanitizer.bypassSecurityTrustUrl(this.blobUrl);
    }
  }

  ngOnDestroy() {
    if (this.type == this.TYPE_IMAGE) {
      this.blobService.revokeObjectURL(this.blobUrl);
    }
  }
}
