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 { plainToClass } from 'class-transformer';
import { Headers } from '@angular/http';
import { HttpErrorService, HttpService, OrderService } from '../../../services';
import { ShowCondition } from '../../../directives/conditional-show/domain/conditional-show.model';
import { catchError, map, tap } from 'rxjs/operators';
import { CaseEventTrigger, Draft } from '../../../domain';
import { WizardPageFieldToCaseFieldMapper } from './wizard-page-field-to-case-field.mapper';
var CasesService = /** @class */ (function () {
    function CasesService(http, appConfig, orderService, errorService, wizardPageFieldToCaseFieldMapper) {
        this.http = http;
        this.appConfig = appConfig;
        this.orderService = orderService;
        this.errorService = errorService;
        this.wizardPageFieldToCaseFieldMapper = wizardPageFieldToCaseFieldMapper;
        /**
         *
         * @type {(caseId:string)=>"../../Observable".Observable<Case>}
         * @deprecated Use `CasesService::getCaseView` instead
         */
        this.get = this.getCaseView;
    }
    CasesService_1 = CasesService;
    CasesService.prototype.getCaseView = function (jurisdictionId, caseTypeId, caseId) {
        var _this = this;
        var url = this.appConfig.getApiUrl()
            + "/caseworkers/:uid"
            + ("/jurisdictions/" + jurisdictionId)
            + ("/case-types/" + caseTypeId)
            + ("/cases/" + caseId);
        return this.http
            .get(url)
            .pipe(map(function (response) { return response.json(); }), catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    CasesService.prototype.getCaseViewV2 = function (caseId) {
        var _this = this;
        var url = this.appConfig.getCaseDataUrl() + "/internal/cases/" + caseId;
        var headers = new Headers({
            'Accept': CasesService_1.V2_MEDIATYPE_CASE_VIEW,
            'experimental': 'true',
        });
        return this.http
            .get(url, { headers: headers })
            .pipe(map(function (response) { return response.json(); }), catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    CasesService.prototype.getEventTrigger = function (caseTypeId, eventTriggerId, caseId, ignoreWarning) {
        var _this = this;
        ignoreWarning = undefined !== ignoreWarning ? ignoreWarning : 'false';
        var url = this.buildEventTriggerUrl(caseTypeId, eventTriggerId, caseId, ignoreWarning);
        var headers = new Headers({
            'experimental': 'true'
        });
        if (Draft.isDraft(caseId)) {
            headers.set('Accept', CasesService_1.V2_MEDIATYPE_START_DRAFT_TRIGGER);
        }
        else if (caseId !== undefined && caseId !== null) {
            headers.set('Accept', CasesService_1.V2_MEDIATYPE_START_EVENT_TRIGGER);
        }
        else {
            headers.set('Accept', CasesService_1.V2_MEDIATYPE_START_CASE_TRIGGER);
        }
        return this.http
            .get(url, { headers: headers })
            .pipe(map(function (response) { return response.json(); }), catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }), map(function (p) { return plainToClass(CaseEventTrigger, p); }), tap(function (eventTrigger) { return _this.initialiseEventTrigger(eventTrigger); }));
    };
    CasesService.prototype.createEvent = function (caseDetails, eventData) {
        var _this = this;
        var caseId = caseDetails.case_id;
        var url = this.appConfig.getCaseDataUrl() + ("/cases/" + caseId + "/events");
        var headers = new Headers({
            'experimental': 'true',
            'Accept': CasesService_1.V2_MEDIATYPE_CREATE_EVENT
        });
        return this.http
            .post(url, eventData, { headers: headers })
            .pipe(map(function (response) { return _this.processResponse(response); }), catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    CasesService.prototype.validateCase = function (ctid, eventData, pageId) {
        var _this = this;
        var pageIdString = pageId ? '?pageId=' + pageId : '';
        var url = this.appConfig.getCaseDataUrl()
            + ("/case-types/" + ctid + "/validate" + pageIdString);
        var headers = new Headers({
            'experimental': 'true',
            'Accept': CasesService_1.V2_MEDIATYPE_CASE_DATA_VALIDATE
        });
        return this.http
            .post(url, eventData, { headers: headers })
            .pipe(map(function (response) { return response.json(); }), catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    CasesService.prototype.createCase = function (jid, ctid, eventData) {
        var _this = this;
        var ignoreWarning = 'false';
        if (eventData.ignore_warning) {
            ignoreWarning = 'true';
        }
        var url = this.appConfig.getCaseDataUrl()
            + ("/caseworkers/:uid/jurisdictions/" + jid + "/case-types/" + ctid + "/cases?ignore-warning=" + ignoreWarning);
        return this.http
            .post(url, eventData)
            .pipe(map(function (response) { return _this.processResponse(response); }), catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    CasesService.prototype.getPrintDocuments = function (jurisdictionId, caseTypeId, caseId) {
        var _this = this;
        var url = this.appConfig.getCaseDataUrl()
            + "/caseworkers/:uid"
            + ("/jurisdictions/" + jurisdictionId)
            + ("/case-types/" + caseTypeId)
            + ("/cases/" + caseId)
            + "/documents";
        return this.http
            .get(url)
            .pipe(map(function (response) { return response.json(); }), catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    CasesService.prototype.buildEventTriggerUrl = function (caseTypeId, eventTriggerId, caseId, ignoreWarning) {
        var url = this.appConfig.getCaseDataUrl() + "/internal";
        if (Draft.isDraft(caseId)) {
            url += "/drafts/" + caseId
                + "/event-trigger"
                + ("?ignore-warning=" + ignoreWarning);
        }
        else if (caseTypeId === undefined || caseTypeId === null) {
            url += "/cases/" + caseId
                + ("/event-triggers/" + eventTriggerId)
                + ("?ignore-warning=" + ignoreWarning);
        }
        else {
            url += "/case-types/" + caseTypeId
                + ("/event-triggers/" + eventTriggerId)
                + ("?ignore-warning=" + ignoreWarning);
        }
        return url;
    };
    CasesService.prototype.processResponse = function (response) {
        if (response.headers && response.headers.get('content-type').match(/application\/.*json/)) {
            return response.json();
        }
        return { 'id': '' };
    };
    CasesService.prototype.initialiseEventTrigger = function (eventTrigger) {
        var _this = this;
        if (!eventTrigger.wizard_pages) {
            eventTrigger.wizard_pages = [];
        }
        eventTrigger.wizard_pages.forEach(function (wizardPage) {
            wizardPage.parsedShowCondition = new ShowCondition(wizardPage.show_condition);
            wizardPage.case_fields = _this.orderService
                .deepSort(_this.wizardPageFieldToCaseFieldMapper.mapAll(wizardPage.wizard_page_fields, eventTrigger.case_fields));
        });
    };
    var CasesService_1;
    CasesService.V2_MEDIATYPE_CASE_VIEW = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.ui-case-view.v2+json';
    CasesService.V2_MEDIATYPE_START_CASE_TRIGGER = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.ui-start-case-trigger.v2+json;charset=UTF-8';
    CasesService.V2_MEDIATYPE_START_EVENT_TRIGGER = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.ui-start-event-trigger.v2+json;charset=UTF-8';
    CasesService.V2_MEDIATYPE_START_DRAFT_TRIGGER = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.ui-start-draft-trigger.v2+json;charset=UTF-8';
    CasesService.V2_MEDIATYPE_CASE_DATA_VALIDATE = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.case-data-validate.v2+json;charset=UTF-8';
    CasesService.V2_MEDIATYPE_CREATE_EVENT = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.create-event.v2+json;charset=UTF-8';
    CasesService = CasesService_1 = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [HttpService,
            AbstractAppConfig,
            OrderService,
            HttpErrorService,
            WizardPageFieldToCaseFieldMapper])
    ], CasesService);
    return CasesService;
}());
export { CasesService };
//# sourceMappingURL=cases.service.js.map