"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
const { createRoutingView } = require("../../db/views/staticViews");
const { ViewNames, getQueryIndex, UNICODE_MAX } = require("../../db/utils");
const { getAppDB } = require("@devlego/backend-core/context");
exports.getRoutingInfo = () => __awaiter(void 0, void 0, void 0, function* () {
    const db = getAppDB();
    try {
        const allRouting = yield db.query(getQueryIndex(ViewNames.ROUTING), {
            startKey: "",
            endKey: UNICODE_MAX,
        });
        return allRouting.rows.map(row => row.value);
    }
    catch (err) {
        // check if the view doesn't exist, it should for all new instances
        /* istanbul ignore next */
        if (err != null && err.name === "not_found") {
            yield createRoutingView();
            return exports.getRoutingInfo();
        }
        else {
            throw err;
        }
    }
});
exports.createRoutingView = createRoutingView;
