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;
    }
}
import { rollup } from "d3-array";
import { merge } from "@d3plus/data";
import { elem } from "@d3plus/dom";
import { configPrep } from "../../utils/index.js";
var legendAttrs = [
    "fill",
    "opacity",
    "texture"
];
/**
    Default label function for the legend.
    @private
*/ export function legendLabel(d, i) {
    return this._drawLabel(d, i, this._legendDepth);
}
/**
    Renders the legend if this._legend is not falsy.
    @private
*/ export default function() {
    var _this, _loop = function(x) {
        var values = labels.map(function(l) {
            return l[x];
        });
        if (!values.some(function(v) {
            return _instanceof(v, Array);
        }) && Array.from(new Set(values)).length === legendData.length) {
            _this._legendDepth = x;
            return "break";
        }
    };
    var _this1 = this;
    var data = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
    var legendData = [];
    var getAttr = function getAttr(d, i, attr) {
        var shape = _this1._shape(d, i);
        if (attr === "fill" && shape === "Line") attr = "stroke";
        var value = _this1._shapeConfig[shape] && _this1._shapeConfig[shape][attr] ? _this1._shapeConfig[shape][attr] : _this1._shapeConfig[attr];
        return typeof value === "function" ? value.bind(_this1)(d, i) : value;
    };
    var fill = function fill(d, i) {
        return legendAttrs.map(function(a) {
            return getAttr(d, i, a);
        }).join("_");
    };
    var rollupData = this._colorScale ? data.filter(function(d, i) {
        return _this1._colorScale(d, i) === undefined;
    }) : data;
    rollup(rollupData, function(leaves) {
        return legendData.push(merge(leaves, _this1._aggs));
    }, fill);
    legendData.sort(this._legendSort);
    var labels = legendData.map(function(d, i) {
        return _this1._ids(d, i).slice(0, _this1._drawDepth + 1);
    });
    this._legendDepth = 0;
    for(var x = 0; x <= this._drawDepth; x++){
        var _ret = (_this = this, _loop(x));
        if (_ret === "break") break;
    }
    var hidden = function hidden(d, i) {
        var id = _this1._id(d, i);
        if (_instanceof(id, Array)) id = id[0];
        return _this1._hidden.includes(id) || _this1._solo.length && !_this1._solo.includes(id);
    };
    var legendBounds = this._legendClass.outerBounds();
    var config = this.config();
    var position = this._legendPosition.bind(this)(config);
    if (![
        false,
        "top",
        "bottom",
        "left",
        "right"
    ].includes(position)) position = "bottom";
    var wide = [
        "top",
        "bottom"
    ].includes(position);
    var padding = this._legendPadding() ? this._padding : {
        top: 0,
        right: 0,
        bottom: 0,
        left: 0
    };
    var transform = {
        transform: "translate(".concat(wide ? this._margin.left + padding.left : this._margin.left, ", ").concat(wide ? this._margin.top : this._margin.top + padding.top, ")")
    };
    var visible = this._legend.bind(this)(config, legendData);
    var legendGroup = elem("g.d3plus-viz-legend", {
        condition: visible && !this._legendConfig.select,
        enter: transform,
        parent: this._select,
        duration: this._duration,
        update: transform
    }).node();
    this._legendClass.id(fill).align(wide ? "center" : position).direction(wide ? "row" : "column").duration(this._duration).data(visible ? legendData : []).height(wide ? this._height - (this._margin.bottom + this._margin.top) : this._height - (this._margin.bottom + this._margin.top + padding.bottom + padding.top)).locale(this._locale).parent(this).select(legendGroup).shape(function(d, i) {
        return _this1._shape(d, i) === "Circle" ? "Circle" : "Rect";
    }).verticalAlign(!wide ? "middle" : position).width(wide ? this._width - (this._margin.left + this._margin.right + padding.left + padding.right) : this._width - (this._margin.left + this._margin.right)).shapeConfig(configPrep.bind(this)(this._shapeConfig, "legend")).shapeConfig({
        fill: function fill(d, i) {
            return hidden(d, i) ? _this1._hiddenColor(d, i) : getAttr(d, i, "fill");
        },
        labelConfig: {
            fontOpacity: function fontOpacity(d, i) {
                return hidden(d, i) ? _this1._hiddenOpacity(d, i) : 1;
            }
        }
    }).config(this._legendConfig).render();
    if (!this._legendConfig.select && legendBounds.height) {
        if (wide) this._margin[position] += legendBounds.height + this._legendClass.padding() * 2;
        else this._margin[position] += legendBounds.width + this._legendClass.padding() * 2;
    }
}
