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(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 } 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 { largestRect } from "@d3plus/math";
import { accessor, constant } from "../utils/index.js";
import Shape from "./Shape.js";
var Area = /*#__PURE__*/ function(Shape) {
    "use strict";
    _inherits(Area, Shape);
    function Area() {
        _class_call_check(this, Area);
        var _this;
        _this = _call_super(this, Area), _define_property(_this, "_curve", void 0), _define_property(_this, "_defined", void 0), _define_property(_this, "_x0", void 0), _define_property(_this, "_x1", void 0), _define_property(_this, "_y0", void 0), _define_property(_this, "_y1", void 0);
        _this._curve = constant("linear");
        _this._defined = function() {
            return true;
        };
        _this._labelBounds = function(d, i, aes) {
            var r = largestRect(aes.points);
            if (!r) return null;
            return {
                angle: r.angle,
                width: r.width,
                height: r.height,
                x: r.cx - r.width / 2 - _this._x(d, i),
                y: r.cy - r.height / 2 - _this._y(d, i)
            };
        };
        _this._labelConfig = Object.assign(_this._labelConfig, {
            textAnchor: "middle",
            verticalAlign: "middle"
        });
        _this._name = "Area";
        _this._x = accessor("x");
        _this._x0 = accessor("x");
        _this._x1 = null;
        _this._y = constant(0);
        _this._y0 = constant(0);
        _this._y1 = accessor("y");
        return _this;
    }
    _create_class(Area, [
        {
            /**
      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) {
                var _this = this;
                var values = d.values.slice().sort(function(a, b) {
                    return _this._y1 ? _this._x(a) - _this._x(b) : _this._y(a) - _this._y(b);
                });
                var points1 = values.map(function(v, z) {
                    return [
                        _this._x0(v, z),
                        _this._y0(v, z)
                    ];
                });
                var points2 = values.reverse().map(function(v, z) {
                    return _this._y1 ? [
                        _this._x(v, z),
                        _this._y1(v, z)
                    ] : [
                        _this._x1(v, z),
                        _this._y(v, z)
                    ];
                });
                var points = points1.concat(points2);
                if (points1[0][1] > points2[0][1]) points = points.reverse();
                points.push(points[0]);
                return {
                    points: points
                };
            }
        },
        {
            /**
      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 areas = 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.map(_this._x).concat(d.values.map(_this._x0)).concat(_this._x1 ? d.values.map(_this._x1) : []));
                    d.xR = x;
                    d.width = x[1] - x[0];
                    d.x = x[0] + d.width / 2;
                    var y = extent(d.values.map(_this._y).concat(d.values.map(_this._y0)).concat(_this._y1 ? d.values.map(_this._y1) : []));
                    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;
                });
                areas.key = function(d) {
                    return d.key;
                };
                return areas;
            }
        },
        {
            /**
      Draws the area polygons.
    @param callback Optional callback invoked after rendering completes.
*/ key: "render",
            value: function render(callback) {
                var _this = this;
                _get(_get_prototype_of(Area.prototype), "render", this).call(this, callback);
                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
                var path = this._path = paths.area().defined(this._defined).curve(curve).x(this._x).x0(this._x0).x1(this._x1).y(this._y).y0(this._y0).y1(this._y1);
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                var exitPath = paths.area().defined(function(d) {
                    return d;
                }).curve(curve).x(this._x).y(this._y).x0(function(d, i) {
                    return _this._x1 ? _this._x0(d, i) + (_this._x1(d, i) - _this._x0(d, i)) / 2 : _this._x0(d, i);
                }).x1(function(d, i) {
                    return _this._x1 ? _this._x0(d, i) + (_this._x1(d, i) - _this._x0(d, i)) / 2 : _this._x0(d, i);
                }).y0(function(d, i) {
                    return _this._y1 ? _this._y0(d, i) + (_this._y1(d, i) - _this._y0(d, i)) / 2 : _this._y0(d, i);
                }).y1(function(d, i) {
                    return _this._y1 ? _this._y0(d, i) + (_this._y1(d, i) - _this._y0(d, i)) / 2 : _this._y0(d, i);
                });
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                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 exitPath(d.values);
                }).call(this._applyStyle.bind(this)).transition(this._transition).attrTween("d", function(d) {
                    return interpolatePath(select(this).attr("d"), path(d.values));
                });
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                this._update.select("path").transition(this._transition).attr("transform", function(d) {
                    return "translate(".concat(-d.xR[0] - d.width / 2, ", ").concat(-d.yR[0] - d.height / 2, ")");
                }).attrTween("d", function(d) {
                    return interpolatePath(select(this).attr("d"), path(d.values));
                })// eslint-disable-next-line @typescript-eslint/no-explicit-any
                .call(this._applyStyle.bind(this));
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                this._exit.select("path").transition(this._transition).attrTween("d", function(d) {
                    return interpolatePath(select(this).attr("d"), exitPath(d.values));
                });
                return this;
            }
        },
        {
            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;
            }
        },
        {
            key: "x",
            value: function x(_) {
                if (!arguments.length) return this._x;
                this._x = typeof _ === "function" ? _ : constant(_);
                this._x0 = this._x;
                return this;
            }
        },
        {
            key: "x0",
            value: function x0(_) {
                if (!arguments.length) return this._x0;
                this._x0 = typeof _ === "function" ? _ : constant(_);
                this._x = this._x0;
                return this;
            }
        },
        {
            key: "x1",
            value: function x1(_) {
                return arguments.length ? (this._x1 = typeof _ === "function" ? _ : _ === null ? null : constant(_), this) : this._x1;
            }
        },
        {
            key: "y",
            value: function y(_) {
                if (!arguments.length) return this._y;
                this._y = typeof _ === "function" ? _ : constant(_);
                this._y0 = this._y;
                return this;
            }
        },
        {
            key: "y0",
            value: function y0(_) {
                if (!arguments.length) return this._y0;
                this._y0 = typeof _ === "function" ? _ : constant(_);
                this._y = this._y0;
                return this;
            }
        },
        {
            key: "y1",
            value: function y1(_) {
                return arguments.length ? (this._y1 = typeof _ === "function" ? _ : _ === null ? null : constant(_), this) : this._y1;
            }
        }
    ]);
    return Area;
}(Shape);
/**
    Creates SVG areas based on an array of data.
*/ export { Area as default };
