"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Stretch = void 0;
const React = __importStar(require("react"));
const react_dom_1 = require("react-dom");
class Stretch extends React.Component {
    constructor(props) {
        super(props);
        this.initialized = false;
        this.cameToViewFirstTime = false;
        this.wrapperRef = React.createRef();
        this._onScroll = () => {
            let callback = undefined;
            const state = this.getState();
            if (!this.cameToViewFirstTime && (state.height || 0) > (this.props.enterViewPortTreshold || 0)) {
                this.cameToViewFirstTime = true;
                if (this.props.onEnterViewPort) {
                    callback = () => { var _a, _b; return (_b = (_a = this.props).onEnterViewPort) === null || _b === void 0 ? void 0 : _b.call(_a); };
                }
            }
            this.update(state, callback);
        };
        this.onScroll = () => {
            requestAnimationFrame(this._onScroll);
        };
        this.update = (state, callback) => {
            const afterStateChange = () => {
                var _a, _b;
                (_b = (_a = this.props).onResize) === null || _b === void 0 ? void 0 : _b.call(_a);
                callback === null || callback === void 0 ? void 0 : callback();
            };
            state ? this.setState(state, () => {
                afterStateChange();
            }) : afterStateChange;
        };
        this.getState = () => {
            const { containerRef, topOffset = 0, bottomOffset = 0, minHeight } = this.props;
            let container = (0, react_dom_1.findDOMNode)(containerRef.current);
            if (this.wrapperRef &&
                container.getBoundingClientRect().top !== (0, react_dom_1.findDOMNode)(this.wrapperRef.current).getBoundingClientRect().top) {
                return {
                    horizontallyAligned: false
                };
            }
            let containerHeight = container.offsetHeight;
            if (minHeight && containerHeight < minHeight) {
                containerHeight = minHeight;
                container = (0, react_dom_1.findDOMNode)(this.wrapperRef.current);
            }
            const viewportHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
            const bottomDist = viewportHeight - container.getBoundingClientRect().top - containerHeight;
            const bottomInvisibleHeight = Math.min(bottomDist, 0);
            return {
                horizontallyAligned: true,
                containerHeight,
                height: Math.max(containerHeight
                    + Math.min(container.getBoundingClientRect().top, 0)
                    + Math.min(bottomInvisibleHeight, 0)
                    - (container.getBoundingClientRect().top < topOffset ? Math.min(topOffset - container.getBoundingClientRect().top, topOffset) : 0)
                    - (bottomDist < bottomOffset ? Math.min(bottomOffset - bottomDist, bottomOffset) : 0), 0),
                top: Math.max(-container.getBoundingClientRect().top + topOffset, 0)
            };
        };
        this.state = {};
    }
    UNSAFE_componentWillReceiveProps(props) {
        if (props.mounted && !this.initialized) {
            const state = this.getState();
            this.update(state);
            this.cameToViewFirstTime = (state.height || 0) > (this.props.enterViewPortTreshold || 0);
            this.initialized = true;
        }
    }
    componentDidMount() {
        window.addEventListener("scroll", this.onScroll);
        window.addEventListener("resize", this.onScroll);
    }
    componentWillUnmount() {
        window.removeEventListener("scroll", this.onScroll);
        window.removeEventListener("resize", this.onScroll);
    }
    render() {
        const { children } = this.props;
        const wrapperStyle = {
            height: this.state.horizontallyAligned ? this.state.containerHeight : this.props.minHeight,
        };
        const style = {
            position: "relative",
            top: this.state.horizontallyAligned ? this.state.top : undefined,
            height: this.state.horizontallyAligned ? this.state.height : "100%",
        };
        return (React.createElement("div", { ref: this.wrapperRef, style: wrapperStyle, className: this.props.className },
            React.createElement("div", { style: style }, children)));
    }
}
exports.Stretch = Stretch;
