/**
 * OpenAPI Validator
 * 
 * Ported from Hoppscotch's openapi-import-worker.ts
 * Converts Web Worker pattern to direct function calls for Next.js
 */

import SwaggerParser from '@apidevtools/swagger-parser'
import type { OpenAPI } from 'openapi-types'

/**
 * Validates an OpenAPI document using SwaggerParser
 * 
 * @param docs - The OpenAPI document to validate
 * @returns Promise resolving to validated OpenAPI document
 * @throws Error if validation fails
 */
export async function validateOpenAPISpec(
  docs: unknown
): Promise<OpenAPI.Document> {
  try {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const validated = await SwaggerParser.validate(docs as any, {
      // @ts-expect-error - this is a valid option, but types may not be updated
      continueOnError: true,
    })
    return validated as unknown as OpenAPI.Document
  } catch {
    throw new Error('COULD_NOT_VALIDATE')
  }
}
