/**
 * @dyna-grid/core - Advanced Data Grid / Data Table supporting Javascript / Web Components
 * @version v1.0.5
 * @link 
 * @license 
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { BeanStub } from "../context/beanStub";
import { Bean, PostConstruct } from "../context/context";
import { Events } from "../eventKeys";
import { getMaxDivHeight } from "../utils/browser";
/**
 * This class solves the 'max height' problem, where the user might want to show more data than
 * the max div height actually allows.
 */
var MaxDivHeightScaler = /** @class */ (function (_super) {
    __extends(MaxDivHeightScaler, _super);
    function MaxDivHeightScaler() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        // the scrollY position
        _this.scrollY = 0;
        // how tall the body is
        _this.uiBodyHeight = 0;
        return _this;
    }
    MaxDivHeightScaler.prototype.postConstruct = function () {
        this.addManagedListener(this.eventService, Events.EVENT_BODY_HEIGHT_CHANGED, this.updateOffset.bind(this));
        this.maxDivHeight = getMaxDivHeight();
    };
    MaxDivHeightScaler.prototype.registerGridComp = function (gridPanel) {
        this.gridPanel = gridPanel;
    };
    MaxDivHeightScaler.prototype.isScaling = function () {
        return this.scaling;
    };
    MaxDivHeightScaler.prototype.getOffset = function () {
        return this.offset;
    };
    MaxDivHeightScaler.prototype.updateOffset = function () {
        if (!this.scaling) {
            return;
        }
        var newScrollY = this.gridPanel.getVScrollPosition().top;
        var newBodyHeight = this.getUiBodyHeight();
        var atLeastOneChanged = newScrollY !== this.scrollY || newBodyHeight !== this.uiBodyHeight;
        if (atLeastOneChanged) {
            this.scrollY = newScrollY;
            this.uiBodyHeight = newBodyHeight;
            this.calculateOffset();
        }
    };
    MaxDivHeightScaler.prototype.calculateOffset = function () {
        this.uiContainerHeight = this.maxDivHeight;
        this.pixelsToShave = this.modelHeight - this.uiContainerHeight;
        this.maxScrollY = this.uiContainerHeight - this.uiBodyHeight;
        var scrollPercent = this.scrollY / this.maxScrollY;
        this.setOffset(scrollPercent * this.pixelsToShave);
    };
    MaxDivHeightScaler.prototype.clearOffset = function () {
        this.uiContainerHeight = this.modelHeight;
        this.pixelsToShave = 0;
        this.setOffset(0);
    };
    MaxDivHeightScaler.prototype.setOffset = function (newOffset) {
        // because we are talking pixels, no point in confusing things with half numbers
        var newOffsetFloor = typeof newOffset === 'number' ? Math.floor(newOffset) : null;
        if (this.offset === newOffsetFloor) {
            return;
        }
        this.offset = newOffsetFloor;
        this.eventService.dispatchEvent({ type: Events.EVENT_HEIGHT_SCALE_CHANGED });
    };
    MaxDivHeightScaler.prototype.setModelHeight = function (modelHeight) {
        this.modelHeight = modelHeight;
        this.scaling = this.maxDivHeight > 0 && modelHeight > this.maxDivHeight;
        if (this.scaling) {
            this.calculateOffset();
        }
        else {
            this.clearOffset();
        }
    };
    MaxDivHeightScaler.prototype.getUiContainerHeight = function () {
        return this.uiContainerHeight;
    };
    MaxDivHeightScaler.prototype.getRealPixelPosition = function (modelPixel) {
        return modelPixel - this.offset;
    };
    MaxDivHeightScaler.prototype.getUiBodyHeight = function () {
        var pos = this.gridPanel.getVScrollPosition();
        return pos.bottom - pos.top;
    };
    MaxDivHeightScaler.prototype.getScrollPositionForPixel = function (rowTop) {
        if (this.pixelsToShave <= 0) {
            return rowTop;
        }
        var modelMaxScroll = this.modelHeight - this.getUiBodyHeight();
        var scrollPercent = rowTop / modelMaxScroll;
        var scrollPixel = this.maxScrollY * scrollPercent;
        return scrollPixel;
    };
    __decorate([
        PostConstruct
    ], MaxDivHeightScaler.prototype, "postConstruct", null);
    MaxDivHeightScaler = __decorate([
        Bean('maxDivHeightScaler')
    ], MaxDivHeightScaler);
    return MaxDivHeightScaler;
}(BeanStub));
export { MaxDivHeightScaler };
