"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Hooks
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAnalysisAnimationDataProvider = void 0;
const React = require("react");
const AnalysisAnimationProvider_1 = require("../timeline/AnalysisAnimationProvider");
/** @internal */
function useSupportsAnalysisAnimation(viewport) {
    const [supportsAnalysisAnimation, setSupportsAnalysisAnimation] = React.useState(!!viewport?.view?.analysisStyle);
    React.useEffect(() => {
        setSupportsAnalysisAnimation(!!viewport?.view?.analysisStyle);
    }, [viewport]);
    React.useEffect(() => {
        return viewport?.onChangeView.addListener((vp) => {
            const hasAnalysisData = !!vp?.view?.analysisStyle;
            if (hasAnalysisData !== supportsAnalysisAnimation)
                setSupportsAnalysisAnimation(hasAnalysisData);
        });
    }, [supportsAnalysisAnimation, viewport]);
    React.useEffect(() => {
        return viewport?.onDisplayStyleChanged.addListener((vp) => {
            const hasAnalysisData = !!vp?.view?.analysisStyle;
            if (hasAnalysisData !== supportsAnalysisAnimation)
                setSupportsAnalysisAnimation(hasAnalysisData);
        });
    }, [viewport, supportsAnalysisAnimation]);
    return supportsAnalysisAnimation;
}
/** Hook that returns either a AnalysisAnimationTimelineDataProvider or undefined based on if the supplied viewport contains analysis data.
 * @public
 **/
function useAnalysisAnimationDataProvider(viewport) {
    const supportsAnalysisAnimation = useSupportsAnalysisAnimation(viewport);
    const [analysisAnimationTimelineDataProvider, setAnalysisAnimationTimelineDataProvider,] = React.useState();
    const isMountedRef = React.useRef(false);
    React.useEffect(() => {
        isMountedRef.current = true;
        return () => {
            isMountedRef.current = false;
        };
    }, []);
    React.useEffect(() => {
        async function fetchNewDataProvider(vp) {
            let newProvider = new AnalysisAnimationProvider_1.AnalysisAnimationTimelineDataProvider(vp.view, vp);
            if (newProvider?.supportsTimelineAnimation) {
                const dataLoaded = await newProvider.loadTimelineData();
                if (!dataLoaded)
                    newProvider = undefined;
            }
            isMountedRef.current &&
                setAnalysisAnimationTimelineDataProvider(newProvider);
        }
        if (supportsAnalysisAnimation && viewport)
            void fetchNewDataProvider(viewport);
        else
            isMountedRef.current &&
                setAnalysisAnimationTimelineDataProvider(undefined);
    }, [supportsAnalysisAnimation, viewport]);
    return analysisAnimationTimelineDataProvider;
}
exports.useAnalysisAnimationDataProvider = useAnalysisAnimationDataProvider;
//# sourceMappingURL=useAnalysisAnimationDataProvider.js.map