"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeocoderNominatimService = exports.GeocoderNominatimRepositoryProvider = exports.GeocoderNominatimDataSource = 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 server_config_service_1 = require("../../server-config/server-config.service");
const geocoder_nominatim_const_1 = require("./geocoder-nominatim.const");
const config = {
    name: geocoder_nominatim_const_1.DATASTORE_NAME_GEOCODER_NOMINATIM,
    connector: 'rest',
    options: {
        headers: {
            accept: 'application/json',
            'content-type': 'application/json',
        },
    },
    operations: [
        {
            template: {
                method: 'GET',
                url: 'https://nominatim.openstreetmap.org/search.php?q={query:string}&polygon_geojson=1&format=jsonv2',
                headers: {
                    referer: '{referer:string}',
                    'user-agent': '{userAgent:string}',
                },
            },
            functions: {
                geocode: ['query', 'referer', 'userAgent'],
            },
        },
    ],
};
// 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 GeocoderNominatimDataSource = class GeocoderNominatimDataSource extends repository_1.juggler.DataSource {
    constructor(dsConfig = config) {
        super(dsConfig);
    }
};
GeocoderNominatimDataSource.dataSourceName = geocoder_nominatim_const_1.DATASTORE_NAME_GEOCODER_NOMINATIM;
GeocoderNominatimDataSource.defaultConfig = config;
GeocoderNominatimDataSource = tslib_1.__decorate([
    core_1.lifeCycleObserver('datasource'),
    tslib_1.__param(0, core_1.inject('datasources.config.' + geocoder_nominatim_const_1.DATASTORE_NAME_GEOCODER_NOMINATIM, {
        optional: true,
    })),
    tslib_1.__metadata("design:paramtypes", [Object])
], GeocoderNominatimDataSource);
exports.GeocoderNominatimDataSource = GeocoderNominatimDataSource;
let GeocoderNominatimRepositoryProvider = class GeocoderNominatimRepositoryProvider {
    constructor(
    // geocoder must match the name property in the datasource json file
    dataSource = new GeocoderNominatimDataSource()) {
        this.dataSource = dataSource;
    }
    value() {
        return service_proxy_1.getService(this.dataSource);
    }
};
GeocoderNominatimRepositoryProvider = tslib_1.__decorate([
    tslib_1.__param(0, core_1.inject('datasources.' + geocoder_nominatim_const_1.DATASTORE_NAME_GEOCODER_NOMINATIM)),
    tslib_1.__metadata("design:paramtypes", [GeocoderNominatimDataSource])
], GeocoderNominatimRepositoryProvider);
exports.GeocoderNominatimRepositoryProvider = GeocoderNominatimRepositoryProvider;
let GeocoderNominatimService = class GeocoderNominatimService {
    constructor(geocoderRepository, serverConfigService) {
        this.geocoderRepository = geocoderRepository;
        this.serverConfigService = serverConfigService;
    }
    getName() {
        return geocoder_nominatim_const_1.DATASTORE_NAME_GEOCODER_NOMINATIM;
    }
    async getAddressLocation(address, isAddressUriEncoded = false, ctx) {
        const addressUriEncoded = isAddressUriEncoded
            ? address
            : encodeURIComponent(address);
        const referer = ctx.uriContext.uri.value.headers.referer
            ? ctx.uriContext.uri.value.headers.referer
            : ctx.uriContext.uri.value.protocol +
                '://' +
                ctx.uriContext.uri.value.host;
        const userAgent = ctx.uriContext.uri.value.headers['user-agent']
            ? ctx.uriContext.uri.value.headers['user-agent']
            : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36';
        const coordinates = (await this.geocoderRepository.geocode(addressUriEncoded, referer, userAgent))[0];
        return {
            latitude: parseFloat(coordinates.lat),
            longitude: parseFloat(coordinates.lon),
        };
    }
};
GeocoderNominatimService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.inject('services.GeocoderNominatimRepository')),
    tslib_1.__param(1, core_1.service(server_config_service_1.ServerConfigService)),
    tslib_1.__metadata("design:paramtypes", [Object, server_config_service_1.ServerConfigService])
], GeocoderNominatimService);
exports.GeocoderNominatimService = GeocoderNominatimService;
//# sourceMappingURL=geocoder-nominatim.service.js.map