<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>5Minds Engine API Documentation</title>
    <link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
    <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
    <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
    <style id="rootstyles" nonce="<%= NONCE_PLACHOLDER %>">
      html {
        box-sizing: border-box;
        overflow: -moz-scrollbars-vertical;
        overflow-y: scroll;
      }

      *,
      *:before,
      *:after {
        box-sizing: inherit;
      }

      body {
        margin: 0;
        background: #fafafa;
      }
    </style>
  </head>

  <body>
    <div id="swagger-ui"></div>

    <script src="./swagger-ui-bundle.js" nonce="<%= NONCE_PLACHOLDER %>"></script>
    <script src="./swagger-ui-standalone-preset.js" nonce="<%= NONCE_PLACHOLDER %>"></script>
    <script nonce="<%= NONCE_PLACHOLDER %>">
      /** This is a hack to get the styles from the inline generated style tag in the svg because of the CSP */
      window.addEventListener('load', function () {
        function addStyles() {
          setTimeout(() => {
            const inlineGeneratedStyleTag = document.querySelector('.swagger-ui svg style');
            if (inlineGeneratedStyleTag) {
              document.getElementById('rootstyles').innerHTML += inlineGeneratedStyleTag.innerHTML;
            } else {
              addStyles();
            }
          }, 16);
        }

        addStyles();
      });

      window.onload = function () {
        const serverUrl = window.location.origin + window.location.pathname;

        const ui = SwaggerUIBundle({
          url: `${serverUrl}atlas_engine/api/v1/swagger`,
          dom_id: '#swagger-ui',
          deepLinking: true,
          defaultModelRendering: 'model',
          presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
          plugins: [SwaggerUIBundle.plugins.DownloadUrl],
          layout: 'StandaloneLayout',
          defaultModelsExpandDepth: 0,
          requestInterceptor: (request) => {
            const serverUrl = window.location.origin + window.location.pathname;

            const urlIncludesPathname = request.url.includes(serverUrl);
            const path = urlIncludesPathname ? request.url.split(`${window.location.origin}${window.location.pathname}`)[1] : request.url.split(`${window.location.origin}/`)[1];
            if (path) {
              request.url = `${serverUrl}${path}`;
            } else {
              request.url = `${serverUrl}`;
            }

            return request;
          },
        });

        window.ui = ui;
      };
    </script>
  </body>
</html>
