"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.GtfsStopsRedisRepository = void 0;
const const_1 = require("../../../../schema-definitions/ropid-gtfs/redis/const");
const IoRedisConnector_1 = require("@golemio/core/dist/helpers/data-access/redis/IoRedisConnector");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const ioc_1 = require("@golemio/core/dist/output-gateway/ioc");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
let GtfsStopsRedisRepository = class GtfsStopsRedisRepository {
    constructor(redisConnector, log) {
        this.redisConnector = redisConnector;
        this.log = log;
        this.setName = undefined; // loaded via pub sub or loaded from redis
    }
    async checkSetNameAndConnection() {
        if (!this.redisConnector.isConnected()) {
            await this.redisConnector.connect();
        }
        const connection = this.redisConnector.getConnection();
        if (!this.setName) {
            const loadedSetName = await connection.get(`${const_1.GTFS_STOPS_NAMESPACE_PREFIX}:activeSetName`);
            if (!loadedSetName) {
                this.log.debug(`Empty setName for ${const_1.GTFS_STOPS_NAMESPACE_PREFIX}`);
                return null;
            }
            else {
                this.setName = loadedSetName;
            }
        }
        return connection;
    }
    setCurrentSetName(name) {
        this.setName = name;
    }
    async getAswNodeIdFromCisId(cisId) {
        const connection = await this.checkSetNameAndConnection();
        if (!connection) {
            return null;
        }
        try {
            return await connection.hget(`${this.setName}:cisToAswNode`, cisId);
        }
        catch (error) {
            throw new golemio_errors_1.GeneralError("Cannot aswNodeId from cache", this.constructor.name, error);
        }
    }
    async getGtfsStopIdsByAswNode(aswNodeId) {
        const connection = await this.checkSetNameAndConnection();
        if (!connection) {
            return [];
        }
        try {
            const listOfGtfsStops = await connection.hget(`${this.setName}:aswNodeToGtfsStops`, aswNodeId);
            if (!listOfGtfsStops) {
                return [];
            }
            return listOfGtfsStops.split(",");
        }
        catch (error) {
            throw new golemio_errors_1.GeneralError("Cannot gtfsStopIds from cache", this.constructor.name, error);
        }
    }
    async getGtfsStopIdsForAswNodes(aswNodes) {
        const result = new Map();
        const connection = await this.checkSetNameAndConnection();
        if (!connection)
            return result;
        try {
            const foundStopsLists = await connection.hmget(`${this.setName}:aswNodeToGtfsStops`, ...aswNodes);
            if (!foundStopsLists?.length)
                return result;
            for (const [index, node] of aswNodes.entries()) {
                const line = foundStopsLists[index];
                if (line) {
                    result.set(node, line.split(","));
                }
            }
            return result;
        }
        catch (error) {
            throw new golemio_errors_1.GeneralError("Cannot gtfsStopIds from cache", this.constructor.name, error);
        }
    }
    async getGtfsStopsDetailsByAswId(aswIds) {
        const connection = await this.checkSetNameAndConnection();
        if (!connection) {
            throw new golemio_errors_1.GeneralError("Unable to get connection", this.constructor.name);
        }
        try {
            const stopsDetail = await connection.hmget(`${this.setName}:staticData`, ...aswIds);
            if (!stopsDetail) {
                throw new golemio_errors_1.GeneralError(`Unable to get stops details by aswId: ${aswIds}`, this.constructor.name);
            }
            const res = [];
            for (const stopDetail of stopsDetail) {
                if (stopDetail === null)
                    continue;
                res.push(JSON.parse(stopDetail));
            }
            return res;
        }
        catch (error) {
            throw new golemio_errors_1.GeneralError("Cannot get stops details from cache", this.constructor.name, error);
        }
    }
};
exports.GtfsStopsRedisRepository = GtfsStopsRedisRepository;
exports.GtfsStopsRedisRepository = GtfsStopsRedisRepository = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(ioc_1.ContainerToken.RedisConnector)),
    __param(1, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.Logger)),
    __metadata("design:paramtypes", [IoRedisConnector_1.IoRedisConnector, Object])
], GtfsStopsRedisRepository);
//# sourceMappingURL=GtfsStopsRedisRepository.js.map