/*
 * Copyright (C) Contributors to the Suwayomi project
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

import type { ComponentProps } from 'react';
import { useRef } from 'react';
import type { StateSnapshot, VirtuosoHandle } from 'react-virtuoso';
import { Virtuoso } from 'react-virtuoso';
import { useMergedRef } from '@mantine/hooks';
import { VirtuosoUtil } from '@/lib/virtuoso/Virtuoso.util.tsx';

export const VirtuosoPersisted = ({
    ref: passedRef,
    persistKey,
    ...props
}: ComponentProps<typeof Virtuoso> & { persistKey: string }) => {
    const { state, persistState } = VirtuosoUtil.usePersistState<StateSnapshot>(persistKey);

    const localRef = useRef<VirtuosoHandle>(undefined);
    const ref = useMergedRef(localRef, passedRef);

    return (
        <Virtuoso
            {...props}
            ref={ref}
            isScrolling={(isScrolling) => {
                if (!isScrolling) {
                    localRef.current?.getState(persistState);
                }

                props.isScrolling?.(isScrolling);
            }}
            restoreStateFrom={state}
        />
    );
};
