const parsers = require('../parsers')
const transform = require('../transform')
const request = require('../request')
const { ConnectionFailedError, EmptyParseOutputError } = require('../errors')

async function parse (parser, text) {
  const parsed = await parsers[parser](text)
  if (!parsed) throw new EmptyParseOutputError()
  return transform(parsed)
}


module.exports = async function parseFromQuery ({ url, parser }) {
  let content;
  try {
    content = await request(url)
  } catch(error) {
    if (error.code === "ENOTFOUND") {
      throw new ConnectionFailedError(url)
    }
    throw error;
  }

  if (parser) {
    return parse(parser, content)
  } else {
    for (let i = 0; i < parsers.keys.length; i++) {
      try {
        return parse(parsers.keys[i], content)
      } catch (error) {
        if (i !== parsers.keys.length - 1) {
          throw error
        }
        continue
      }
    }
  }
}

@@ -352,17 +352,16 @@
 sed)%0A%7D%0A%0A
-%0A
 module.e
@@ -432,17 +432,16 @@
  content
-;
 %0A  try %7B
@@ -483,16 +483,17 @@
  %7D catch
+ 
 (error) 
@@ -517,17 +517,17 @@
 ode === 
-%22
+'
 ENOTFOUN
@@ -531,9 +531,9 @@
 OUND
-%22
+'
 ) %7B%0A
@@ -577,24 +577,24 @@
 (url)%0A    %7D%0A
+
     throw er
@@ -596,17 +596,16 @@
 ow error
-;
 %0A  %7D%0A%0A  

Fix lint warnings