import bisect_default from "../../d3-array/src/bisect.js";
import number_default from "../../d3-interpolate/src/number.js";
import value_default from "../../d3-interpolate/src/value.js";
import round_default from "../../d3-interpolate/src/round.js";
import constants from "./constant.js";
import number from "./number.js";
var unit = [0, 1];
function identity(e) {
	return e;
}
function normalize(e, i) {
	return (i -= e = +e) ? function(a) {
		return (a - e) / i;
	} : constants(isNaN(i) ? NaN : .5);
}
function clamper(e, i) {
	var a;
	return e > i && (a = e, e = i, i = a), function(a) {
		return Math.max(e, Math.min(i, a));
	};
}
function bimap(e, i, a) {
	var o = e[0], s = e[1], c = i[0], l = i[1];
	return s < o ? (o = normalize(s, o), c = a(l, c)) : (o = normalize(o, s), c = a(c, l)), function(e) {
		return c(o(e));
	};
}
function polymap(i, a, o) {
	var s = Math.min(i.length, a.length) - 1, c = Array(s), l = Array(s), u = -1;
	for (i[s] < i[0] && (i = i.slice().reverse(), a = a.slice().reverse()); ++u < s;) c[u] = normalize(i[u], i[u + 1]), l[u] = o(a[u], a[u + 1]);
	return function(a) {
		var o = bisect_default(i, a, 1, s) - 1;
		return l[o](c[o](a));
	};
}
function copy(e, i) {
	return i.domain(e.domain()).range(e.range()).interpolate(e.interpolate()).clamp(e.clamp()).unknown(e.unknown());
}
function transformer() {
	var e = unit, s = unit, d = value_default, f, p, m, h = identity, g, _, v;
	function y() {
		var i = Math.min(e.length, s.length);
		return h !== identity && (h = clamper(e[0], e[i - 1])), g = i > 2 ? polymap : bimap, _ = v = null, b;
	}
	function b(i) {
		return i == null || isNaN(i = +i) ? m : (_ ||= g(e.map(f), s, d))(f(h(i)));
	}
	return b.invert = function(a) {
		return h(p((v ||= g(s, e.map(f), number_default))(a)));
	}, b.domain = function(i) {
		return arguments.length ? (e = Array.from(i, number), y()) : e.slice();
	}, b.range = function(e) {
		return arguments.length ? (s = Array.from(e), y()) : s.slice();
	}, b.rangeRound = function(e) {
		return s = Array.from(e), d = round_default, y();
	}, b.clamp = function(e) {
		return arguments.length ? (h = e ? !0 : identity, y()) : h !== identity;
	}, b.interpolate = function(e) {
		return arguments.length ? (d = e, y()) : d;
	}, b.unknown = function(e) {
		return arguments.length ? (m = e, b) : m;
	}, function(e, i) {
		return f = e, p = i, y();
	};
}
function continuous() {
	return transformer()(identity, identity);
}
export { copy, continuous as default };
