/**
 * @prettier
 */
"use client";

import React, { useEffect, useCallback, useState } from "react";
import PropTypes from "prop-types";
import SwaggerUIConstructor from "#swagger-ui";
const {
  config
} = SwaggerUIConstructor;
const SwaggerUI = ({
  spec = config.defaults.spec,
  url = config.defaults.url,
  layout = config.defaults.layout,
  requestInterceptor = config.defaults.requestInterceptor,
  responseInterceptor = config.defaults.responseInterceptor,
  supportedSubmitMethods = config.defaults.supportedSubmitMethods,
  queryConfigEnabled = config.defaults.queryConfigEnabled,
  plugins = config.defaults.plugins,
  displayOperationId = config.defaults.displayOperationId,
  showMutatedRequest = config.defaults.showMutatedRequest,
  docExpansion = config.defaults.docExpansion,
  defaultModelExpandDepth = config.defaults.defaultModelExpandDepth,
  defaultModelsExpandDepth = config.defaults.defaultModelsExpandDepth,
  defaultModelRendering = config.defaults.defaultModelRendering,
  presets = config.defaults.presets,
  deepLinking = config.defaults.deepLinking,
  showExtensions = config.defaults.showExtensions,
  showCommonExtensions = config.defaults.showCommonExtensions,
  filter = config.defaults.filter,
  requestSnippetsEnabled = config.defaults.requestSnippetsEnabled,
  requestSnippets = config.defaults.requestSnippets,
  tryItOutEnabled = config.defaults.tryItOutEnabled,
  displayRequestDuration = config.defaults.displayRequestDuration,
  withCredentials = config.defaults.withCredentials,
  persistAuthorization = config.defaults.persistAuthorization,
  oauth2RedirectUrl = config.defaults.oauth2RedirectUrl,
  onComplete = null,
  activeOperationMethod = "",
  activeOperationPath = ""
}) => {
  const [system, setSystem] = useState(null);
  const SwaggerUIComponent = system?.getComponent("App", "root");
  const handleComplete = useCallback(() => {
    if (typeof onComplete === "function") {
      onComplete();
    }
  }, [onComplete]);
  useEffect(() => {
    setSystem(SwaggerUIConstructor({
      plugins,
      spec,
      url,
      layout,
      defaultModelsExpandDepth,
      defaultModelRendering,
      presets: [SwaggerUIConstructor.presets.apis, ...presets],
      requestInterceptor,
      responseInterceptor,
      onComplete: handleComplete,
      docExpansion,
      supportedSubmitMethods,
      queryConfigEnabled,
      defaultModelExpandDepth,
      displayOperationId,
      tryItOutEnabled,
      displayRequestDuration,
      requestSnippetsEnabled,
      requestSnippets,
      showMutatedRequest,
      deepLinking,
      showExtensions,
      showCommonExtensions,
      filter,
      persistAuthorization,
      withCredentials,
      ...(typeof oauth2RedirectUrl === "string" ? {
        oauth2RedirectUrl: oauth2RedirectUrl
      } : {})
    }));
  }, []);
  useEffect(() => {
    if (system) {
      const prevStateUrl = system.specSelectors.url();
      if (url !== prevStateUrl) {
        system.specActions.updateSpec("");
        if (url) {
          system.specActions.updateUrl(url);
          system.specActions.download(url);
        }
      }
      const prevStateSpec = system.specSelectors.specStr();
      if (spec && spec !== prevStateSpec) {
        const updatedSpec = typeof spec === "object" ? JSON.stringify(spec) : spec;
        system.specActions.updateSpec(updatedSpec);
      }
    }
  }, [url, spec]);
  return SwaggerUIComponent ? /*#__PURE__*/React.createElement(SwaggerUIComponent, {
    activeOperationMethod: activeOperationMethod,
    activeOperationPath: activeOperationPath
  }) : null;
};
SwaggerUI.System = SwaggerUIConstructor.System;
SwaggerUI.presets = SwaggerUIConstructor.presets;
SwaggerUI.plugins = SwaggerUIConstructor.plugins;
SwaggerUI.config = SwaggerUIConstructor.config;
export default SwaggerUI;

