import { AsyncAPISchema } from '@mintlify/common/asyncapi';
import * as fs from 'fs/promises';
import yaml from 'js-yaml';
import * as path from 'path';

import { fetchAsyncApi } from '../utils/network.js';

export const getAsyncApiDefinition = async (
  pathOrDocumentOrUrl: string | AsyncAPISchema | URL
): Promise<{ document: AsyncAPISchema; isUrl: boolean }> => {
  if (typeof pathOrDocumentOrUrl === 'string') {
    if (pathOrDocumentOrUrl.startsWith('http://')) {
      // This is an invalid location either for a file or a URL
      throw new Error('Only HTTPS URLs are supported. Please provide an HTTPS URL');
    } else {
      try {
        const url = new URL(pathOrDocumentOrUrl);
        pathOrDocumentOrUrl = url;
      } catch {
        const pathname = path.join(process.cwd(), pathOrDocumentOrUrl.toString());
        const file = await fs.readFile(pathname, 'utf-8');
        pathOrDocumentOrUrl = yaml.load(file) as AsyncAPISchema;
      }
    }
  }
  const isUrl = pathOrDocumentOrUrl instanceof URL;
  if (pathOrDocumentOrUrl instanceof URL) {
    if (pathOrDocumentOrUrl.protocol !== 'https:') {
      throw new Error('Only HTTPS URLs are supported. Please provide an HTTPS URL');
    }
    pathOrDocumentOrUrl = await fetchAsyncApi(pathOrDocumentOrUrl);
  }

  return { document: pathOrDocumentOrUrl, isUrl };
};
