"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 (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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScrollToAddedAttributeProvider = exports.ScrollToAddedAttributeContext = void 0;
var react_1 = __importStar(require("react"));
var EMPTY_SET = new Set();
var HIGHLIGHT_DURATION_MS = 2000;
exports.ScrollToAddedAttributeContext = react_1.default.createContext({
    highlightedIds: EMPTY_SET,
    highlight: function () { },
    scrollToFirst: function () { }
});
var ScrollToAddedAttributeProvider = function (_a) {
    var children = _a.children;
    var _b = (0, react_1.useState)(EMPTY_SET), highlightedIds = _b[0], setHighlightedIds = _b[1];
    var highlightTimer = (0, react_1.useRef)(null);
    var highlightDelay = (0, react_1.useRef)(null);
    var bestScrollTarget = (0, react_1.useRef)(null);
    var scrollRafId = (0, react_1.useRef)(0);
    var highlight = (0, react_1.useCallback)(function (addedIds) {
        if (addedIds.length === 0)
            return;
        bestScrollTarget.current = null;
        clearTimeout(highlightTimer.current);
        clearTimeout(highlightDelay.current);
        highlightDelay.current = setTimeout(function () {
            setHighlightedIds(new Set(addedIds));
            highlightTimer.current = setTimeout(function () { return setHighlightedIds(EMPTY_SET); }, HIGHLIGHT_DURATION_MS);
        }, 300);
    }, []);
    var scrollToFirst = (0, react_1.useCallback)(function (ref) {
        if (!ref.current)
            return;
        var el = ref.current;
        if (!bestScrollTarget.current ||
            el.getBoundingClientRect().top < bestScrollTarget.current.getBoundingClientRect().top) {
            bestScrollTarget.current = el;
        }
        cancelAnimationFrame(scrollRafId.current);
        scrollRafId.current = requestAnimationFrame(function () {
            var _a, _b;
            (_b = (_a = bestScrollTarget.current) === null || _a === void 0 ? void 0 : _a.scrollIntoView) === null || _b === void 0 ? void 0 : _b.call(_a, { behavior: 'smooth', block: 'center' });
            bestScrollTarget.current = null;
        });
    }, []);
    (0, react_1.useEffect)(function () {
        return function () {
            clearTimeout(highlightTimer.current);
            clearTimeout(highlightDelay.current);
            cancelAnimationFrame(scrollRafId.current);
        };
    }, []);
    var value = (0, react_1.useMemo)(function () { return ({ highlightedIds: highlightedIds, highlight: highlight, scrollToFirst: scrollToFirst }); }, [highlightedIds, highlight, scrollToFirst]);
    return react_1.default.createElement(exports.ScrollToAddedAttributeContext.Provider, { value: value }, children);
};
exports.ScrollToAddedAttributeProvider = ScrollToAddedAttributeProvider;
