"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScrollViewSectionContainer = exports.ScrollViewSection = void 0;
var react_1 = __importStar(require("react"));
var react_native_1 = require("react-native");
var dot_object_1 = __importDefault(require("dot-object"));
var use_deep_compare_effect_1 = __importStar(require("use-deep-compare-effect"));
var use_immer_1 = require("use-immer");
var ContextScroll = react_1.createContext({});
function useUpdateKey(name) {
    var _a = react_1.useContext(ContextScroll), registerItem = _a.registerItem, unRegisterItem = _a.unRegisterItem;
    react_1.useRef(function () { return function () { return unRegisterItem(name); }; });
    return {
        registerItem: registerItem,
    };
}
function ScrollViewSection(_a) {
    var _b = _a.testID, testID = _b === void 0 ? null : _b, name = _a.name, children = _a.children;
    var viewRef = react_1.useRef();
    var registerItem = useUpdateKey(name).registerItem;
    function onLayout() {
        viewRef.current.measure(function (fx, fy, width, height, px, py) {
            registerItem({
                name: name,
                pos: py,
            });
        });
    }
    return (react_1.default.createElement(react_native_1.View, { testID: testID, ref: viewRef, onLayout: onLayout }, children));
}
exports.ScrollViewSection = ScrollViewSection;
exports.ScrollViewSectionContainer = react_1.default.forwardRef(function ScrollViewSectionContainer(_a, ref) {
    var _b = _a.testID, testID = _b === void 0 ? null : _b, onChange = _a.onChange, children = _a.children;
    var _c = use_immer_1.useImmer({}), items = _c[0], setItems = _c[1];
    var _d = react_1.useState(), currentSection = _d[0], setCurrentSection = _d[1];
    var _e = react_1.useState(0), heightScroll = _e[0], setHeightScroll = _e[1];
    var scrollPosY = react_1.useRef(0);
    var scrollOffset = react_1.useRef(0);
    var itemsArray = react_1.useRef([]);
    var ScrollViewRef = react_1.useRef();
    react_1.useImperativeHandle(ref, function () { return ({
        setSection: function (name) {
            if (items[name]) {
                ScrollViewRef.current.scrollTo({
                    x: 0,
                    y: items[name].pos,
                    animated: true,
                });
                setTimeout(function () {
                    setCurrentSection(items[name]);
                }, 1);
            }
        },
    }); }, [items]);
    react_1.useEffect(function () {
        scrollPosY.current = 0;
    }, []);
    use_deep_compare_effect_1.useDeepCompareEffectNoCheck(function () {
        if (onChange && currentSection) {
            onChange(currentSection.name);
        }
    }, [currentSection, onChange]);
    function registerItem(_a) {
        var name = _a.name, pos = _a.pos;
        setItems(function (draft) {
            dot_object_1.default.set(name, {
                name: name,
                pos: pos + scrollPosY.current - scrollOffset.current,
            }, draft);
        });
    }
    function unRegisterItem(name) {
        setItems(function (draft) {
            dot_object_1.default.remove(name, draft);
        });
    }
    use_deep_compare_effect_1.default(function () {
        itemsArray.current = Object.values(items);
    }, [items]);
    return (react_1.default.createElement(ContextScroll.Provider, { value: {
            registerItem: registerItem,
            unRegisterItem: unRegisterItem,
            items: [],
        } },
        react_1.default.createElement(react_native_1.ScrollView, { ref: ScrollViewRef, testID: testID, onLayout: function (ev) {
                scrollOffset.current = ev.nativeEvent.layout.y;
                setHeightScroll(ev.nativeEvent.layout.height);
            }, onScroll: function (event) {
                scrollPosY.current = event.nativeEvent.contentOffset.y;
            }, onMomentumScrollEnd: function () {
                var currentOffset = 0;
                var selectSection;
                itemsArray.current.forEach(function (item) {
                    if (scrollPosY.current >= item.pos - heightScroll * 0.2 && item.pos >= currentOffset) {
                        currentOffset = item.pos;
                        selectSection = item;
                    }
                });
                setCurrentSection(function () { return selectSection; });
            } }, children)));
});
//# sourceMappingURL=ScrollViewSection.js.map