"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startServer = startServer;
const dotenv_1 = __importDefault(require("dotenv"));
const app_1 = require("./app");
const default_1 = require("../sdk/default");
dotenv_1.default.config();
const DEFAULT_PORT = 3001;
async function startServer() {
    var _a;
    const app = (0, app_1.createApp)();
    const port = Number.parseInt((_a = process.env.PORT) !== null && _a !== void 0 ? _a : `${DEFAULT_PORT}`, 10);
    try {
        await default_1.defaultSdk.initialize({
            startupPassword: process.env.DZAP_STARTUP_PASSWORD,
            syncProviders: true,
            initializeRetrieval: true,
        });
    }
    catch (error) {
        if (error instanceof Error && error.message.includes("Invalid startup password.")) {
            throw error;
        }
        console.error("[Setup] Startup initialization failed:", error);
    }
    app.listen(port, () => {
        console.log(`Server is running on http://localhost:${port}`);
    });
    default_1.defaultSdk.startSchedulerLoop(60 * 1000);
}
if (process.env.NODE_ENV !== "test") {
    startServer().catch((error) => {
        console.error("Failed to start server:", error);
        process.exitCode = 1;
    });
}
