import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

import { WEB_SERVICE_CONFIG } from './provider-name-token';
import { WebServiceConfig } from './web-service-config';

@Injectable({
  providedIn: 'root',
})
export class MailService {

  constructor(
    private http: HttpClient,
    @Inject(WEB_SERVICE_CONFIG) private webServiceConfig: WebServiceConfig
  ) { }

  public send(params: any): Observable<void> {
    const url = `${this.webServiceConfig.webServiceUrl}/${this.webServiceConfig.appMailPath}/send`;

    const formData = new FormData();
    for (const key in params) {
      if (params.hasOwnProperty(key)) {
        if (params[key] instanceof File) {
          formData.append(key, params[key], params[key].name);
        } else {
          formData.append(key, params[key]);
        }
      }
    }

    return this.http.post<void>(url, formData);
  }

}
