"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _d3Format = require("d3-format");

var _d3TimeFormat = require("d3-time-format");

var _lodash = require("lodash");

var scales = [[1000000000000, "trillion"], [1000000000, "billion"], [1000000, "million"]];

var abbreviateLarge = function abbreviateLarge(number) {
  var num;
  var scale;

  for (var i = 0; i <= scales.length; i += 1) {
    if (Math.abs(number) >= scales[i][0]) {
      num = (0, _d3Format.format)(".2~r")(number / scales[i][0]);
      scale = scales[i][1];
      break;
    }
  }

  return "".concat(num, " ").concat(scale);
};

var numeric = function numeric(d) {
  var formatted;

  if (Math.abs(d) >= 1000000) {
    formatted = abbreviateLarge(d);
  } else {
    formatted = (0, _d3Format.format)(",.0f")(d);
  }

  return formatted;
};

var roundedDecimal = function roundedDecimal(d) {
  return (0, _d3Format.format)(",.2f")(d);
};

var scalesShort = [[1000000000000, "t"], [1000000000, "b"], [1000000, "m"], [1000, "k"]];

var abbreviateLargeShort = function abbreviateLargeShort(number) {
  var num;
  var scale;

  for (var i = 0; i <= scalesShort.length; i += 1) {
    if (Math.abs(number) >= scalesShort[i][0]) {
      num = (0, _d3Format.format)(".2~r")(number / scalesShort[i][0]);
      scale = scalesShort[i][1];
      break;
    }
  }

  return "".concat(num).concat(scale);
};

var numericShort = function numericShort(d) {
  var formatted;

  if (Math.abs(d) >= 1000) {
    formatted = abbreviateLargeShort(d);
  } else {
    formatted = (0, _d3Format.format)(",.0f")(d);
  }

  return formatted;
};

var decimalToPercent = function decimalToPercent(num) {
  var formatted;

  if (Number.isInteger(num)) {
    formatted = (0, _d3Format.format)(",~%")(num);
  } else {
    formatted = (0, _d3Format.format)(",.1%")(num);
  }

  return formatted;
};

var year = (0, _d3Format.format)(".0f");
var percentage = (0, _d3Format.format)(".0%");

var dollars = function dollars(d) {
  return "$".concat(numeric(d));
};

var titleCase = function titleCase(str) {
  return (0, _lodash.startCase)((0, _lodash.toLower)(str));
};

var unformatted = function unformatted(d) {
  return d;
};

var monthYear = (0, _d3TimeFormat.timeFormat)("%B %Y");
var civicFormat = {
  numeric: numeric,
  roundedDecimal: roundedDecimal,
  year: year,
  percentage: percentage,
  dollars: dollars,
  titleCase: titleCase,
  unformatted: unformatted,
  monthYear: monthYear,
  numericShort: numericShort,
  decimalToPercent: decimalToPercent
};
var _default = civicFormat;
exports["default"] = _default;