"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PublicVehiclePositionsFacade = void 0;
const output_gateway_1 = require("@golemio/core/dist/output-gateway");
const PublicVehiclePositionsTransformation_1 = require("../transformations/PublicVehiclePositionsTransformation");
class PublicVehiclePositionsFacade {
    constructor(repository, boundingBoxHelper, log) {
        this.repository = repository;
        this.boundingBoxHelper = boundingBoxHelper;
        this.log = log;
        this.transformation = new PublicVehiclePositionsTransformation_1.PublicVehiclePositionsTransformation();
    }
    async getAll(boundingBox, routeShortName, type) {
        const boundingBoxWithCenter = this.boundingBoxHelper.mapToCenterFormat(boundingBox);
        this.log.debug(`width: ${boundingBoxWithCenter.width}m, height: ${boundingBoxWithCenter.height}m`);
        const vehicleIds = await this.repository.getAllVehicleIds(boundingBoxWithCenter);
        if (vehicleIds.length === 0) {
            this.log.debug("No vehicle ids found.");
            return (0, output_gateway_1.buildGeojsonFeatureCollection)([]);
        }
        const vehiclePositions = await this.repository.getAllVehiclePositions(vehicleIds);
        const filteredResults = vehiclePositions.filter((el) => this.filterClientSide(el, routeShortName, type));
        if (filteredResults.length === 0) {
            this.log.debug("No vehicle positions found.");
            return (0, output_gateway_1.buildGeojsonFeatureCollection)([]);
        }
        return (0, output_gateway_1.buildGeojsonFeatureCollection)(this.transformation.transformArray(filteredResults));
    }
    filterClientSide(el, routeShortName, types) {
        if (routeShortName instanceof Array && routeShortName.includes(el.gtfs_route_short_name)) {
            return true;
        }
        if (routeShortName && el.gtfs_route_short_name !== routeShortName) {
            return false;
        }
        if (types && types.every((type) => type !== el.route_type)) {
            return false;
        }
        return true;
    }
}
exports.PublicVehiclePositionsFacade = PublicVehiclePositionsFacade;
//# sourceMappingURL=VehiclePositionsFacade.js.map