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, 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';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { plainToClass } from 'class-transformer';
var CasesService = /** @class */ (function () {
    function CasesService(httpClient, appConfig, orderService, errorService, wizardPageFieldToCaseFieldMapper) {
        this.httpClient = httpClient;
        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 (caseId) {
        var _this = this;
        var url = this.appConfig.getCaseDataUrl() + "/internal/cases/" + caseId;
        var headers = new HttpHeaders({
            'Accept': [CasesService_1.V2_MEDIATYPE_CASE_VIEW, CasesService_1.QC_V2_MEDIATYPE_CASE_VIEW],
        });
        return this.httpClient.get(url, { headers: headers })
            .pipe(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;
        if (Draft.isDraft(caseId)) {
            headers = new HttpHeaders({
                'Accept': [CasesService_1.V2_MEDIATYPE_START_DRAFT_TRIGGER, CasesService_1.QC_V2_MEDIATYPE_START_DRAFT_TRIGGER],
            });
        }
        else if (caseId !== undefined && caseId !== null) {
            headers = new HttpHeaders({
                'Accept': [CasesService_1.V2_MEDIATYPE_START_EVENT_TRIGGER, CasesService_1.QC_V2_MEDIATYPE_START_EVENT_TRIGGER],
            });
        }
        else {
            headers = new HttpHeaders({
                'Accept': [CasesService_1.V2_MEDIATYPE_START_CASE_TRIGGER, CasesService_1.QC_V2_MEDIATYPE_START_CASE_TRIGGER],
            });
        }
        return this.httpClient
            .get(url, { headers: headers })
            .pipe(map(function (p) { return plainToClass(CaseEventTrigger, p); }), catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }), 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() + ("/internal/cases/" + caseId + "/events");
        var headers = new HttpHeaders({
            'Accept': [CasesService_1.V2_MEDIATYPE_CREATE_EVENT, CasesService_1.QC_V2_MEDIATYPE_CREATE_EVENT],
        });
        return this.httpClient
            .post(url, eventData, { observe: 'response', 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()
            + ("/internal/case-types/" + ctid + "/validate" + pageIdString);
        var headers = new HttpHeaders({
            'Accept': [CasesService_1.V2_MEDIATYPE_CASE_DATA_VALIDATE, CasesService_1.QC_V2_MEDIATYPE_CASE_DATA_VALIDATE],
        });
        return this.httpClient
            .post(url, eventData, { headers: headers })
            .pipe(catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    CasesService.prototype.createCase = function (ctid, eventData) {
        var _this = this;
        var ignoreWarning = 'false';
        if (eventData.ignore_warning) {
            ignoreWarning = 'true';
        }
        var url = this.appConfig.getCaseDataUrl()
            + ("/internal/case-types/" + ctid + "/cases?ignore-warning=" + ignoreWarning);
        var headers = new HttpHeaders({
            'Accept': [CasesService_1.V2_MEDIATYPE_CREATE_CASE, CasesService_1.QC_V2_MEDIATYPE_CREATE_CASE],
        });
        return this.httpClient
            .post(url, eventData, { observe: 'response', headers: headers })
            .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.httpClient
            .get(url)
            .pipe(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') && response.headers.get('content-type').match(/application\/.*json/)) {
            return response.body;
        }
        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.V2_MEDIATYPE_CREATE_CASE = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.create-case.v2+json;charset=UTF-8';
    CasesService.QC_V2_MEDIATYPE_CASE_VIEW = 'application/vnd.app.quickcase.store.data.api.ui-case-view.v2+json';
    CasesService.QC_V2_MEDIATYPE_START_CASE_TRIGGER = 'application/vnd.app.quickcase.store.data.api.ui-start-case-trigger.v2+json;charset=UTF-8';
    CasesService.QC_V2_MEDIATYPE_START_EVENT_TRIGGER = 'application/vnd.app.quickcase.store.data.api.ui-start-event-trigger.v2+json;charset=UTF-8';
    CasesService.QC_V2_MEDIATYPE_START_DRAFT_TRIGGER = 'application/vnd.app.quickcase.store.data.api.ui-start-draft-trigger.v2+json;charset=UTF-8';
    CasesService.QC_V2_MEDIATYPE_CASE_DATA_VALIDATE = 'application/vnd.app.quickcase.store.data.api.case-data-validate.v2+json;charset=UTF-8';
    CasesService.QC_V2_MEDIATYPE_CREATE_EVENT = 'application/vnd.app.quickcase.store.data.api.create-event.v2+json;charset=UTF-8';
    CasesService.QC_V2_MEDIATYPE_CREATE_CASE = 'application/vnd.app.quickcase.store.data.api.create-case.v2+json;charset=UTF-8';
    CasesService = CasesService_1 = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [HttpClient,
            AbstractAppConfig,
            OrderService,
            HttpErrorService,
            WizardPageFieldToCaseFieldMapper])
    ], CasesService);
    return CasesService;
}());
export { CasesService };
//# sourceMappingURL=cases.service.js.map