import Handlebars from 'handlebars/dist/cjs/handlebars'

import { formatNumber, formatPercent, formatDate, formatMoney } from 'lib/i18n';


Handlebars.registerHelper("formatNumber", (n, ctx) => formatNumber(n, ctx));

Handlebars.registerHelper("formatPercent", (n, ctx) => formatPercent(n, ctx));

Handlebars.registerHelper("formatMoney", (n, currency, ctx) => formatMoney(n, currency, ctx));

Handlebars.registerHelper("formatDate", formatDate);

Handlebars.registerHelper("formatLabel", function(key, labels) {
    let label = labels && labels[key];
    if (label && arguments.length > 3) {
        label = replaceParams(label, Array.prototype.slice.call(arguments, 2));
    }
    return label || key;
});

/*
Handlebars.registerHelper('switch', function(value, options) {
  this.switch_value = value;
  return options.fn(this);
});

Handlebars.registerHelper('case', function(value, options) {
  if (value == this.switch_value) {
    return options.fn(this);
  }
});
*/

Handlebars.registerHelper("eq", function (a, b, options) {
    if (a == b) { return options.fn(this); }
    return options.inverse(this);
});

Handlebars.registerHelper("neq", function (a, b, options) {
    if (a != b) { return options.fn(this); }
    return options.inverse(this);
});

export default Handlebars;


function replaceParams(string, replacements) {
    return string.replace(/\{(\d+)\}/g, function() {
        return replacements[arguments[1]];
    });
}
