"use strict";
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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeoMeasurementHelper = void 0;
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const cheap_ruler_1 = __importDefault(require("cheap-ruler"));
const APPROXIMATION_LATITUDE = 50.0;
let GeoMeasurementHelper = class GeoMeasurementHelper {
    constructor() {
        this.rulerInstanceDict = new Map();
    }
    /** Get distance in kilometers between two points */
    getDistanceInKilometers(pointA, pointB) {
        const validPointA = this.getValidPoint(pointA);
        const validPointB = this.getValidPoint(pointB);
        return this.getRulerInstance("kilometers").distance(validPointA, validPointB);
    }
    /** Get distance in meters between two points */
    getDistanceInMeters(pointA, pointB) {
        const validPointA = this.getValidPoint(pointA);
        const validPointB = this.getValidPoint(pointB);
        return this.getRulerInstance("meters").distance(validPointA, validPointB);
    }
    /** Get buffered bounding box in kilometers */
    getBufferedBBoxInKilometers(point, bufferInKm) {
        const validPoint = this.getValidPoint(point);
        return this.getRulerInstance("kilometers").bufferPoint(validPoint, bufferInKm);
    }
    /** Check if point is inside bounding box */
    isPointInBBox(point, bbox) {
        const validPoint = this.getValidPoint(point);
        return this.getRulerInstance("kilometers").insideBBox(validPoint, bbox);
    }
    getRulerInstance(unit) {
        let ruler = this.rulerInstanceDict.get(unit);
        if (!ruler) {
            ruler = new cheap_ruler_1.default(APPROXIMATION_LATITUDE, unit);
            this.rulerInstanceDict.set(unit, ruler);
        }
        return ruler;
    }
    getValidPoint(point) {
        if (point.length !== 2) {
            throw new golemio_errors_1.GeneralError("getValidPoint: invalid point " + JSON.stringify(point), this.constructor.name, undefined, 422);
        }
        return point;
    }
};
exports.GeoMeasurementHelper = GeoMeasurementHelper;
exports.GeoMeasurementHelper = GeoMeasurementHelper = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], GeoMeasurementHelper);
//# sourceMappingURL=GeoMeasurementHelper.js.map