import * as tslib_1 from "tslib";
import { debounce, AEventDispatcher } from '../../internal';
import { isGroup } from '../../model';
import { DataProvider } from '../../provider';
import EngineRenderer from '../EngineRenderer';
var TaggleRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(TaggleRenderer, _super);
    function TaggleRenderer(data, parent, options) {
        var _this = _super.call(this) || this;
        _this.data = data;
        _this.isDynamicLeafHeight = false;
        _this.rule = null;
        _this.levelOfDetail = null;
        _this.resizeListener = function () { return debounce(function () { return _this.update(); }, 100); };
        _this.options = {
            violationChanged: function () { return undefined; },
            rowPadding: 2
        };
        Object.assign(_this.options, options);
        _this.renderer = new EngineRenderer(data, parent, Object.assign({}, options, {
            dynamicHeight: function (data, ranking) {
                var r = _this.dynamicHeight(data, ranking);
                if (r) {
                    return r;
                }
                return options.dynamicHeight ? options.dynamicHeight(data, ranking) : null;
            },
            levelOfDetail: function (rowIndex) { return _this.levelOfDetail ? _this.levelOfDetail(rowIndex) : 'high'; }
        }));
        _this.data.on(DataProvider.EVENT_SELECTION_CHANGED + ".rule", function () {
            if (_this.isDynamicLeafHeight) {
                _this.update();
            }
        });
        _this.forward(_this.renderer, TaggleRenderer.EVENT_HIGHLIGHT_CHANGED + ".main");
        window.addEventListener('resize', _this.resizeListener, {
            passive: true
        });
        return _this;
    }
    Object.defineProperty(TaggleRenderer.prototype, "style", {
        get: function () {
            return this.renderer.style;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(TaggleRenderer.prototype, "ctx", {
        get: function () {
            return this.renderer.ctx;
        },
        enumerable: true,
        configurable: true
    });
    TaggleRenderer.prototype.pushUpdateAble = function (updateAble) {
        this.renderer.pushUpdateAble(updateAble);
    };
    TaggleRenderer.prototype.dynamicHeight = function (data, ranking) {
        var _this = this;
        if (!this.rule) {
            this.levelOfDetail = null;
            return null;
        }
        var availableHeight = this.renderer ? this.renderer.node.querySelector('main').clientHeight : 100;
        var topNGetter = function (group) { return _this.data.getTopNAggregated(ranking, group); };
        var instance = this.rule.apply(data, availableHeight, new Set(this.data.getSelection()), topNGetter);
        this.isDynamicLeafHeight = typeof instance.item === 'function';
        this.options.violationChanged(this.rule, instance.violation || '');
        var height = function (item) {
            if (isGroup(item)) {
                return typeof instance.group === 'number' ? instance.group : instance.group(item);
            }
            return typeof instance.item === 'number' ? instance.item : instance.item(item);
        };
        this.levelOfDetail = function (rowIndex) {
            var item = data[rowIndex];
            return _this.rule ? _this.rule.levelOfDetail(item, height(item)) : 'high';
        };
        // padding is always 0 since included in height
        // const padding = (item: IGroupData | IGroupItem | null) => {
        //   if (!item) {
        //     item = data[0];
        //   }
        //   const lod = this.rule ? this.rule.levelOfDetail(item, height(item)) : 'high';
        //   return lod === 'high' ? 0 : 0; // always 0 since
        // };
        return {
            defaultHeight: typeof instance.item === 'number' ? instance.item : NaN,
            height: height,
            padding: 0
        };
    };
    TaggleRenderer.prototype.createEventList = function () {
        return _super.prototype.createEventList.call(this).concat([TaggleRenderer.EVENT_HIGHLIGHT_CHANGED]);
    };
    TaggleRenderer.prototype.on = function (type, listener) {
        return _super.prototype.on.call(this, type, listener);
    };
    TaggleRenderer.prototype.zoomOut = function () {
        this.renderer.zoomOut();
    };
    TaggleRenderer.prototype.zoomIn = function () {
        this.renderer.zoomIn();
    };
    TaggleRenderer.prototype.switchRule = function (rule) {
        if (this.rule === rule) {
            return;
        }
        this.rule = rule;
        this.update();
    };
    TaggleRenderer.prototype.destroy = function () {
        this.renderer.destroy();
        window.removeEventListener('resize', this.resizeListener);
    };
    TaggleRenderer.prototype.update = function () {
        this.renderer.update();
    };
    TaggleRenderer.prototype.setDataProvider = function (data) {
        var _this = this;
        if (this.data) {
            this.data.on(DataProvider.EVENT_SELECTION_CHANGED + ".rule", null);
        }
        this.data = data;
        this.data.on(DataProvider.EVENT_SELECTION_CHANGED + ".rule", function () {
            if (_this.isDynamicLeafHeight) {
                _this.update();
            }
        });
        this.renderer.setDataProvider(data);
        this.update();
    };
    TaggleRenderer.prototype.setHighlight = function (dataIndex, scrollIntoView) {
        return this.renderer.setHighlight(dataIndex, scrollIntoView);
    };
    TaggleRenderer.prototype.getHighlight = function () {
        return this.renderer.getHighlight();
    };
    TaggleRenderer.prototype.enableHighlightListening = function (enable) {
        this.renderer.enableHighlightListening(enable);
    };
    TaggleRenderer.EVENT_HIGHLIGHT_CHANGED = EngineRenderer.EVENT_HIGHLIGHT_CHANGED;
    return TaggleRenderer;
}(AEventDispatcher));
export default TaggleRenderer;
//# sourceMappingURL=TaggleRenderer.js.map