"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GtfsStopParser = void 0;
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const sequelize_1 = require("@golemio/core/dist/shared/sequelize");
const const_1 = require("../const");
const aswNodeMatch = /\d+/;
const nonNumericRegExp = /[^\d]+/g;
class GtfsStopParser {
    static isValidInteger(value) {
        return value >= const_1.PG_INT_MIN && value <= const_1.PG_INT_MAX;
    }
    static prepareAswWhereOptions(aswIds) {
        let aswWhereOptions = [];
        for (const aswId of aswIds) {
            const { aswNode, aswStopId } = GtfsStopParser.parseAswId(aswId);
            if (!aswNode) {
                continue;
            }
            GtfsStopParser.validateAswId(aswId, aswNode, aswStopId);
            aswWhereOptions.push(sequelize_1.Sequelize.and(sequelize_1.Sequelize.where(sequelize_1.Sequelize.col("asw_node_id"), "=", aswNode), aswStopId && sequelize_1.Sequelize.where(sequelize_1.Sequelize.col("asw_stop_id"), "=", aswStopId)));
        }
        return aswWhereOptions;
    }
    static validateAswId(aswId, nodeId, stopId) {
        const nodeIdNum = Number(nodeId);
        if (!GtfsStopParser.isValidInteger(nodeIdNum) || (stopId && !GtfsStopParser.isValidInteger(Number(stopId)))) {
            throw new golemio_errors_1.GeneralError(`Invalid ASW ID: ${aswId}`, GtfsStopParser.constructor.name, `Invalid ASW ID: ${aswId}`, 400);
        }
    }
    static parseAswId(aswId) {
        const [aswNode, aswStopId] = aswId.replaceAll("_", "/").split("/");
        return { aswNode, aswStopId };
    }
    static getAswNodeFromId(aswId) {
        return GtfsStopParser.parseAswId(aswId).aswNode;
    }
    static normalizedStopId(stopId) {
        return stopId
            .split(nonNumericRegExp)
            .filter((s) => !!s.length)
            .join("/");
    }
    static normalizedNodeId(stopId) {
        return (aswNodeMatch.exec(stopId) || [])[0] ?? "";
    }
}
exports.GtfsStopParser = GtfsStopParser;
//# sourceMappingURL=GtfsStopParser.js.map