"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LazyRenderer = void 0;
var react_1 = __importStar(require("react"));
var CUSTOM_EVENT_NAME = 'notify_lazy_renderer';
var DEFAULT_STYLE = { height: '20px' };
var InternalLazyRenderer = function (_a) {
    var children = _a.children, className = _a.className, _b = _a.style, style = _b === void 0 ? DEFAULT_STYLE : _b;
    var _c = (0, react_1.useState)(false), isVisible = _c[0], setIsVisible = _c[1];
    var placeholderRef = (0, react_1.useRef)(null);
    (0, react_1.useEffect)(function () {
        var observer = new IntersectionObserver(function (_a) {
            var entry = _a[0];
            if (entry.isIntersecting) {
                setIsVisible(true);
                observer.disconnect();
                window.removeEventListener(CUSTOM_EVENT_NAME, reobserve);
            }
        }, {
            threshold: 0.1
        });
        if (placeholderRef.current) {
            observer.observe(placeholderRef.current);
        }
        var reobserve = function () {
            if (placeholderRef.current) {
                observer.disconnect();
                observer.observe(placeholderRef.current);
            }
        };
        window.addEventListener(CUSTOM_EVENT_NAME, reobserve);
        return function () {
            observer.disconnect();
            window.removeEventListener(CUSTOM_EVENT_NAME, reobserve);
        };
    }, []);
    return isVisible ? react_1.default.createElement(react_1.default.Fragment, null, children) : react_1.default.createElement("div", { ref: placeholderRef, className: className, style: style });
};
var LazyRenderer = function (_a) {
    var _b = _a.enabled, enabled = _b === void 0 ? true : _b, props = __rest(_a, ["enabled"]);
    return enabled ? react_1.default.createElement(InternalLazyRenderer, __assign({}, props)) : react_1.default.createElement(react_1.default.Fragment, null, props.children);
};
exports.LazyRenderer = LazyRenderer;
exports.LazyRenderer.notify = function () {
    window.dispatchEvent(new Event(CUSTOM_EVENT_NAME));
};
