const isURL = require('is-url')
const got = require('got')
const schemas = require('./schemas')

module.exports = async function getSchema (schemaName) {
  if (!schemaName) schemaName = 'api.github.com-deref'

  // allow test/staging schemas to be loaded from remote URLs
  if (isURL(schemaName)) {
    const schema = await got(schemaName).json()
    return schema
  }

  const schemaNames = Object.keys(schemas)
  if (!schemaNames.includes(schemaName)) {
    throw new Error(`There is no schema named '${schemaName}'. Available names: ${schemaNames.join(', ')}`)
  }
  return schemas[schemaName]
}
