"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeocoderPositionStackService = exports.GeocoderPositionStackRepositoryProvider = exports.GeocoderPositionStackDataSource = void 0;
const tslib_1 = require("tslib");
/* eslint-disable @typescript-eslint/naming-convention */
const core_1 = require("@loopback/core");
const repository_1 = require("@loopback/repository");
const service_proxy_1 = require("@loopback/service-proxy");
const application_error_1 = require("./../../../helper/application-error");
const server_config_service_1 = require("./../../server-config/server-config.service");
const geocoder_position_stack_const_1 = require("./geocoder-position-stack.const");
const config = {
    name: geocoder_position_stack_const_1.DATASTORE_NAME_GEOCODER_POSITION_STACK,
    connector: 'rest',
    options: {
        headers: {
            accept: 'application/json',
            'content-type': 'application/json',
        },
    },
    operations: [
        {
            template: {
                method: 'GET',
                url: 'http://api.positionstack.com/v1/forward?access_key={accessKey:string}&limit=1&output=json&query={query:string}',
            },
            functions: {
                geocode: ['accessKey', 'query'],
            },
        },
    ],
};
// Observe application's life cycle to disconnect the datasource when
// application is stopped. This allows the application to be shut down
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
let GeocoderPositionStackDataSource = class GeocoderPositionStackDataSource extends repository_1.juggler.DataSource {
    constructor(dsConfig = config) {
        super(dsConfig);
    }
};
GeocoderPositionStackDataSource.dataSourceName = geocoder_position_stack_const_1.DATASTORE_NAME_GEOCODER_POSITION_STACK;
GeocoderPositionStackDataSource.defaultConfig = config;
GeocoderPositionStackDataSource = tslib_1.__decorate([
    core_1.lifeCycleObserver('datasource'),
    tslib_1.__param(0, core_1.inject('datasources.config.' + geocoder_position_stack_const_1.DATASTORE_NAME_GEOCODER_POSITION_STACK, {
        optional: true,
    })),
    tslib_1.__metadata("design:paramtypes", [Object])
], GeocoderPositionStackDataSource);
exports.GeocoderPositionStackDataSource = GeocoderPositionStackDataSource;
let GeocoderPositionStackRepositoryProvider = class GeocoderPositionStackRepositoryProvider {
    constructor(
    // geocoder must match the name property in the datasource json file
    dataSource = new GeocoderPositionStackDataSource()) {
        this.dataSource = dataSource;
    }
    value() {
        return service_proxy_1.getService(this.dataSource);
    }
};
GeocoderPositionStackRepositoryProvider = tslib_1.__decorate([
    tslib_1.__param(0, core_1.inject('datasources.' + geocoder_position_stack_const_1.DATASTORE_NAME_GEOCODER_POSITION_STACK)),
    tslib_1.__metadata("design:paramtypes", [GeocoderPositionStackDataSource])
], GeocoderPositionStackRepositoryProvider);
exports.GeocoderPositionStackRepositoryProvider = GeocoderPositionStackRepositoryProvider;
let GeocoderPositionStackService = class GeocoderPositionStackService {
    constructor(geocoderRepository, serverConfigService) {
        this.geocoderRepository = geocoderRepository;
        this.serverConfigService = serverConfigService;
        this.serverConfigService.registerNewConfigurationKey('positionStackApiKey', { type: 'string', title: 'positionStack Api Key' });
    }
    getName() {
        return geocoder_position_stack_const_1.DATASTORE_NAME_GEOCODER_POSITION_STACK;
    }
    async getAddressLocation(address, isAddressUriEncoded = false, ctx) {
        const addressUriEncoded = isAddressUriEncoded
            ? address
            : encodeURIComponent(address);
        const secret = await this.serverConfigService.get('positionStackApiKey');
        if (!secret) {
            throw application_error_1.ApplicationError.missingParameter('serverConfigService.positionStackApiKey');
        }
        return (await this.geocoderRepository.geocode(secret, addressUriEncoded))
            .data[0];
    }
};
GeocoderPositionStackService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.inject('services.GeocoderPositionStackRepository')),
    tslib_1.__param(1, core_1.service(server_config_service_1.ServerConfigService)),
    tslib_1.__metadata("design:paramtypes", [Object, server_config_service_1.ServerConfigService])
], GeocoderPositionStackService);
exports.GeocoderPositionStackService = GeocoderPositionStackService;
//# sourceMappingURL=geocoder-position-stack.service.js.map