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, max, range } from "d3-array";
import { scaleBand } from "d3-scale";
import { Axis } from "../components/index.js";
import { assign, date, elem } from "@d3plus/dom";
import { nestGroups } from "@d3plus/data";
import { Rect } from "../shapes/index.js";
import { accessor, configPrep } from "../utils/index.js";
import Viz from "./Viz.js";
var Priestley = /*#__PURE__*/ function(Viz) {
    "use strict";
    _inherits(Priestley, Viz);
    function Priestley() {
        _class_call_check(this, Priestley);
        var _this;
        _this = _call_super(this, Priestley);
        _this._axis = new Axis().align("end").orient("bottom");
        _this._axisConfig = {
            scale: "time"
        };
        _this._axisTest = new Axis().align("end").gridSize(0).orient("bottom");
        _this.end("end");
        _this._paddingInner = 0.05;
        _this._paddingOuter = 0.05;
        _this._shapeConfig = assign({}, _this._shapeConfig, {
            ariaLabel: function ariaLabel(d, i) {
                return "".concat(_this._drawLabel(d, i), ", ").concat(_this._start(d, i), " - ").concat(_this._end(d, i), ".");
            }
        });
        _this.start("start");
        return _this;
    }
    _create_class(Priestley, [
        {
            /**
      Extends the render behavior of the abstract Viz class.
      @private
*/ key: "_draw",
            value: function _draw(callback) {
                var _this = this;
                _get(_get_prototype_of(Priestley.prototype), "_draw", this).call(this, callback);
                if (!this._filteredData) return this;
                var data = this._filteredData.map(function(data, i) {
                    return {
                        __d3plus__: true,
                        data: data,
                        end: _this._axisConfig.scale === "time" ? date(_this._end(data, i)) : _this._end(data, i),
                        i: i,
                        id: _this._id(data, i),
                        start: _this._axisConfig.scale === "time" ? date(_this._start(data, i)) : _this._start(data, i)
                    };
                }).filter(function(d) {
                    return d.end - d.start > 0;
                }).sort(function(a, b) {
                    return a.start - b.start;
                });
                var nestedData;
                if (this._groupBy.length > 1 && this._drawDepth > 0) {
                    var _loop = function(i) {
                        keyFns.push(function(d) {
                            return _this._groupBy[i](d.data, d.i);
                        });
                    };
                    var keyFns = [];
                    for(var i = 0; i < this._drawDepth; i++)_loop(i);
                    nestedData = nestGroups(data, keyFns);
                } else nestedData = [
                    {
                        values: data
                    }
                ];
                var maxLane = 0;
                nestedData.forEach(function(g) {
                    var track = [];
                    g.values.forEach(function(d) {
                        track = track.map(function(t) {
                            return t <= d.start ? false : t;
                        });
                        var i = track.indexOf(false);
                        if (i < 0) {
                            d.lane = maxLane + track.length;
                            track.push(d.end);
                        } else {
                            track[i] = d.end;
                            d.lane = maxLane + i;
                        }
                    });
                    maxLane += track.length;
                });
                var axisConfig = {
                    domain: [
                        min(data, function(d) {
                            return d.start;
                        }) || 0,
                        max(data, function(d) {
                            return d.end;
                        }) || 0
                    ],
                    height: this._height - this._margin.top - this._margin.bottom,
                    width: this._width - this._margin.left - this._margin.right
                };
                var transform = "translate(".concat(this._margin.left, ", ").concat(this._margin.top, ")");
                this._axisTest.config(axisConfig).config(this._axisConfig).select(elem("g.d3plus-priestley-axis-test", {
                    parent: this._select,
                    enter: {
                        opacity: 0
                    }
                }).node()).render();
                this._axis.config(axisConfig).config(this._axisConfig).select(elem("g.d3plus-priestley-axis", {
                    parent: this._select,
                    enter: {
                        transform: transform
                    },
                    update: {
                        transform: transform
                    }
                }).node()).render();
                var axisPad = this._axisTest._padding;
                var xScale = this._axis._d3Scale;
                var yScale = scaleBand().domain(range(0, maxLane, 1)).paddingInner(this._paddingInner).paddingOuter(this._paddingOuter).rangeRound([
                    this._height - this._margin.bottom - this._axisTest.outerBounds().height - axisPad,
                    this._margin.top + axisPad
                ]);
                var bandWidth = yScale.bandwidth();
                this._shapes.push(new Rect().data(data).duration(this._duration).height(bandWidth).label(function(d, i) {
                    return _this._drawLabel(d.data, i);
                }).select(elem("g.d3plus-priestley-shapes", {
                    parent: this._select
                }).node()).width(function(d) {
                    var w = Math.abs(xScale(d.end) - xScale(d.start));
                    return w > 2 ? w - 2 : w;
                }).x(function(d) {
                    return xScale(d.start) + (xScale(d.end) - xScale(d.start)) / 2;
                }).y(function(d) {
                    return yScale(d.lane) + bandWidth / 2;
                }).config(configPrep.bind(this)(this._shapeConfig, "shape", "Rect")).render());
                return this;
            }
        },
        {
            /**
      Configuration object for the axis.
*/ key: "axisConfig",
            value: function axisConfig(_) {
                return arguments.length ? (this._axisConfig = assign(this._axisConfig, _), this) : this._axisConfig;
            }
        },
        {
            /**
      Accessor function or string key for the end date of each data point.
*/ key: "end",
            value: function end(_) {
                if (arguments.length) {
                    if (typeof _ === "function") this._end = _;
                    else {
                        this._end = accessor(_);
                        if (!this._aggs[_]) this._aggs[_] = max;
                    }
                    return this;
                } else return this._end;
            }
        },
        {
            /**
      The [paddingInner](https://github.com/d3/d3-scale#band_paddingInner) value of the underlining [Band Scale](https://github.com/d3/d3-scale#band-scales) used to determine the height of each bar. Values should be a ratio between 0 and 1 representing the space in between each rectangle.
*/ key: "paddingInner",
            value: function paddingInner(_) {
                return arguments.length ? (this._paddingInner = _, this) : this._paddingInner;
            }
        },
        {
            /**
      The [paddingOuter](https://github.com/d3/d3-scale#band_paddingOuter) value of the underlining [Band Scale](https://github.com/d3/d3-scale#band-scales) used to determine the height of each bar. Values should be a ratio between 0 and 1 representing the space around the outer rectangles.
*/ key: "paddingOuter",
            value: function paddingOuter(_) {
                return arguments.length ? (this._paddingOuter = _, this) : this._paddingOuter;
            }
        },
        {
            /**
      Accessor function or string key for the start date of each data point.
*/ key: "start",
            value: function start(_) {
                if (arguments.length) {
                    if (typeof _ === "function") this._start = _;
                    else {
                        this._start = accessor(_);
                        if (!this._aggs[_]) this._aggs[_] = min;
                    }
                    return this;
                } else return this._start;
            }
        }
    ]);
    return Priestley;
}(Viz);
/**
    Creates a priestley timeline based on an array of data.
*/ export { Priestley as default };
