"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenderPassInfo = void 0;
var DefaultRenderLayer_1 = require("../../types/DefaultRenderLayer");
var BaseRenderableSeries_1 = require("../Visuals/RenderableSeries/BaseRenderableSeries");
var RenderPassInfo = /** @class */ (function () {
    function RenderPassInfo(seriesCount, seriesViewRect) {
        this.renderableSeriesArray = [];
        this.rendermap = new Map();
        this.reverseMap = new Map();
        this.relativeItems = [];
        /**
         * This flag is used to trace if there are other renderables at the SeriesLayer.
         * If there are we need to call renderContext.setTranslationRotationAndClip() before rendering each renderable series
         */
        this.hasOtherSeriesLayerRenderables = false;
        this.seriesViewRect = seriesViewRect;
    }
    RenderPassInfo.prototype.addRenderable = function (renderable, drawFunction) {
        var _a;
        if (!this.hasOtherSeriesLayerRenderables) {
            if (!(renderable instanceof BaseRenderableSeries_1.BaseRenderableSeries) &&
                renderable.getRenderOrder() === DefaultRenderLayer_1.EDefaultRenderLayer.SeriesLayer) {
                this.hasOtherSeriesLayerRenderables = true;
            }
        }
        var surfaceOrder = renderable.getSurfaceRenderOrder();
        var layerOrder = renderable.getRenderLayer();
        if (renderable.getRenderNextTo()) {
            if (!this.hasOtherSeriesLayerRenderables) {
                if (renderable.getRenderNextTo().renderable instanceof BaseRenderableSeries_1.BaseRenderableSeries) {
                    this.hasOtherSeriesLayerRenderables = true;
                }
            }
            this.relativeItems.push({ renderable: renderable, drawFunction: drawFunction, tries: 0 });
            return;
        }
        if (!this.rendermap.has(surfaceOrder)) {
            this.rendermap.set(surfaceOrder, new Map());
        }
        if (!this.rendermap.get(surfaceOrder).get(layerOrder)) {
            this.rendermap.get(surfaceOrder).set(layerOrder, []);
        }
        var items = this.rendermap.get(surfaceOrder).get(layerOrder);
        var itemOrder = (_a = renderable.getRenderOrder()) !== null && _a !== void 0 ? _a : items.length;
        items.push({ renderable: renderable, resolvedOrder: itemOrder, drawFunction: drawFunction });
        // maybe we can change this structure to store a renderOrder as well
        this.reverseMap.set(renderable.id, { surfaceOrder: surfaceOrder, layer: layerOrder, resolvedOrder: itemOrder });
    };
    RenderPassInfo.prototype.resolveNextTo = function () {
        while (this.relativeItems.length > 0) {
            var item = this.relativeItems.shift();
            var nextTo = item.renderable.getRenderNextTo();
            var referenceRenderableId = typeof nextTo.renderable === "string" ? nextTo.renderable : nextTo.renderable.id;
            if (item.tries < this.relativeItems.length + 3) {
                // find item in reverseMap
                if (this.reverseMap.has(referenceRenderableId)) {
                    // here we expect nextTo.renderable to be an instance again
                    var _a = this.reverseMap.get(referenceRenderableId), surfaceOrder = _a.surfaceOrder, layer = _a.layer, resolvedOrder = _a.resolvedOrder;
                    var items = this.rendermap.get(surfaceOrder).get(layer);
                    var newResolvedOrder = resolvedOrder + nextTo.offset;
                    items.push({
                        renderable: item.renderable,
                        resolvedOrder: newResolvedOrder,
                        drawFunction: item.drawFunction
                    });
                    this.reverseMap.set(item.renderable.id, { surfaceOrder: surfaceOrder, layer: layer, resolvedOrder: newResolvedOrder });
                }
                else {
                    item.tries += 1;
                    this.relativeItems.push(item);
                }
            }
            else {
                console.warn("Could not find relative item ".concat(referenceRenderableId, " "));
            }
        }
    };
    return RenderPassInfo;
}());
exports.RenderPassInfo = RenderPassInfo;
