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