"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperatorRegistry = void 0;
const tsyringe_1 = require("tsyringe");
const utils_1 = require("@streamr/utils");
const lodash_1 = require("lodash");
const LoggerFactory_1 = require("../utils/LoggerFactory");
let OperatorRegistry = class OperatorRegistry {
    theGraphClient;
    logger;
    constructor(theGraphClient, loggerFactory) {
        this.theGraphClient = theGraphClient;
        this.logger = loggerFactory.createLogger(module);
    }
    async findRandomNetworkEntrypoints(maxEntryPoints, maxQueryResults, maxHeartbeatAgeHours) {
        const createQuery = () => {
            return {
                query: `{
                    operators(
                        orderBy: latestHeartbeatTimestamp
                        orderDirection: desc
                        first: ${maxQueryResults}
                        where: {
                            latestHeartbeatMetadata_contains: "\\"tls\\":true", 
                            latestHeartbeatTimestamp_gt: "${Math.floor(Date.now() / 1000) - (maxHeartbeatAgeHours * 60 * 60)}"
                        }
                    ) {
                        id
                        latestHeartbeatMetadata
                    }
                }`
            };
        };
        const operatorMetadatas = this.theGraphClient.queryEntities(createQuery);
        const peerDescriptors = [];
        for await (const operator of operatorMetadatas) {
            peerDescriptors.push(JSON.parse(operator.latestHeartbeatMetadata));
        }
        const picked = (0, lodash_1.shuffle)(peerDescriptors).slice(0, maxEntryPoints);
        this.logger.debug(`Found ${peerDescriptors.length} network entrypoints, picked ${picked.length}`, { picked });
        return picked;
    }
};
exports.OperatorRegistry = OperatorRegistry;
exports.OperatorRegistry = OperatorRegistry = __decorate([
    (0, tsyringe_1.scoped)(tsyringe_1.Lifecycle.ContainerScoped),
    __metadata("design:paramtypes", [utils_1.TheGraphClient,
        LoggerFactory_1.LoggerFactory])
], OperatorRegistry);
//# sourceMappingURL=OperatorRegistry.js.map