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 _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 _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 _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 { min } from "d3-array";
import { arc, pie } from "d3-shape";
import { Path } from "../shapes/index.js";
import { assign, elem } from "@d3plus/dom";
import { accessor, configPrep } from "../utils/index.js";
import Viz from "./Viz.js";
var Pie = /*#__PURE__*/ function(Viz) {
    "use strict";
    _inherits(Pie, Viz);
    function Pie() {
        _class_call_check(this, Pie);
        var _this;
        _this = _call_super(this, Pie);
        var defaultLegend = _this._legend;
        _this._legend = function(config, arr) {
            if (arr.length === _this._filteredData.length) return false;
            return defaultLegend.bind(_this)(config, arr);
        };
        _this._legendSort = function(a, b) {
            return _this._value(b) - _this._value(a);
        };
        _this._shapeConfig = assign(_this._shapeConfig, {
            ariaLabel: function ariaLabel(d, i) {
                return _this._pieData ? "".concat(++_this._pieData[i].index, ". ").concat(_this._drawLabel(d, i), ", ").concat(_this._value(d, i), ".") : "";
            },
            Path: {
                labelConfig: {
                    fontResize: true
                }
            }
        });
        _this._innerRadius = 0;
        _this._legendSort = function(a, b) {
            return _this._value(b) - _this._value(a);
        };
        _this._padPixel = 0;
        _this._pie = pie();
        _this._sort = function(a, b) {
            return _this._value(b) - _this._value(a);
        };
        _this._value = accessor("value");
        return _this;
    }
    _create_class(Pie, [
        {
            /**
      Extends the draw behavior of the abstract Viz class.
      @private
  */ key: "_draw",
            value: function _draw(callback) {
                var _this = this;
                _get(_get_prototype_of(Pie.prototype), "_draw", this).call(this, callback);
                var height = this._height - this._margin.top - this._margin.bottom, width = this._width - this._margin.left - this._margin.right;
                var outerRadius = min([
                    width,
                    height
                ]) / 2;
                var pieData = this._pieData = this._pie.padAngle(this._padAngle || this._padPixel / outerRadius).sort(this._sort).value(this._value)(this._filteredData);
                pieData.forEach(function(d, i) {
                    d.__d3plus__ = true;
                    d.i = i;
                });
                var arcData = arc().innerRadius(this._innerRadius).outerRadius(outerRadius);
                var transform = "translate(".concat(width / 2 + this._margin.left, ", ").concat(height / 2 + this._margin.top, ")");
                this._shapes.push(new Path().data(pieData).d(arcData).select(elem("g.d3plus-Pie", {
                    parent: this._select,
                    enter: {
                        transform: transform
                    },
                    update: {
                        transform: transform
                    }
                }).node()).config({
                    id: function id(d) {
                        return _this._ids(d).join("-");
                    },
                    x: 0,
                    y: 0
                }).label(this._drawLabel).config(configPrep.bind(this)(this._shapeConfig, "shape", "Path")).render());
                return this;
            }
        },
        {
            /**
      The pixel value, or function that returns a pixel value, that is used as the inner radius of the Pie (creating a Donut).
*/ key: "innerRadius",
            value: function innerRadius(_) {
                return arguments.length ? (this._innerRadius = _, this) : this._innerRadius;
            }
        },
        {
            /**
      The padding between each arc, set as a radian value between \`0\` and \`1\`.

If set, this will override any previously set padPixel value.
*/ key: "padAngle",
            value: function padAngle(_) {
                return arguments.length ? (this._padAngle = _, this) : this._padAngle;
            }
        },
        {
            /**
      The padding between each arc, set as a pixel number value.

By default the value is \`0\`, which shows no padding between each arc.

If \`padAngle\` is defined, the \`padPixel\` value will not be considered.
*/ key: "padPixel",
            value: function padPixel(_) {
                return arguments.length ? (this._padPixel = _, this) : this._padPixel;
            }
        },
        {
            /**
      A comparator function that sorts the Pie slices.
*/ key: "sort",
            value: function sort(_) {
                return arguments.length ? (this._sort = _, this) : this._sort;
            }
        },
        {
            /**
      The accessor key for each data point used to calculate the size of each Pie section.
  */ key: "value",
            value: function value(_) {
                return arguments.length ? (this._value = typeof _ === "function" ? _ : accessor(_), this) : this._value;
            }
        }
    ]);
    return Pie;
}(Viz);
/**
    Uses the [d3 pie layout](https://github.com/d3/d3-shape#pies) to creates SVG arcs based on an array of data.
*/ export { Pie as default };
