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, catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
import { AbstractAppConfig } from '../../../app.config';
import { RequestOptionsBuilder } from '../request';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { HttpErrorService } from '../http';
import { SearchHttpParameterCodec } from './search-http-parameter-codec';
var SearchService = /** @class */ (function () {
    function SearchService(appConfig, httpClient, requestOptionsBuilder, errorService) {
        this.appConfig = appConfig;
        this.httpClient = httpClient;
        this.requestOptionsBuilder = requestOptionsBuilder;
        this.errorService = errorService;
    }
    SearchService_1 = SearchService;
    SearchService.prototype.search = function (jurisdictionId, caseTypeId, metaCriteria, caseCriteria, view) {
        var _this = this;
        var url = this.appConfig.getApiUrl() + "/caseworkers/:uid"
            + ("/jurisdictions/" + jurisdictionId)
            + ("/case-types/" + caseTypeId)
            + "/cases";
        var options = this.requestOptionsBuilder.buildOptions(metaCriteria, caseCriteria, view);
        if (options && options.params) {
            options.params = new HttpParams({
                fromObject: options.params,
                encoder: new SearchHttpParameterCodec(),
            });
        }
        return this.httpClient
            .get(url, options)
            .pipe(catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    SearchService.prototype.getSearchInputs = function (jurisdictionId, caseTypeId) {
        var _this = this;
        var url = this.getSearchInputUrl(caseTypeId);
        var headers = new HttpHeaders({
            'Accept': [SearchService_1.V2_MEDIATYPE_SEARCH_INPUTS, SearchService_1.QC_V2_MEDIATYPE_SEARCH_INPUTS],
        });
        this.currentJurisdiction = jurisdictionId;
        this.currentCaseType = caseTypeId;
        return this.httpClient
            .get(url, { headers: headers })
            .pipe(map(function (response) {
            var searchInputs = response.searchInputs;
            if (_this.isDataValid(jurisdictionId, caseTypeId)) {
                searchInputs.forEach(function (item) {
                    item.field.label = item.label;
                });
            }
            else {
                throw new Error('Response expired');
            }
            return searchInputs;
        }), catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }));
    };
    SearchService.prototype.getSearchInputUrl = function (caseTypeId) {
        return this.appConfig.getCaseDataUrl() + "/internal/case-types/" + caseTypeId + "/search-inputs";
    };
    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.QC_V2_MEDIATYPE_SEARCH_INPUTS = 'application/vnd.app.quickcase.store.data.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,
            HttpClient,
            RequestOptionsBuilder,
            HttpErrorService])
    ], SearchService);
    return SearchService;
}());
export { SearchService };
//# sourceMappingURL=search.service.js.map