"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "setupSwagger", {
    enumerable: true,
    get: function() {
        return setupSwagger;
    }
});
const _generator = require("./generator");
const _express = require("express");
const _nodepath = require("node:path");
const _serverconstants = require("../../server.constants");
const _fsutils = require("../fs.utils");
const _promises = require("node:fs/promises");
function getSwaggerUiDistPath() {
    try {
        return (0, _nodepath.dirname)(require.resolve("swagger-ui-dist/package.json"));
    } catch  {
        return (0, _nodepath.join)(process.cwd(), "node_modules", "swagger-ui-dist");
    }
}
function generateSwaggerHTML() {
    return `
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>FDM Monster API Documentation</title>
  <link rel="stylesheet" href="/api-docs/static/swagger-ui.css" />
  <style>
    html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; }
    *, *:before, *:after { box-sizing: inherit; }
    body { margin: 0; padding: 0; }
  </style>
</head>
<body>
  <div id="swagger-ui"></div>
  <script src="/api-docs/static/swagger-ui-bundle.js"></script>
  <script src="/api-docs/static/swagger-ui-standalone-preset.js"></script>
  <script>
    window.onload = function() {
      window.ui = SwaggerUIBundle({
        url: '/api-docs/swagger.json',
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout"
      });
    };
  </script>
</body>
</html>
  `.trim();
}
async function setupSwagger(app, logger) {
    const generator = new _generator.SwaggerGenerator(logger);
    const specification = await generator.generate();
    const generateJsonFile = process.env[_serverconstants.AppConstants.GENERATE_SWAGGER_JSON] === "true";
    if (generateJsonFile) {
        try {
            const mediaPath = (0, _fsutils.getMediaPath)();
            (0, _fsutils.ensureDirExists)(mediaPath);
            const swaggerJsonPath = (0, _nodepath.join)(mediaPath, "swagger.json");
            const jsonContent = JSON.stringify(specification, null, 2);
            await (0, _promises.writeFile)(swaggerJsonPath, jsonContent, "utf-8");
            logger.log(`Swagger JSON file generated at: ${swaggerJsonPath}`);
        } catch (error) {
            logger.error("Failed to generate swagger.json file", error);
        }
    }
    const swaggerUiPath = getSwaggerUiDistPath();
    app.use("/api-docs/static", (0, _express.static)(swaggerUiPath));
    app.get("/api-docs/swagger.json", (_req, res)=>{
        res.setHeader("Content-Type", "application/json");
        res.send(specification);
    });
    app.get("/api-docs", (_req, res)=>{
        res.setHeader("Content-Type", "text/html");
        res.send(generateSwaggerHTML());
    });
    return specification;
}

//# sourceMappingURL=swagger.js.map