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 { AbstractAppConfig } from '../../../app.config';
import { AddressesModel } from '../../domain/addresses';
import { HttpErrorService } from '../http';
import { map, catchError } from 'rxjs/operators';
import { plainToClass } from 'class-transformer';
import { throwError } from 'rxjs';
import { HttpClient } from '@angular/common/http';
var AddressesService = /** @class */ (function () {
    function AddressesService(httpClient, errorService, appConfig) {
        this.httpClient = httpClient;
        this.errorService = errorService;
        this.appConfig = appConfig;
    }
    AddressesService.prototype.getAddressesForPostcode = function (postcode) {
        var _this = this;
        return this.httpClient
            .get(this.appConfig.getPostcodeLookupUrl().replace('${postcode}', postcode))
            .pipe(catchError(function (error) {
            _this.errorService.setError(error);
            return throwError(error);
        }), map(function (o) { return plainToClass(AddressesModel, o); }), map(function (output) { return output.addresses.map(function (address) { return _this.format(address); }); }));
    };
    AddressesService.prototype.format = function (addressModel) {
        return this.formatAddressLines(this.shiftAddressLinesUp(addressModel));
    };
    AddressesService.prototype.formatAddressLines = function (addressModel) {
        var _this = this;
        ['AddressLine1', 'AddressLine2', 'AddressLine3', 'PostTown'].forEach(function (value) {
            addressModel[value] = _this.toCapitalCase(addressModel[value]);
        });
        return addressModel;
    };
    AddressesService.prototype.shiftAddressLinesUp = function (addressModel) {
        if (addressModel.AddressLine2 === '') {
            addressModel.AddressLine2 = addressModel.AddressLine3;
            addressModel.AddressLine3 = '';
        }
        if (addressModel.AddressLine1 === '') {
            addressModel.AddressLine1 = addressModel.AddressLine2;
            addressModel.AddressLine2 = '';
        }
        return addressModel;
    };
    AddressesService.prototype.toCapitalCase = function (sentence) {
        sentence = sentence.toLowerCase();
        sentence.split(' ').forEach(function (value, index) {
            sentence = sentence.replace(value, value.charAt(0).toUpperCase() + value.substr(1));
        });
        return sentence;
    };
    AddressesService = __decorate([
        Injectable(),
        __metadata("design:paramtypes", [HttpClient, HttpErrorService, AbstractAppConfig])
    ], AddressesService);
    return AddressesService;
}());
export { AddressesService };
//# sourceMappingURL=addresses.service.js.map