let UrlPattern = require('url-pattern')
const logger = require('../lib/Logger').getLogger('proxy-custom')

module.exports = {
  load,
  add,
  match,
  resolve
}

const patterns = {}

function load(rules) {
  for (let name of Object.keys(rules)) {
    let pattern = new UrlPattern(name)
    let address = rules[name]
    if (!address) address = pattern.stringify()
    patterns[name] = {
      pattern: pattern,
      address: address
    }
  }
}

function add(rules) {
  load(rules)
}

function match(path) {
  for (let name of Object.keys(patterns)) {
    let match = patterns[name].pattern.match(path)
    if (match) {
      logger.trace(match + ' ' + patterns[name].pattern.stringify() + ' ' + patterns[name].pattern.stringify(match))
    }
  }
}

/**
 * Get the real path and REST params
 * Note that querystring params are located along with the path
 * @param {String} path -A URL path to match against current routing table
 *@return {Object} {path:the real path, params: the REST params}
 */
function resolve(path) {
  let result
  for (let name of Object.keys(patterns)) {
    let pattern = patterns[name].pattern
    let match = pattern.match(path)
    if (match) {
      let mainpath = patterns[name].address
      if (match['_']) {
        //pure URL (/p/a/t/h/?abcd)
        let subpath = match['_']
        if (!subpath) subpath = ''
        if (mainpath.endsWith('/') === false) mainpath = mainpath + '/'
        if (subpath.startsWith('/')) subpath = subpath.substring(1)
        result = {
          path: mainpath + subpath,
          params: match
        }
      } else {
        //REST service
        result = {
          path: mainpath,
          params: match
        }
      }
      return result;
    }
  }
  //if not found return the path itself
  return {
    path: path,
    params: null
  }
}

@@ -263,22 +263,25 @@
 n = 
-new UrlPattern
+patchIPParameters
 (nam
@@ -437,23 +437,507 @@
 s%0A    %7D%0A
-
   %7D%0A%7D%0A%0A
+/**%0A * REST params can contain dots. I.e: ips/17.223.37.12, properties/database.user%0A * @return %7BURLPattern%7D%0A */%0Afunction patchIPParameters(name) %7B%0A  const pattern = new UrlPattern(name)%0A  let regex = pattern.regex.toString()%0A  if (regex.includes('%5B')) %7B%0A    regex = regex.split('%5B').map((el, i) =%3E i === 0 ? el : '%5C%5C.' + el).reduce((a, el) =%3E a + '%5B' + el)%0A    regex = regex.substring(1, regex.length - 2)%0A    pattern.regex = new RegExp(regex, pattern.flags)%0A  %7D%0A  return pattern%0A%7D%0A%0A
 function
@@ -2299,13 +2299,12 @@
 : null%0A  %7D%0A%7D
-%0A

allow /IPs and /a.b.c as REST parameters