import J from "./exponent.js";
import K from "./formatGroup.js";
import O from "./formatNumerals.js";
import z from "./formatSpecifier.js";
import Q from "./formatTrim.js";
import A from "./formatTypes.js";
import { prefixExponent as C } from "./formatPrefixAuto.js";
import E from "./identity.js";
var G = Array.prototype.map, I = ["y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y"];
function rn(r) {
  var w = r.grouping === void 0 || r.thousands === void 0 ? E : K(G.call(r.grouping, Number), r.thousands + ""), L = r.currency === void 0 ? "" : r.currency[0] + "", $ = r.currency === void 0 ? "" : r.currency[1] + "", j = r.decimal === void 0 ? "." : r.decimal + "", F = r.numerals === void 0 ? E : O(G.call(r.numerals, String)), X = r.percent === void 0 ? "%" : r.percent + "", Y = r.minus === void 0 ? "−" : r.minus + "", Z = r.nan === void 0 ? "NaN" : r.nan + "";
  function k(t, e) {
    t = z(t);
    var u = t.fill, h = t.align, m = t.sign, x = t.symbol, g = t.zero, b = t.width, M = t.comma, o = t.precision, S = t.trim, f = t.type;
    f === "n" ? (M = !0, f = "g") : A[f] || (o === void 0 && (o = 12), S = !0, f = "g"), (g || u === "0" && h === "=") && (g = !0, u = "0", h = "=");
    var B = (e && e.prefix !== void 0 ? e.prefix : "") + (x === "$" ? L : x === "#" && /[boxX]/.test(f) ? "0" + f.toLowerCase() : ""), D = (x === "$" ? $ : /[%p]/.test(f) ? X : "") + (e && e.suffix !== void 0 ? e.suffix : ""), N = A[f], H = /[defgprs%]/.test(f);
    o = o === void 0 ? 6 : /[gprs]/.test(f) ? Math.max(1, Math.min(21, o)) : Math.max(0, Math.min(20, o));
    function P(n) {
      var s = B, i = D, a, T, p;
      if (f === "c")
        i = N(n) + i, n = "";
      else {
        n = +n;
        var y = n < 0 || 1 / n < 0;
        if (n = isNaN(n) ? Z : N(Math.abs(n), o), S && (n = Q(n)), y && +n == 0 && m !== "+" && (y = !1), s = (y ? m === "(" ? m : Y : m === "-" || m === "(" ? "" : m) + s, i = (f === "s" && !isNaN(n) && C !== void 0 ? I[8 + C / 3] : "") + i + (y && m === "(" ? ")" : ""), H) {
          for (a = -1, T = n.length; ++a < T; )
            if (p = n.charCodeAt(a), 48 > p || p > 57) {
              i = (p === 46 ? j + n.slice(a + 1) : n.slice(a)) + i, n = n.slice(0, a);
              break;
            }
        }
      }
      M && !g && (n = w(n, 1 / 0));
      var c = s.length + n.length + i.length, d = c < b ? new Array(b - c + 1).join(u) : "";
      switch (M && g && (n = w(d + n, d.length ? b - i.length : 1 / 0), d = ""), h) {
        case "<":
          n = s + n + i + d;
          break;
        case "=":
          n = s + d + n + i;
          break;
        case "^":
          n = d.slice(0, c = d.length >> 1) + s + n + i + d.slice(c);
          break;
        default:
          n = d + s + n + i;
          break;
      }
      return F(n);
    }
    return P.toString = function() {
      return t + "";
    }, P;
  }
  function q(t, e) {
    var u = Math.max(-8, Math.min(8, Math.floor(J(e) / 3))) * 3, h = Math.pow(10, -u), m = k((t = z(t), t.type = "f", t), { suffix: I[8 + u / 3] });
    return function(x) {
      return m(h * x);
    };
  }
  return {
    format: k,
    formatPrefix: q
  };
}
export {
  rn as default
};
