import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import Constants from '../constants';

import { IUserEmployment } from '../models';
import {
  IDeleteEmploymentPayload,
  IUpdateEmploymentPayload,
} from '../store/actions/employment';

@Injectable()
export class EmploymentService {
  constructor(private http: Http) 


  public updateEmployment(payload: IUpdateEmploymentPayload, token): Observable<IUserEmployment> {
    const headers = new Headers({ 'Content-Type': 'application/json', 'X-Auth-Secret': token });

    const {
      company,
      position,
      description,
      date_from,
      date_to,
      users_talent_ids,
    } = payload.employment;

    const dataToReturn = {
      users_employment: {
        company,
        position,
        description,
        date_from,
        date_to,
        users_talent_ids,
      },
    };

    return this.http.patch(this.getAPIUrl(payload.userId) + payload.employment.id, dataToReturn, { headers })
      .map((res) => res ? res.json().data : null);
  }

  public deleteEmployment(payload: IDeleteEmploymentPayload, token): Observable<any> {
    const headers = new Headers({ 'Content-Type': 'application/json', 'X-Auth-Secret': token });

    return this.http.delete(this.getAPIUrl(payload.userId) + payload.employmentId, { headers });
  }

  private getAPIUrl = (userId) => `${Constants.API_URL}/user/${userId}/users_employment/`;
}

{ }