var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Injectable } from '@angular/core';
import { throwError } from 'rxjs';
import { AbstractAppConfig } from '../../../app.config';
import { HttpErrorService } from '../http';
import { Draft, DRAFT_PREFIX } from '../../domain';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
var DraftService = /** @class */ (function () {
    function DraftService(httpClient, appConfig, errorService) {
        this.httpClient = httpClient;
        this.appConfig = appConfig;
        this.errorService = errorService;
    }
    DraftService_1 = DraftService;
    DraftService.prototype.createDraft = function (ctid, eventData) {
        var _this = this;
        var saveDraftEndpoint = this.appConfig.getCreateOrUpdateDraftsUrl(ctid);
        var headers = new HttpHeaders({
            'Accept': [DraftService_1.V2_MEDIATYPE_DRAFT_CREATE, DraftService_1.QC_V2_MEDIATYPE_DRAFT_CREATE],
        });
        return this.httpClient
            .post(saveDraftEndpoint, eventData, { headers: headers })
            .pipe(catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    DraftService.prototype.updateDraft = function (ctid, draftId, eventData) {
        var _this = this;
        var saveDraftEndpoint = this.appConfig.getCreateOrUpdateDraftsUrl(ctid) + draftId;
        var headers = new HttpHeaders({
            'Accept': [DraftService_1.V2_MEDIATYPE_DRAFT_UPDATE, DraftService_1.QC_V2_MEDIATYPE_DRAFT_UPDATE],
        });
        return this.httpClient
            .put(saveDraftEndpoint, eventData, { headers: headers })
            .pipe(catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    DraftService.prototype.getDraft = function (draftId) {
        var _this = this;
        var url = this.appConfig.getViewOrDeleteDraftsUrl(draftId.slice(DRAFT_PREFIX.length));
        var headers = new HttpHeaders({
            'Accept': [DraftService_1.V2_MEDIATYPE_DRAFT_READ, DraftService_1.QC_V2_MEDIATYPE_DRAFT_READ],
        });
        return this.httpClient
            .get(url, { headers: headers })
            .pipe(catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    DraftService.prototype.deleteDraft = function (draftId) {
        var _this = this;
        var url = this.appConfig.getViewOrDeleteDraftsUrl(draftId.slice(DRAFT_PREFIX.length));
        var headers = new HttpHeaders({
            'Accept': [DraftService_1.V2_MEDIATYPE_DRAFT_DELETE, DraftService_1.QC_V2_MEDIATYPE_DRAFT_DELETE],
        });
        return this.httpClient
            .delete(url, { headers: headers })
            .pipe(catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    DraftService.prototype.createOrUpdateDraft = function (caseTypeId, draftId, caseEventData) {
        if (!draftId) {
            return this.createDraft(caseTypeId, caseEventData);
        }
        else {
            return this.updateDraft(caseTypeId, Draft.stripDraftId(draftId), caseEventData);
        }
    };
    var DraftService_1;
    DraftService.V2_MEDIATYPE_DRAFT_CREATE = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.ui-draft-create.v2+json;charset=UTF-8';
    DraftService.V2_MEDIATYPE_DRAFT_UPDATE = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.ui-draft-update.v2+json;charset=UTF-8';
    DraftService.V2_MEDIATYPE_DRAFT_READ = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.ui-draft-read.v2+json;charset=UTF-8';
    DraftService.V2_MEDIATYPE_DRAFT_DELETE = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.ui-draft-delete.v2+json;charset=UTF-8';
    DraftService.QC_V2_MEDIATYPE_DRAFT_CREATE = 'application/vnd.app.quickcase.store.data.api.ui-draft-create.v2+json;charset=UTF-8';
    DraftService.QC_V2_MEDIATYPE_DRAFT_UPDATE = 'application/vnd.app.quickcase.store.data.api.ui-draft-update.v2+json;charset=UTF-8';
    DraftService.QC_V2_MEDIATYPE_DRAFT_READ = 'application/vnd.app.quickcase.store.data.api.ui-draft-read.v2+json;charset=UTF-8';
    DraftService.QC_V2_MEDIATYPE_DRAFT_DELETE = 'application/vnd.app.quickcase.store.data.api.ui-draft-delete.v2+json;charset=UTF-8';
    DraftService = DraftService_1 = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [HttpClient,
            AbstractAppConfig,
            HttpErrorService])
    ], DraftService);
    return DraftService;
}());
export { DraftService };
//# sourceMappingURL=draft.service.js.map