'use strict';
var uritemplate = require('uritemplate'),
extend = require('./tiny-extend');

var templateCache = {};

function toKeysUsed(memo, expr) {
  if (expr.templateText) memo[expr.templateText] = true;
  return memo;
}

function ensureTrailingSlash(url) {
  return (url.charAt(url.length-1) === '/') ? url : (url + '/');
}

/**
 * Creates, evaluates based on context, and returns a string URL for a Mozu API request.
 * @param  {Object} context The context of a client. Should have a `baseUrl` property at minimum.
 * @param  {string} tpt     A string to be compiled into a UriTemplate. Should be a valid UriTemplate.
 * @param  {Object} body      An object consisting of the JSON body of the request, to be used to interpolate URL paramters.
 * @return {string}         A fully qualified URL.
 */
module.exports = function makeUrl(client, tpt, body) {
  var context = client.context,
    template = templateCache[tpt] && templateCache[tpt].template;
    if (!template) {
      template = uritemplate.parse(tpt);
      templateCache[tpt] = {
        template: template,
        keysUsed: template.expressions.reduce(toKeysUsed, {})
      };
    }
    var ctx = extend({
      homePod: context.baseUrl && ensureTrailingSlash(context.baseUrl),
      tenantId: context.tenant, // URI templates expect tenantId
      pciPod: context.basePciUrl && ensureTrailingSlash(context.basePciUrl)
    }, context, body || {});

  if (ctx.tenantPod) ctx.tenantPod = ensureTrailingSlash(ctx.tenantPod);

  if (!body || !body.hasOwnProperty("version")) delete ctx.version; // don't pass the API version!

  // ensure the correct base url is present
  var baseVar = template.expressions[0];
  if (baseVar.operator && baseVar.operator.symbol === '+' && baseVar.varspecs && !ctx[baseVar.varspecs[0].varname]) {
    throw new Error('Could not make URL from template ' + tpt + '. Your context is missing a ' + baseVar.varspecs[0].varname + '.');
  } 

  return {
    url: template.expand(ctx),
    keysUsed: templateCache[tpt].keysUsed
  };
};

'use strict';
var uritemplate = require('uritemplate'),
extend = require('./tiny-extend');

var templateCache = {};

function toKeysUsed(memo, expr) {
  if (expr.templateText) memo[expr.templateText] = true;
  return memo;
}

function ensureTrailingSlash(url) {
  return (url.charAt(url.length-1) === '/') ? url : (url + '/');
}

/**
 * Creates, evaluates based on context, and returns a string URL for a Mozu API request.
 * @param  {Object} context The context of a client. Should have a `baseUrl` property at minimum.
 * @param  {string} tpt     A string to be compiled into a UriTemplate. Should be a valid UriTemplate.
 * @param  {Object} body      An object consisting of the JSON body of the request, to be used to interpolate URL paramters.
 * @return {string}         A fully qualified URL.
 */
module.exports = function makeUrl(client, tpt, body) {
  var context = client.context,
    template = templateCache[tpt] && templateCache[tpt].template;
    if (!template) {
      template = uritemplate.parse(tpt);
      templateCache[tpt] = {
        template: template,
        keysUsed: template.expressions.reduce(toKeysUsed, {})
      };
    }
    var ctx = extend({
      homePod: context.baseUrl && ensureTrailingSlash(context.baseUrl),
      tenantId: context.tenant || context.tenantId, // URI templates expect tenantId
      pciPod: context.basePciUrl && ensureTrailingSlash(context.basePciUrl)
    }, context, body || {});

  if (ctx.tenantPod) ctx.tenantPod = ensureTrailingSlash(ctx.tenantPod);

  if (!body || !body.hasOwnProperty("version")) delete ctx.version; // don't pass the API version!

  // ensure the correct base url is present
  var baseVar = template.expressions[0];
  if (baseVar.operator && baseVar.operator.symbol === '+' && baseVar.varspecs && !ctx[baseVar.varspecs[0].varname]) {
    throw new Error('Could not make URL from template ' + tpt + '. Your context is missing a ' + baseVar.varspecs[0].varname + '.');
  } 

  return {
    url: template.expand(ctx),
    keysUsed: templateCache[tpt].keysUsed
  };
};

fix bug with tenant/tenantId normalization