/*---------------------------------------------------------------------------------------------
 * 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
 */
import * as React from "react";
import { AnalysisAnimationTimelineDataProvider } from "../timeline/AnalysisAnimationProvider";
/** @internal */
// istanbul ignore next
function useSupportsAnalysisAnimation(viewport) {
    var _a;
    const [supportsAnalysisAnimation, setSupportsAnalysisAnimation] = React.useState(!!((_a = viewport === null || viewport === void 0 ? void 0 : viewport.view) === null || _a === void 0 ? void 0 : _a.analysisStyle));
    React.useEffect(() => {
        var _a;
        setSupportsAnalysisAnimation(!!((_a = viewport === null || viewport === void 0 ? void 0 : viewport.view) === null || _a === void 0 ? void 0 : _a.analysisStyle));
    }, [viewport]);
    React.useEffect(() => {
        const handleViewChanged = (vp) => {
            var _a;
            const hasAnalysisData = !!((_a = vp === null || vp === void 0 ? void 0 : vp.view) === null || _a === void 0 ? void 0 : _a.analysisStyle);
            if (hasAnalysisData !== supportsAnalysisAnimation)
                setSupportsAnalysisAnimation(hasAnalysisData);
        };
        return viewport === null || viewport === void 0 ? void 0 : viewport.onChangeView.addListener(handleViewChanged);
    }, [supportsAnalysisAnimation, viewport]);
    React.useEffect(() => {
        const handleDisplayStyleChange = (vp) => {
            var _a;
            const hasAnalysisData = !!((_a = vp === null || vp === void 0 ? void 0 : vp.view) === null || _a === void 0 ? void 0 : _a.analysisStyle);
            if (hasAnalysisData !== supportsAnalysisAnimation)
                setSupportsAnalysisAnimation(hasAnalysisData);
        };
        return viewport === null || viewport === void 0 ? void 0 : viewport.onDisplayStyleChanged.addListener(handleDisplayStyleChange);
    }, [viewport, supportsAnalysisAnimation]);
    return supportsAnalysisAnimation;
}
/** Hook that returns either a AnalysisAnimationTimelineDataProvider or undefined based on if the supplied viewport contains analysis data.
 * @public
 **/
// istanbul ignore next
export 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 AnalysisAnimationTimelineDataProvider(vp.view, vp);
            if (newProvider === null || newProvider === void 0 ? void 0 : 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;
}
//# sourceMappingURL=useAnalysisAnimationDataProvider.js.map