"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DescriptorFilter = void 0;
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const VehicleDescriptorEnums_1 = require("../../../../../helpers/VehicleDescriptorEnums");
let DescriptorFilter = class DescriptorFilter {
    constructor(logger) {
        this.logger = logger;
        this.isValidState = (state) => {
            return Object.values(VehicleDescriptorEnums_1.VehicleDescriptorStateEnum).includes(state);
        };
        this.isValidTraction = (traction) => {
            return Object.values(VehicleDescriptorEnums_1.VehicleDescriptorTractionEnum).includes(traction);
        };
    }
    /**
     * Yield descriptors that are valid for saving (filter out duplicates entirely)
     *   - combination of registration number and gtfs route type is unique
     */
    *yieldFilteredDescriptors(descriptors) {
        const sortedDescriptors = descriptors.sort((a, b) => {
            return (this.getKeyWithState(a.registration_number, a.gtfs_route_type, a.state) -
                this.getKeyWithState(b.registration_number, b.gtfs_route_type, b.state));
        });
        const seenCombinations = new Set();
        for (const descriptor of sortedDescriptors) {
            const { state, traction, registration_number, gtfs_route_type } = descriptor;
            if (this.isValidState(state) && this.isValidTraction(traction)) {
                const combination = `${registration_number}-${gtfs_route_type}`;
                if (!seenCombinations.has(combination)) {
                    seenCombinations.add(combination);
                    yield descriptor;
                }
                else {
                    this.logger.info(`${this.constructor.name}: descriptor ${registration_number} for route type ${gtfs_route_type} is invalid`);
                }
            }
            else {
                this.logger.info(`${this.constructor.name}: invalid state: ${state} or traction: ${traction}`);
            }
        }
    }
    getKeyWithState(registration_number, gtfs_route_type, state) {
        return parseInt(`${registration_number}${gtfs_route_type}${Object.values(VehicleDescriptorEnums_1.VehicleDescriptorStateEnum).indexOf(state)}`);
    }
};
exports.DescriptorFilter = DescriptorFilter;
exports.DescriptorFilter = DescriptorFilter = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.Logger)),
    __metadata("design:paramtypes", [Object])
], DescriptorFilter);
//# sourceMappingURL=DescriptorFilter.js.map