"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
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 __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Api = exports.ApiService = void 0;
const http = __importStar(require("http"));
const config_1 = __importDefault(require("../config"));
const Loggify_1 = require("../decorators/Loggify");
const logger_1 = __importDefault(require("../logger"));
const routes_1 = __importDefault(require("../routes"));
const config_2 = require("./config");
const socket_1 = require("./socket");
const storage_1 = require("./storage");
let ApiService = class ApiService {
    constructor({ port = 3000, timeout = 600000, configService = config_2.Config, storageService = storage_1.Storage, socketService = socket_1.Socket } = {}) {
        this.stopped = true;
        this.port = Number(process.env.BITCORE_NODE_HTTP_PORT) || port;
        this.timeout = timeout;
        this.configService = configService;
        this.storageService = storageService;
        this.socketService = socketService;
        this.app = routes_1.default;
        this.httpServer = new http.Server(routes_1.default);
    }
    async start() {
        if (this.configService.isDisabled('api')) {
            logger_1.default.info('Disabled API Service');
            return;
        }
        if (!this.storageService.connected) {
            await this.storageService.start({});
        }
        if (this.stopped) {
            this.stopped = false;
            this.httpServer = new http.Server(routes_1.default);
            this.httpServer.timeout = this.timeout;
            this.httpServer.listen(this.port, () => {
                logger_1.default.info(`Starting API Service on port ${this.port}`);
                this.socketService.start({ server: this.httpServer });
            });
        }
        return this.httpServer;
    }
    async stop() {
        this.stopped = true;
        await this.socketService.stop();
        return new Promise(resolve => {
            this.httpServer.close(() => {
                logger_1.default.info('Stopped API Service');
                resolve();
            });
            this.httpServer.emit('close');
        });
    }
};
exports.ApiService = ApiService;
exports.ApiService = ApiService = __decorate([
    Loggify_1.LoggifyClass
], ApiService);
// TOOO: choose a place in the config for the API timeout and include it here
exports.Api = new ApiService({
    port: config_1.default.port
});
//# sourceMappingURL=api.js.map