import openapiTS, { astToString } from 'openapi-typescript';

import fs from 'fs';

// They need to match with any custom schema added to CustomApiSchemas keys
const CUSTOMIZED_SCHEMAS = ['Webhook', 'AlertGroup', 'User'];

const addCustomSchemasToAutogeneratedOutput = (
  originalOutput: string,
  customizedSchemas: string[] = CUSTOMIZED_SCHEMAS
) => {
  const REGEX = new RegExp(`\\s(${customizedSchemas.join('|')})\\s*:`, 'g');

  let newOutput = `
      import type { CustomApiSchemas } from './types-generator/custom-schemas';
  
      ${originalOutput}
    `;
  newOutput = newOutput.replace(REGEX, (match) =>
    match.concat(` CustomApiSchemas["${match.split(':')[0].trim()}"] & `)
  );

  return newOutput;
};

(async () => {
  const ast = await openapiTS(new URL('http://localhost:8080/internal/schema/'));
  const output = astToString(ast);

  fs.writeFileSync(
    '../autogenerated-api.types.d.ts',
    addCustomSchemasToAutogeneratedOutput(output, CUSTOMIZED_SCHEMAS)
  );
})();
