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 { map } from 'rxjs/operators';
import { AbstractAppConfig } from '../../../app.config';
import { HttpService } from '../http';
import { Headers } from '@angular/http';
import { RequestOptionsBuilder } from '../request';
var SearchService = /** @class */ (function () {
    function SearchService(appConfig, httpService, requestOptionsBuilder) {
        this.appConfig = appConfig;
        this.httpService = httpService;
        this.requestOptionsBuilder = requestOptionsBuilder;
    }
    SearchService_1 = SearchService;
    SearchService.prototype.search = function (jurisdictionId, caseTypeId, metaCriteria, caseCriteria, view) {
        var url = this.appConfig.getApiUrl() + "/caseworkers/:uid"
            + ("/jurisdictions/" + jurisdictionId)
            + ("/case-types/" + caseTypeId)
            + "/cases";
        var options = this.requestOptionsBuilder.buildOptions(metaCriteria, caseCriteria, view);
        return this.httpService
            .get(url, options)
            .pipe(map(function (response) { return response.json(); }));
    };
    SearchService.prototype.getSearchInputUrl = function (caseTypeId) {
        return this.appConfig.getCaseDataUrl() + "/internal/case-types/" + caseTypeId + "/search-inputs";
    };
    SearchService.prototype.getSearchInputs = function (jurisdictionId, caseTypeId) {
        var _this = this;
        var url = this.getSearchInputUrl(caseTypeId);
        var headers = new Headers({
            'Accept': SearchService_1.V2_MEDIATYPE_SEARCH_INPUTS,
            'experimental': 'true',
        });
        this.currentJurisdiction = jurisdictionId;
        this.currentCaseType = caseTypeId;
        return this.httpService
            .get(url, { headers: headers })
            .pipe(map(function (response) {
            var jsonResponse = response.json();
            var searchInputs = jsonResponse.searchInputs;
            if (_this.isDataValid(jurisdictionId, caseTypeId)) {
                searchInputs.forEach(function (item) {
                    item.field.label = item.label;
                });
            }
            else {
                throw new Error('Response expired');
            }
            return searchInputs;
        }));
    };
    SearchService.prototype.isDataValid = function (jurisdictionId, caseTypeId) {
        return this.currentJurisdiction === jurisdictionId && this.currentCaseType === caseTypeId;
    };
    var SearchService_1;
    SearchService.V2_MEDIATYPE_SEARCH_INPUTS = 'application/vnd.uk.gov.hmcts.ccd-data-store-api.ui-search-input-details.v2+json;charset=UTF-8';
    SearchService.VIEW_SEARCH = 'SEARCH';
    SearchService.VIEW_WORKBASKET = 'WORKBASKET';
    SearchService.FIELD_PREFIX = 'case.';
    SearchService = SearchService_1 = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [AbstractAppConfig,
            HttpService,
            RequestOptionsBuilder])
    ], SearchService);
    return SearchService;
}());
export { SearchService };
//# sourceMappingURL=search.service.js.map