"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;
    };
})();
var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useVirtualizer = useVirtualizer;
exports.useWindowVirtualizer = useWindowVirtualizer;
const React = __importStar(require("react"));
const react_dom_1 = require("react-dom");
const virtual_core_1 = require("../virtual-core");
__exportStar(require("../virtual-core"), exports);
const useIsomorphicLayoutEffect = typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect;
function useVirtualizerBase(options) {
    const rerender = React.useReducer(() => ({}), {})[1];
    const resolvedOptions = {
        ...options,
        onChange: (instance, sync) => {
            var _a;
            if (sync) {
                (0, react_dom_1.flushSync)(rerender);
            }
            else {
                rerender();
            }
            (_a = options.onChange) === null || _a === void 0 ? void 0 : _a.call(options, instance, sync);
        },
    };
    const [instance] = React.useState(() => new virtual_core_1.Virtualizer(resolvedOptions));
    instance.setOptions(resolvedOptions);
    useIsomorphicLayoutEffect(() => {
        return instance._didMount();
    }, []);
    useIsomorphicLayoutEffect(() => {
        return instance._willUpdate();
    });
    return instance;
}
function useVirtualizer(options) {
    return useVirtualizerBase({
        observeElementRect: virtual_core_1.observeElementRect,
        observeElementOffset: virtual_core_1.observeElementOffset,
        scrollToFn: virtual_core_1.elementScroll,
        ...options,
    });
}
function useWindowVirtualizer(options) {
    return useVirtualizerBase({
        getScrollElement: () => (typeof document !== 'undefined' ? window : null),
        observeElementRect: virtual_core_1.observeWindowRect,
        observeElementOffset: virtual_core_1.observeWindowOffset,
        scrollToFn: virtual_core_1.windowScroll,
        initialOffset: () => (typeof document !== 'undefined' ? window.scrollY : 0),
        ...options,
    });
}
