function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
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 _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
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 _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _type_of(obj) {
    "@swc/helpers - typeof";
    return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
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 { groups } from "d3-array";
import { select } from "d3-selection";
import { assign, elem } from "@d3plus/dom";
import { accessor, BaseClass, configPrep, constant } from "../utils/index.js";
import Circle from "./Circle.js";
import Line from "./Line.js";
import Rect from "./Rect.js";
var shapes = {
    Circle: Circle,
    Rect: Rect
};
var Whisker = /*#__PURE__*/ function(BaseClass) {
    "use strict";
    _inherits(Whisker, BaseClass);
    function Whisker() {
        _class_call_check(this, Whisker);
        var _this;
        _this = _call_super(this, Whisker), _define_property(_this, "_endpoint", void 0), _define_property(_this, "_endpointConfig", void 0), _define_property(_this, "_length", void 0), _define_property(_this, "_lineConfig", void 0), _define_property(_this, "_orient", void 0), _define_property(_this, "_x", void 0), _define_property(_this, "_y", void 0), _define_property(_this, "_data", void 0), _define_property(_this, "_select", void 0), _define_property(_this, "_line", void 0), _define_property(_this, "_whiskerEndpoint", void 0);
        _this._endpoint = accessor("endpoint", "Rect");
        _this._endpointConfig = {
            Circle: {
                r: accessor("r", 5)
            }
        };
        _this._length = accessor("length", 25);
        _this._lineConfig = {};
        _this._orient = accessor("orient", "top");
        _this._x = accessor("x", 0);
        _this._y = accessor("y", 0);
        _this._whiskerEndpoint = [];
        return _this;
    }
    _create_class(Whisker, [
        {
            /**
      Draws the whisker.
    @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").style("width", "".concat(window.innerWidth, "px")).style("height", "".concat(window.innerHeight, "px")).style("display", "block").node());
                }
                var lineData = [];
                this._data.forEach(function(d, i) {
                    var orient = _this._orient(d, i);
                    var x = _this._x(d, i);
                    var y = _this._y(d, i);
                    var endpointX = x;
                    if (orient === "left") endpointX -= _this._length(d, i);
                    else if (orient === "right") endpointX += _this._length(d, i);
                    var endpointY = y;
                    if (orient === "top") endpointY -= _this._length(d, i);
                    else if (orient === "bottom") endpointY += _this._length(d, i);
                    lineData.push({
                        __d3plus__: true,
                        data: d,
                        i: i,
                        id: i,
                        x: x,
                        y: y
                    });
                    lineData.push({
                        __d3plus__: true,
                        data: d,
                        i: i,
                        id: i,
                        x: endpointX,
                        y: endpointY
                    });
                });
                // Draw whisker line.
                this._line = new Line().data(lineData).select(elem("g.d3plus-Whisker", {
                    parent: this._select
                }).node())// eslint-disable-next-line @typescript-eslint/no-explicit-any
                .config(configPrep.bind(this)(this._lineConfig, "shape")).render(callback);
                var whiskerData = this._data.map(function(d, i) {
                    var dataObj = {};
                    dataObj.__d3plus__ = true;
                    dataObj.data = d;
                    dataObj.i = i;
                    dataObj.endpoint = _this._endpoint(d, i);
                    dataObj.length = _this._length(d, i);
                    dataObj.orient = _this._orient(d, i);
                    var endpointX = _this._x(d, i);
                    if (dataObj.orient === "left") endpointX -= dataObj.length;
                    else if (dataObj.orient === "right") endpointX += dataObj.length;
                    var endpointY = _this._y(d, i);
                    if (dataObj.orient === "top") endpointY -= dataObj.length;
                    else if (dataObj.orient === "bottom") endpointY += dataObj.length;
                    dataObj.x = endpointX;
                    dataObj.y = endpointY;
                    return dataObj;
                });
                // Draw whisker endpoint.
                this._whiskerEndpoint = [];
                groups(whiskerData, function(d) {
                    return d.endpoint;
                }).forEach(function(param) {
                    var _param = _sliced_to_array(param, 2), shapeName = _param[0], values = _param[1];
                    _this._whiskerEndpoint.push(new shapes[shapeName]().data(values).select(elem("g.d3plus-Whisker-Endpoint-".concat(shapeName), {
                        parent: _this._select
                    }).node()).config({
                        height: function height(d) {
                            return d.orient === "top" || d.orient === "bottom" ? 5 : 20;
                        },
                        width: function width(d) {
                            return d.orient === "top" || d.orient === "bottom" ? 20 : 5;
                        }
                    })// eslint-disable-next-line @typescript-eslint/no-explicit-any
                    .config(configPrep.bind(_this)(_this._endpointConfig, "shape", shapeName)).render());
                });
                return this;
            }
        },
        {
            /**
      The active highlight state for all sub-shapes in this Whisker.
*/ key: "active",
            value: function active(_) {
                if (this._line) this._line.active(_);
                if (this._whiskerEndpoint) this._whiskerEndpoint.forEach(function(endPoint) {
                    return endPoint.active(_);
                });
            }
        },
        {
            key: "data",
            value: function data(_) {
                return arguments.length ? (this._data = _, this) : this._data;
            }
        },
        {
            key: "endpoint",
            value: function endpoint(_) {
                return arguments.length ? (this._endpoint = typeof _ === "function" ? _ : constant(_), this) : this._endpoint;
            }
        },
        {
            key: "endpointConfig",
            value: function endpointConfig(_) {
                return arguments.length ? (this._endpointConfig = assign(this._endpointConfig, _), this) : this._endpointConfig;
            }
        },
        {
            /**
      The hover highlight state for all sub-shapes in this Whisker.
*/ key: "hover",
            value: function hover(_) {
                if (this._line) this._line.hover(_);
                if (this._whiskerEndpoint) this._whiskerEndpoint.forEach(function(endPoint) {
                    return endPoint.hover(_);
                });
            }
        },
        {
            key: "length",
            value: function length(_) {
                return arguments.length ? (this._length = typeof _ === "function" ? _ : constant(_), this) : this._length;
            }
        },
        {
            key: "lineConfig",
            value: function lineConfig(_) {
                return arguments.length ? (this._lineConfig = assign(this._lineConfig, _), this) : this._lineConfig;
            }
        },
        {
            key: "orient",
            value: function orient(_) {
                return arguments.length ? (this._orient = typeof _ === "function" ? _ : constant(_), this) : this._orient;
            }
        },
        {
            key: "select",
            value: function select1(_) {
                return arguments.length ? (this._select = select(_), this) : this._select;
            }
        },
        {
            key: "x",
            value: function x(_) {
                return arguments.length ? (this._x = typeof _ === "function" ? _ : constant(_), this) : this._x;
            }
        },
        {
            key: "y",
            value: function y(_) {
                return arguments.length ? (this._y = typeof _ === "function" ? _ : constant(_), this) : this._y;
            }
        }
    ]);
    return Whisker;
}(BaseClass);
/**
    Creates SVG whisker based on an array of data.
*/ export { Whisker as default };
