function _assert_this_initialized(self) {
    if (self === void 0) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
    }
    return self;
}
function _call_super(_this, derived, args) {
    derived = _get_prototype_of(derived);
    return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
}
function _class_call_check(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}
function _defineProperties(target, props) {
    for(var i = 0; i < props.length; i++){
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
    }
}
function _create_class(Constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
    if (staticProps) _defineProperties(Constructor, staticProps);
    return Constructor;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _get_prototype_of(o) {
    _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
        return o.__proto__ || Object.getPrototypeOf(o);
    };
    return _get_prototype_of(o);
}
function _inherits(subClass, superClass) {
    if (typeof superClass !== "function" && superClass !== null) {
        throw new TypeError("Super expression must either be null or a function");
    }
    subClass.prototype = Object.create(superClass && superClass.prototype, {
        constructor: {
            value: subClass,
            writable: true,
            configurable: true
        }
    });
    if (superClass) _set_prototype_of(subClass, superClass);
}
function _instanceof(left, right) {
    "@swc/helpers - instanceof";
    if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
        return !!right[Symbol.hasInstance](left);
    } else {
        return left instanceof right;
    }
}
function _possible_constructor_return(self, call) {
    if (call && (_type_of(call) === "object" || typeof call === "function")) {
        return call;
    }
    return _assert_this_initialized(self);
}
function _set_prototype_of(o, p) {
    _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
        o.__proto__ = p;
        return o;
    };
    return _set_prototype_of(o, p);
}
function _type_of(obj) {
    "@swc/helpers - typeof";
    return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _is_native_reflect_construct() {
    try {
        var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
    } catch (_) {}
    return (_is_native_reflect_construct = function() {
        return !!result;
    })();
}
import { extent, max, min, quantile, range, deviation } from "d3-array";
import { interpolateRgb } from "d3-interpolate";
import { scaleLinear, scaleThreshold } from "d3-scale";
import { select } from "d3-selection";
import { transition } from "d3-transition";
import { colorContrast, colorDefaults, colorLighter } from "@d3plus/color";
import { unique } from "@d3plus/data";
import { assign, backgroundColor, elem, textWidth } from "@d3plus/dom";
import { formatAbbreviate } from "@d3plus/format";
import { ckmeans } from "@d3plus/math";
import { Axis, TextBox } from "../components/index.js";
import { Rect } from "../shapes/index.js";
import { accessor, BaseClass, constant } from "../utils/index.js";
import Legend from "./Legend.js";
var ColorScale = /*#__PURE__*/ function(BaseClass) {
    "use strict";
    _inherits(ColorScale, BaseClass);
    function ColorScale() {
        _class_call_check(this, ColorScale);
        var _this;
        _this = _call_super(this, ColorScale), _define_property(_this, "_select", void 0), _define_property(_this, "_axisClass", void 0), _define_property(_this, "_axisConfig", void 0), _define_property(_this, "_axisTest", void 0), _define_property(_this, "_align", void 0), _define_property(_this, "_buckets", void 0), _define_property(_this, "_bucketAxis", void 0), _define_property(_this, "_bucketFormat", void 0), _define_property(_this, "_bucketJoiner", void 0), _define_property(_this, "_centered", void 0), _define_property(_this, "_color", void 0), _define_property(_this, "_colorMax", void 0), _define_property(_this, "_colorMid", void 0), _define_property(_this, "_colorMin", void 0), // eslint-disable-next-line @typescript-eslint/no-explicit-any
        _define_property(_this, "_colorScale", void 0), _define_property(_this, "_data", void 0), _define_property(_this, "_domain", void 0), _define_property(_this, "_duration", void 0), _define_property(_this, "_group", void 0), _define_property(_this, "_height", void 0), _define_property(_this, "_labelClass", void 0), _define_property(_this, "_labelConfig", void 0), _define_property(_this, "_labelMin", void 0), _define_property(_this, "_labelMax", void 0), _define_property(_this, "_legendClass", void 0), _define_property(_this, "_legendConfig", void 0), _define_property(_this, "_midpoint", void 0), _define_property(_this, "_orient", void 0), _define_property(_this, "_outerBounds", void 0), _define_property(_this, "_padding", void 0), _define_property(_this, "_rectClass", void 0), _define_property(_this, "_rectConfig", void 0), _define_property(_this, "_scale", void 0), _define_property(_this, "_size", void 0), _define_property(_this, "_value", void 0), _define_property(_this, "_width", void 0);
        _this._axisClass = new Axis();
        _this._axisConfig = {
            gridSize: 0
        };
        _this._axisTest = new Axis();
        _this._align = "middle";
        _this._buckets = 5;
        _this._bucketAxis = false;
        _this._bucketFormat = function(tick, i, ticks, allValues) {
            var format = _this._axisConfig.tickFormat ? _this._axisConfig.tickFormat : formatAbbreviate;
            var next = ticks[i + 1];
            var prev = i ? ticks[i - 1] : false;
            var last = i === ticks.length - 1;
            if (tick === next || last) {
                var suffix = last && tick < max(allValues) ? "+" : "";
                return "".concat(format(tick)).concat(suffix);
            } else {
                var mod = next ? next / 100 : tick / 100;
                var pow = mod >= 1 || mod <= -1 ? Math.round(mod).toString().length - 1 : mod.toString().split(".")[1].replace(/([1-9])[1-9].*$/, "$1").length * -1;
                var ten = Math.pow(10, pow);
                var prevValue = prev === tick && i === 1 ? format(min([
                    tick + ten,
                    allValues.find(function(d) {
                        return d > tick && d < next;
                    })
                ])) : format(tick);
                var nextValue = tick && i === 1 ? format(next) : format(max([
                    next - ten,
                    allValues.reverse().find(function(d) {
                        return d > tick && d < next;
                    })
                ]));
                return _this._bucketJoiner(prevValue, nextValue);
            }
        };
        _this._bucketJoiner = function(a, b) {
            return a !== b ? "".concat(a, " - ").concat(b) : "".concat(a);
        };
        _this._centered = true;
        _this._color = [
            "#54478C",
            "#2C699A",
            "#0DB39E",
            "#83E377",
            "#EFEA5A"
        ];
        _this._colorMax = colorDefaults.on;
        _this._colorMid = colorDefaults.light;
        _this._colorMin = colorDefaults.off;
        _this._data = [];
        _this._duration = 600;
        _this._height = 200;
        _this._labelClass = new TextBox();
        _this._labelConfig = {
            fontColor: function fontColor() {
                var bg = _this._select ? backgroundColor(_this._select.node()) : "rgb(255, 255, 255)";
                return colorContrast(bg);
            },
            fontSize: 12
        };
        _this._legendClass = new Legend();
        _this._legendConfig = {
            shapeConfig: {
                stroke: colorDefaults.dark,
                strokeWidth: 1
            }
        };
        _this._midpoint = 0;
        _this._orient = "bottom";
        _this._outerBounds = {
            width: 0,
            height: 0,
            x: 0,
            y: 0
        };
        _this._padding = 5;
        _this._rectClass = new Rect().parent(_this);
        _this._rectConfig = {
            stroke: "#999",
            strokeWidth: 1
        };
        _this._scale = "linear";
        _this._size = 10;
        _this._value = accessor("value");
        _this._width = 400;
        return _this;
    }
    _create_class(ColorScale, [
        {
            /**
      Renders the current ColorScale to the page.
    @param callback Optional callback invoked after rendering completes.
*/ key: "render",
            value: function render(callback) {
                var _this = this;
                if (this._select === void 0) this.select(select("body").append("svg").attr("width", "".concat(this._width, "px")).attr("height", "".concat(this._height, "px")).node());
                var horizontal = [
                    "bottom",
                    "top"
                ].includes(this._orient);
                var height = horizontal ? "height" : "width", width = horizontal ? "width" : "height", x = horizontal ? "x" : "y", y = horizontal ? "y" : "x";
                // Shape <g> Group
                this._group = elem("g.d3plus-ColorScale", {
                    parent: this._select
                });
                var allValues = this._data.map(this._value).filter(function(d) {
                    return d !== null && typeof d === "number";
                }).sort(function(a, b) {
                    return a - b;
                });
                var domain = this._domain || extent(allValues);
                var negative = domain[0] < this._midpoint;
                var positive = domain[1] > this._midpoint;
                var diverging = negative && positive;
                var numBuckets = min([
                    _instanceof(this._buckets, Array) ? this._buckets.length : this._buckets,
                    diverging && this._scale !== "jenks" ? 2 * Math.floor(unique(allValues).length / 2) - 1 : unique(allValues).length
                ]);
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                var colors = diverging && (!this._color || _instanceof(this._color, Array) && !this._color.includes(this._colorMid)) ? undefined : this._color, labels, ticks;
                if (colors && !_instanceof(colors, Array)) {
                    colors = range(0, numBuckets, 1).map(function(i) {
                        return colorLighter(colors, (i + 1) / numBuckets);
                    }).reverse();
                }
                if (this._scale === "jenks") {
                    var buckets = min([
                        colors ? colors.length : numBuckets,
                        numBuckets,
                        allValues.length
                    ]);
                    var jenks = [];
                    if (_instanceof(this._buckets, Array)) {
                        ticks = this._buckets;
                    } else {
                        if (diverging && this._centered) {
                            var half = Math.floor(buckets / 2);
                            var residual = buckets % 2;
                            var negatives = allValues.filter(function(d) {
                                return d < _this._midpoint;
                            });
                            var negativesDeviation = deviation(negatives);
                            var positives = allValues.concat(this._midpoint).filter(function(d) {
                                return d >= _this._midpoint;
                            });
                            var positivesDeviation = deviation(positives);
                            var isNegativeMax = negativesDeviation > positivesDeviation ? 1 : 0;
                            var isPositiveMax = positivesDeviation > negativesDeviation ? 1 : 0;
                            var negativeJenks = ckmeans(negatives, min([
                                half + residual * isNegativeMax,
                                negatives.length
                            ]));
                            var positiveJenks = ckmeans(positives, min([
                                half + residual * isPositiveMax,
                                positives.length
                            ]));
                            jenks = negativeJenks.concat(positiveJenks);
                        } else {
                            jenks = ckmeans(allValues, buckets);
                        }
                        ticks = jenks.map(function(c) {
                            return c[0];
                        });
                    }
                    var tickSet = new Set(ticks);
                    if (ticks.length !== tickSet.size) labels = Array.from(tickSet);
                    if (!colors) {
                        if (diverging) {
                            colors = [
                                this._colorMin,
                                this._colorMid,
                                this._colorMax
                            ];
                            var negatives1 = ticks.slice(0, buckets).filter(function(d, i) {
                                return d < _this._midpoint && ticks[i + 1] <= _this._midpoint;
                            });
                            var spanning = ticks.slice(0, buckets).filter(function(d, i) {
                                return d <= _this._midpoint && ticks[i + 1] > _this._midpoint;
                            });
                            var positives1 = ticks.slice(0, buckets).filter(function(d) {
                                return d > _this._midpoint;
                            });
                            var negativeColors = negatives1.map(function(_d, i) {
                                return !i ? colors[0] : colorLighter(colors[0], i / negatives1.length);
                            });
                            var spanningColors = spanning.map(function() {
                                return colors[1];
                            });
                            var positiveColors = positives1.map(function(_d, i) {
                                return i === positives1.length - 1 ? colors[2] : colorLighter(colors[2], 1 - (i + 1) / positives1.length);
                            });
                            colors = negativeColors.concat(spanningColors).concat(positiveColors);
                        } else {
                            colors = range(0, numBuckets, 1).map(function(i) {
                                return colorLighter(_this._colorMax, i / numBuckets);
                            }).reverse();
                        }
                    }
                    if (ticks.length <= buckets) colors = colors.slice(-ticks.length);
                    colors = [
                        colors[0]
                    ].concat(colors);
                    this._colorScale = scaleThreshold().domain(ticks).range(colors);
                } else {
                    var buckets1 = _instanceof(this._buckets, Array) ? this._buckets : undefined;
                    if (diverging && !colors) {
                        var half1 = Math.floor(numBuckets / 2);
                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
                        var negativeColorScale = interpolateRgb.gamma(2.2)(this._colorMin, this._colorMid);
                        var negativeColors1 = range(0, half1, 1).map(function(i) {
                            return negativeColorScale(i / half1);
                        });
                        var spanningColors1 = (numBuckets % 2 ? [
                            0
                        ] : []).map(function() {
                            return _this._colorMid;
                        });
                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
                        var positiveColorScale = interpolateRgb.gamma(2.2)(this._colorMax, this._colorMid);
                        var positiveColors1 = range(0, half1, 1).map(function(i) {
                            return positiveColorScale(i / half1);
                        }).reverse();
                        colors = negativeColors1.concat(spanningColors1).concat(positiveColors1);
                        if (!buckets1) {
                            var step = (colors.length - 1) / 2;
                            buckets1 = [
                                domain[0],
                                this._midpoint,
                                domain[1]
                            ];
                            buckets1 = range(domain[0], this._midpoint, -(domain[0] - this._midpoint) / step).concat(range(this._midpoint, domain[1], (domain[1] - this._midpoint) / step)).concat([
                                domain[1]
                            ]);
                        }
                    } else {
                        if (!colors) {
                            if (this._scale === "buckets" || this._scale === "quantile") {
                                colors = range(0, numBuckets, 1).map(function(i) {
                                    return colorLighter(negative ? _this._colorMin : _this._colorMax, i / numBuckets);
                                });
                                if (positive) colors = colors.reverse();
                            } else {
                                colors = negative ? [
                                    this._colorMin,
                                    colorLighter(this._colorMin, 0.8)
                                ] : [
                                    colorLighter(this._colorMax, 0.8),
                                    this._colorMax
                                ];
                            }
                        }
                        if (!buckets1) {
                            if (this._scale === "quantile") {
                                var step1 = 1 / (colors.length - 1);
                                buckets1 = range(0, 1 + step1 / 2, step1).map(function(d) {
                                    return quantile(allValues, d);
                                });
                            } else if (diverging && this._color && this._centered) {
                                var midIndex = colors.indexOf(this._colorMid);
                                var negativeStep = (this._midpoint - domain[0]) / midIndex;
                                var positiveStep = (domain[1] - this._midpoint) / (colors.length - midIndex);
                                var negativeBuckets = range(domain[0], this._midpoint, negativeStep);
                                var positiveBuckets = range(this._midpoint, domain[1] + positiveStep / 2, positiveStep);
                                buckets1 = negativeBuckets.concat(positiveBuckets);
                            } else {
                                var step2 = (domain[1] - domain[0]) / (colors.length - 1);
                                buckets1 = range(domain[0], domain[1] + step2 / 2, step2);
                            }
                        }
                    }
                    if (this._scale === "buckets" || this._scale === "quantile") {
                        ticks = buckets1;
                        colors = [
                            colors[0]
                        ].concat(colors);
                    } else if (this._scale === "log") {
                        var negativeBuckets1 = buckets1.filter(function(d) {
                            return d < 0;
                        });
                        if (negativeBuckets1.length) {
                            var minVal = negativeBuckets1[0];
                            var newNegativeBuckets = negativeBuckets1.map(function(d) {
                                return -Math.pow(Math.abs(minVal), d / minVal);
                            });
                            negativeBuckets1.forEach(function(bucket, i) {
                                buckets1[buckets1.indexOf(bucket)] = newNegativeBuckets[i];
                            });
                        }
                        var positiveBuckets1 = buckets1.filter(function(d) {
                            return d > 0;
                        });
                        if (positiveBuckets1.length) {
                            var maxVal = positiveBuckets1[positiveBuckets1.length - 1];
                            var newPositiveBuckets = positiveBuckets1.map(function(d) {
                                return Math.pow(maxVal, d / maxVal);
                            });
                            positiveBuckets1.forEach(function(bucket, i) {
                                buckets1[buckets1.indexOf(bucket)] = newPositiveBuckets[i];
                            });
                        }
                        if (buckets1.includes(0)) buckets1[buckets1.indexOf(0)] = 1;
                    }
                    var scaleFn = this._scale === "buckets" || this._scale === "quantile" ? scaleThreshold : scaleLinear;
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    this._colorScale = scaleFn().domain(buckets1).range(colors);
                }
                if (this._colorScale.clamp) this._colorScale.clamp(true);
                var gradient = this._bucketAxis || ![
                    "buckets",
                    "jenks",
                    "quantile"
                ].includes(this._scale);
                var t = transition().duration(this._duration);
                var groupParams = {
                    enter: {
                        opacity: 0
                    },
                    exit: {
                        opacity: 0
                    },
                    parent: this._group,
                    transition: t,
                    update: {
                        opacity: 1
                    }
                };
                var labelGroup = elem("g.d3plus-ColorScale-labels", Object.assign({
                    condition: gradient
                }, groupParams));
                var rectGroup = elem("g.d3plus-ColorScale-Rect", Object.assign({
                    condition: gradient
                }, groupParams));
                var legendGroup = elem("g.d3plus-ColorScale-legend", Object.assign({
                    condition: !gradient
                }, groupParams));
                if (gradient) {
                    var offsets = {
                        x: 0,
                        y: 0
                    };
                    var axisDomain = domain.slice();
                    if (this._bucketAxis) {
                        var last = axisDomain[axisDomain.length - 1];
                        var prev = axisDomain[axisDomain.length - 2];
                        var mod = last ? last / 10 : prev / 10;
                        var pow = mod >= 1 || mod <= -1 ? Math.round(mod).toString().length - 1 : mod.toString().split(".")[1].replace(/([1-9])[1-9].*$/, "$1").length * -1;
                        var ten = Math.pow(10, pow);
                        axisDomain[axisDomain.length - 1] = last + ten;
                    }
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    var axisConfig = assign({
                        domain: horizontal ? axisDomain : axisDomain.slice().reverse(),
                        duration: this._duration,
                        height: this._height,
                        labels: labels || ticks,
                        orient: this._orient,
                        padding: this._padding,
                        scale: this._scale === "log" ? "log" : "linear",
                        ticks: ticks,
                        width: this._width
                    }, this._axisConfig);
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    var labelConfig = assign({
                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
                        height: this["_".concat(height)] / 2,
                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
                        width: this["_".concat(width)] / 2
                    }, this._labelConfig);
                    this._labelClass.config(labelConfig);
                    var labelData = [];
                    if (horizontal && this._labelMin) {
                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
                        var labelCSS = {
                            // eslint-disable-next-line @typescript-eslint/no-explicit-any
                            "font-family": this._labelClass.fontFamily()(this._labelMin),
                            // eslint-disable-next-line @typescript-eslint/no-explicit-any
                            "font-size": this._labelClass.fontSize()(this._labelMin),
                            // eslint-disable-next-line @typescript-eslint/no-explicit-any
                            "font-weight": this._labelClass.fontWeight()(this._labelMin)
                        };
                        if (_instanceof(labelCSS["font-family"], Array)) labelCSS["font-family"] = labelCSS["font-family"][0];
                        var labelMinWidth = textWidth(this._labelMin, labelCSS);
                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
                        if (labelMinWidth && labelMinWidth < this["_".concat(width)] / 2) {
                            labelData.push(this._labelMin);
                            labelMinWidth += this._padding;
                            if (horizontal) offsets.x += labelMinWidth;
                            axisConfig[width] -= labelMinWidth;
                        }
                    }
                    if (horizontal && this._labelMax) {
                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
                        var labelCSS1 = {
                            // eslint-disable-next-line @typescript-eslint/no-explicit-any
                            "font-family": this._labelClass.fontFamily()(this._labelMax),
                            // eslint-disable-next-line @typescript-eslint/no-explicit-any
                            "font-size": this._labelClass.fontSize()(this._labelMax),
                            // eslint-disable-next-line @typescript-eslint/no-explicit-any
                            "font-weight": this._labelClass.fontWeight()(this._labelMax)
                        };
                        if (_instanceof(labelCSS1["font-family"], Array)) labelCSS1["font-family"] = labelCSS1["font-family"][0];
                        var labelMaxWidth = textWidth(this._labelMax, labelCSS1);
                        // eslint-disable-next-line @typescript-eslint/no-explicit-any
                        if (labelMaxWidth && labelMaxWidth < this["_".concat(width)] / 2) {
                            labelData.push(this._labelMax);
                            labelMaxWidth += this._padding;
                            if (!horizontal) offsets.y += labelMaxWidth;
                            axisConfig[width] -= labelMaxWidth;
                        }
                    }
                    this._axisTest.select(elem("g.d3plus-ColorScale-axisTest", {
                        enter: {
                            opacity: 0
                        },
                        parent: this._group
                    }).node()).config(axisConfig).duration(0).render();
                    var axisBounds = this._axisTest.outerBounds();
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    this._outerBounds[width] = this["_".concat(width)] - this._padding * 2;
                    this._outerBounds[height] = axisBounds[height] + this._size;
                    this._outerBounds[x] = this._padding;
                    this._outerBounds[y] = this._padding;
                    if (this._align === "middle") this._outerBounds[y] = // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    (this["_".concat(height)] - this._outerBounds[height]) / 2;
                    else if (this._align === "end") this._outerBounds[y] = // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    this["_".concat(height)] - this._padding - this._outerBounds[height];
                    var axisGroupOffset = this._outerBounds[y] + ([
                        "bottom",
                        "right"
                    ].includes(this._orient) ? this._size : 0) - (axisConfig.padding || this._axisClass.padding());
                    var transform = "translate(".concat(offsets.x + (horizontal ? 0 : axisGroupOffset), ", ").concat(offsets.y + (horizontal ? axisGroupOffset : 0), ")");
                    this._axisClass.select(elem("g.d3plus-ColorScale-axis", assign(groupParams, {
                        condition: true,
                        enter: {
                            transform: transform
                        },
                        update: {
                            transform: transform
                        }
                    })).node()).config(axisConfig).align("start").render();
                    var axisScale = this._axisTest._getPosition.bind(this._axisTest);
                    var scaleRange = this._axisTest._getRange();
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    var defs = this._group.selectAll("defs").data([
                        0
                    ]);
                    var defsEnter = defs.enter().append("defs");
                    defsEnter.append("linearGradient").attr("id", "gradient-".concat(this._uuid));
                    defs = defsEnter.merge(defs);
                    defs.select("linearGradient").attr("".concat(x, "1"), horizontal ? "0%" : "100%").attr("".concat(x, "2"), horizontal ? "100%" : "0%").attr("".concat(y, "1"), "0%").attr("".concat(y, "2"), "0%");
                    var stops = defs.select("linearGradient").selectAll("stop").data(colors);
                    var scaleDomain = this._colorScale.domain();
                    var offsetScale = scaleLinear().domain(scaleRange).range(horizontal ? [
                        0,
                        100
                    ] : [
                        100,
                        0
                    ]);
                    stops.enter().append("stop").merge(stops).attr("offset", function(_d, i) {
                        return "".concat(i <= scaleDomain.length - 1 ? offsetScale(axisScale(scaleDomain[i])) : 100, "%");
                    }).attr("stop-color", String);
                    /** determines the width of buckets*/ var bucketWidth = function bucketWidth(d, i) {
                        var next = ticks[i + 1] || axisDomain[axisDomain.length - 1];
                        return Math.abs(axisScale(next) - axisScale(d));
                    };
                    var _obj;
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    var rectConfig = assign((_obj = {
                        duration: this._duration,
                        fill: ticks ? function(d) {
                            return _this._colorScale(d);
                        } : "url(#gradient-".concat(this._uuid, ")")
                    }, _define_property(_obj, x, ticks ? function(d, i) {
                        return axisScale(d) + bucketWidth(d, i) / 2 - ([
                            "left",
                            "right"
                        ].includes(_this._orient) ? bucketWidth(d, i) : 0);
                    } : scaleRange[0] + (scaleRange[1] - scaleRange[0]) / 2 + offsets[x]), _define_property(_obj, y, this._outerBounds[y] + ([
                        "top",
                        "left"
                    ].includes(this._orient) ? axisBounds[height] : 0) + this._size / 2 + offsets[y]), _define_property(_obj, width, ticks ? bucketWidth : scaleRange[1] - scaleRange[0]), _define_property(_obj, height, this._size), _obj), this._rectConfig);
                    this._rectClass// eslint-disable-next-line @typescript-eslint/no-explicit-any
                    .data(ticks || [
                        0
                    ])// eslint-disable-next-line @typescript-eslint/no-explicit-any
                    .id(function(_d, i) {
                        return i;
                    }).select(rectGroup.node()).config(rectConfig).render();
                    labelConfig.height = this._outerBounds[height];
                    labelConfig.width = this._outerBounds[width];
                    this._labelClass.config(labelConfig)// eslint-disable-next-line @typescript-eslint/no-explicit-any
                    .data(labelData).select(labelGroup.node()).x(function(d) {
                        return d === _this._labelMax ? rectConfig.x + rectConfig.width / 2 + _this._padding : _this._outerBounds.x;
                    }).y(function(d) {
                        return rectConfig.y - _this._labelClass.fontSize()(d) / 2;
                    }).text(function(d) {
                        return d;
                    }).rotate(horizontal ? 0 : this._orient === "right" ? 90 : -90).render();
                } else {
                    elem("g.d3plus-ColorScale-axis", Object.assign({
                        condition: gradient
                    }, groupParams));
                    var legendData = ticks.reduce(function(arr, tick, i) {
                        var label = _this._bucketFormat.bind(_this)(tick, i, ticks, allValues);
                        arr.push({
                            color: colors[i + 1],
                            id: label
                        });
                        return arr;
                    }, []);
                    if (!horizontal) legendData = legendData.reverse();
                    var legendConfig = assign({
                        align: horizontal ? "center" : ({
                            start: "left",
                            middle: "center",
                            end: "right"
                        })[this._align],
                        direction: horizontal ? "row" : "column",
                        duration: this._duration,
                        height: this._height,
                        padding: this._padding,
                        shapeConfig: assign({
                            duration: this._duration
                        }, this._axisConfig.shapeConfig || {}),
                        title: this._axisConfig.title,
                        titleConfig: this._axisConfig.titleConfig || {},
                        width: this._width,
                        verticalAlign: horizontal ? ({
                            start: "top",
                            middle: "middle",
                            end: "bottom"
                        })[this._align] : "middle"
                    }, this._legendConfig);
                    this._legendClass.data(legendData).select(legendGroup.node()).config(legendConfig).render();
                    this._outerBounds = this._legendClass.outerBounds();
                }
                if (callback) setTimeout(callback, this._duration + 100);
                return this;
            }
        },
        {
            key: "axisConfig",
            value: function axisConfig(_) {
                return arguments.length ? (this._axisConfig = assign(this._axisConfig, _), this) : this._axisConfig;
            }
        },
        {
            key: "align",
            value: function align(_) {
                return arguments.length ? (this._align = _, this) : this._align;
            }
        },
        {
            key: "buckets",
            value: function buckets(_) {
                return arguments.length ? (this._buckets = _, this) : this._buckets;
            }
        },
        {
            key: "bucketAxis",
            value: function bucketAxis(_) {
                return arguments.length ? (this._bucketAxis = _, this) : this._bucketAxis;
            }
        },
        {
            key: "bucketFormat",
            value: function bucketFormat(_) {
                return arguments.length ? (this._bucketFormat = _, this) : this._bucketFormat;
            }
        },
        {
            key: "bucketJoiner",
            value: function bucketJoiner(_) {
                return arguments.length ? (this._bucketJoiner = _, this) : this._bucketJoiner;
            }
        },
        {
            key: "centered",
            value: function centered(_) {
                return arguments.length ? (this._centered = _, this) : this._centered;
            }
        },
        {
            key: "color",
            value: function color(_) {
                return arguments.length ? (this._color = _, this) : this._color;
            }
        },
        {
            key: "colorMax",
            value: function colorMax(_) {
                return arguments.length ? (this._colorMax = _, this) : this._colorMax;
            }
        },
        {
            key: "colorMid",
            value: function colorMid(_) {
                return arguments.length ? (this._colorMid = _, this) : this._colorMid;
            }
        },
        {
            key: "colorMin",
            value: function colorMin(_) {
                return arguments.length ? (this._colorMin = _, this) : this._colorMin;
            }
        },
        {
            key: "data",
            value: function data(_) {
                return arguments.length ? (this._data = _, this) : this._data;
            }
        },
        {
            key: "domain",
            value: function domain(_) {
                return arguments.length ? (this._domain = _, this) : this._domain;
            }
        },
        {
            key: "duration",
            value: function duration(_) {
                return arguments.length ? (this._duration = _, this) : this._duration;
            }
        },
        {
            key: "height",
            value: function height(_) {
                return arguments.length ? (this._height = _, this) : this._height;
            }
        },
        {
            key: "labelConfig",
            value: function labelConfig(_) {
                return arguments.length ? (this._labelConfig = assign(this._labelConfig, _), this) : this._labelConfig;
            }
        },
        {
            key: "labelMin",
            value: function labelMin(_) {
                return arguments.length ? (this._labelMin = _, this) : this._labelMin;
            }
        },
        {
            key: "labelMax",
            value: function labelMax(_) {
                return arguments.length ? (this._labelMax = _, this) : this._labelMax;
            }
        },
        {
            key: "legendConfig",
            value: function legendConfig(_) {
                return arguments.length ? (this._legendConfig = assign(this._legendConfig, _), this) : this._legendConfig;
            }
        },
        {
            key: "midpoint",
            value: function midpoint(_) {
                return arguments.length ? (this._midpoint = _, this) : this._midpoint;
            }
        },
        {
            key: "orient",
            value: function orient(_) {
                return arguments.length ? (this._orient = _, this) : this._orient;
            }
        },
        {
            /**
      Returns the outer bounds of the ColorScale content. Must be called after rendering.
      @example
{"width": 180, "height": 24, "x": 10, "y": 20}
*/ key: "outerBounds",
            value: function outerBounds() {
                return this._outerBounds;
            }
        },
        {
            key: "padding",
            value: function padding(_) {
                return arguments.length ? (this._padding = _, this) : this._padding;
            }
        },
        {
            key: "rectConfig",
            value: function rectConfig(_) {
                return arguments.length ? (this._rectConfig = assign(this._rectConfig, _), this) : this._rectConfig;
            }
        },
        {
            key: "scale",
            value: function scale(_) {
                return arguments.length ? (this._scale = _, this) : this._scale;
            }
        },
        {
            key: "select",
            value: function select1(_) {
                return arguments.length ? (this._select = select(_), this) : this._select;
            }
        },
        {
            key: "size",
            value: function size(_) {
                return arguments.length ? (this._size = _, this) : this._size;
            }
        },
        {
            key: "value",
            value: function value(_) {
                return arguments.length ? (this._value = typeof _ === "function" ? _ : constant(_), this) : this._value;
            }
        },
        {
            key: "width",
            value: function width(_) {
                return arguments.length ? (this._width = _, this) : this._width;
            }
        }
    ]);
    return ColorScale;
}(BaseClass);
/**
    Creates an SVG color scale based on an array of data.
*/ export { ColorScale as default };
