import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {MessagesService} from "@webbpm/base-package";

@Injectable({
              providedIn: "root"
            })
export class ValidateFileService {
  constructor(private httpClient: HttpClient) {
  }

  public exportFile(fileId: string) {
    const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    this.httpClient.get('validate/export/' + fileId, {
      responseType: 'blob',
      headers: {
        "Client-Time-Zone": timeZone,
      },
      observe: 'response'
    }).toPromise()
        .then((response) => {
          const data = window.URL.createObjectURL(response.body);
          const link = document.createElement("a");
          link.href = data;
          const contentDisposition = response.headers.get('Content-Disposition');
          const fileNameMatch = contentDisposition.match(/filename\*=?UTF-8''(.+)/i);
          link.download = decodeURIComponent(fileNameMatch[1].replace(/\+/g, '%20'));
          document.body.appendChild(link);
          link.click();
          window.URL.revokeObjectURL(data);
          link.remove();
        });
  }
}