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 _get(target, property, receiver) {
    if (typeof Reflect !== "undefined" && Reflect.get) {
        _get = Reflect.get;
    } else {
        _get = function get(target, property, receiver) {
            var base = _super_prop_base(target, property);
            if (!base) return;
            var desc = Object.getOwnPropertyDescriptor(base, property);
            if (desc.get) {
                return desc.get.call(receiver || target);
            }
            return desc.value;
        };
    }
    return _get(target, property, receiver || target);
}
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 _super_prop_base(object, property) {
    while(!Object.prototype.hasOwnProperty.call(object, property)){
        object = _get_prototype_of(object);
        if (object === null) break;
    }
    return object;
}
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 { extent, groups, sum } from "d3-array";
import { interpolatePath } from "d3-interpolate-path";
import { select } from "d3-selection";
import * as paths from "d3-shape";
import { merge } from "@d3plus/data";
import { constant } from "../utils/index.js";
import Shape from "./Shape.js";
var Line = /*#__PURE__*/ function(Shape) {
    "use strict";
    _inherits(Line, Shape);
    function Line() {
        _class_call_check(this, Line);
        var _this;
        _this = _call_super(this, Line);
        _this._curve = constant("linear");
        _this._defined = function(d) {
            return d;
        };
        _this._fill = constant("none");
        _this._hitArea = constant({
            d: function d(d) {
                return _this._path(d.values);
            },
            fill: "none",
            "stroke-width": 10,
            transform: null
        });
        _this._name = "Line";
        _this._path = paths.line();
        _this._stroke = constant("black");
        _this._strokeWidth = constant(1);
        return _this;
    }
    _create_class(Line, [
        {
            /**
      Filters/manipulates the data array before binding each point to an SVG group.
      @param data @private
*/ key: "_dataFilter",
            value: function _dataFilter(data) {
                var _this = this;
                var lines = groups(data, this._id).map(function(param) {
                    var _param = _sliced_to_array(param, 2), key = _param[0], values = _param[1];
                    var d = {
                        key: key,
                        values: values
                    };
                    d.data = merge(d.values);
                    d.i = data.indexOf(d.values[0]);
                    var x = extent(d.values, _this._x);
                    d.xR = x;
                    d.width = x[1] - x[0];
                    d.x = x[0] + d.width / 2;
                    var y = extent(d.values, _this._y);
                    d.yR = y;
                    d.height = y[1] - y[0];
                    d.y = y[0] + d.height / 2;
                    d.nested = true;
                    d.translate = [
                        d.x,
                        d.y
                    ];
                    d.__d3plusShape__ = true;
                    return d;
                });
                lines.key = function(d) {
                    return d.key;
                };
                return lines;
            }
        },
        {
            /**
      Draws the lines.
    @param callback Optional callback invoked after rendering completes.
*/ key: "render",
            value: function render(callback) {
                var _this = this;
                _get(_get_prototype_of(Line.prototype), "render", this).call(this, callback);
                var that = this;
                /**
        Calculates the stroke-dasharray used for animations
        @param d data point
        @private
*/ function calculateStrokeDashArray(d) {
                    d.initialLength = this.getTotalLength();
                    var strokeArray = that._strokeDasharray(d.values[0], that._data.indexOf(d.values[0])).split(" ").map(Number);
                    if (strokeArray.length === 1 && strokeArray[0] === 0) strokeArray = [
                        d.initialLength
                    ];
                    else if (strokeArray.length === 1) strokeArray.push(strokeArray[0]);
                    else if (strokeArray.length % 2) strokeArray = strokeArray.concat(strokeArray);
                    var newStrokeArray = [];
                    var strokeLength = 0;
                    while(strokeLength < d.initialLength){
                        for(var i = 0; i < strokeArray.length; i++){
                            var num = strokeArray[i];
                            strokeLength += num;
                            newStrokeArray.push(num);
                            if (strokeLength >= d.initialLength) break;
                        }
                    }
                    if (newStrokeArray.length > 1 && newStrokeArray.length % 2) newStrokeArray.pop();
                    newStrokeArray[newStrokeArray.length - 1] += d.initialLength - sum(newStrokeArray);
                    if (newStrokeArray.length % 2 === 0) newStrokeArray.push(0);
                    d.initialStrokeArray = newStrokeArray.join(" ");
                }
                var userCurve = this._curve.bind(this)(this.config());
                var curve = paths["curve".concat(userCurve.charAt(0).toUpperCase()).concat(userCurve.slice(1))];
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                this._path.curve(curve).defined(this._defined).x(this._x).y(this._y);
                var enter = this._enter.append("path").attr("transform", function(d) {
                    return "translate(".concat(-d.xR[0] - d.width / 2, ", ").concat(-d.yR[0] - d.height / 2, ")");
                }).attr("d", function(d) {
                    return _this._path(d.values);
                }).call(this._applyStyle.bind(this));
                var update = this._update.select("path").attr("stroke-dasharray", function(d) {
                    return that._strokeDasharray(d.values[0], that._data.indexOf(d.values[0]));
                });
                if (this._duration) {
                    enter.each(calculateStrokeDashArray).attr("stroke-dasharray", function(d) {
                        return "".concat(d.initialStrokeArray, " ").concat(d.initialLength);
                    }).attr("stroke-dashoffset", function(d) {
                        return d.initialLength;
                    }).transition(this._transition).attr("stroke-dashoffset", 0);
                    update = update.transition(this._transition).attrTween("d", function(d) {
                        return interpolatePath(select(this).attr("d"), that._path(d.values));
                    });
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    this._exit.selectAll("path").each(calculateStrokeDashArray).attr("stroke-dasharray", function(d) {
                        return "".concat(d.initialStrokeArray, " ").concat(d.initialLength);
                    }).transition(this._transition).attr("stroke-dashoffset", function(d) {
                        return -d.initialLength;
                    });
                } else {
                    update = update.attr("d", function(d) {
                        return that._path(d.values);
                    });
                }
                update.attr("transform", function(d) {
                    return "translate(".concat(-d.xR[0] - d.width / 2, ", ").concat(-d.yR[0] - d.height / 2, ")");
                }).call(this._applyStyle.bind(this));
                return this;
            }
        },
        {
            /**
      Given a specific data point and index, returns the aesthetic properties of the shape.
      @param data point*
      @param index @private
*/ key: "_aes",
            value: function _aes(d, i) {
                var _this = this;
                return {
                    points: d.values.map(function(p) {
                        return [
                            _this._x(p, i),
                            _this._y(p, i)
                        ];
                    })
                };
            }
        },
        {
            key: "curve",
            value: function curve(_) {
                return arguments.length ? (this._curve = typeof _ === "function" ? _ : constant(_), this) : this._curve;
            }
        },
        {
            key: "defined",
            value: function defined(_) {
                return arguments.length ? (this._defined = _, this) : this._defined;
            }
        }
    ]);
    return Line;
}(Shape);
/**
    Creates SVG lines based on an array of data.
*/ export { Line as default };
