/*---------------------------------------------------------------------------------------------
 * 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 { ScheduleAnimationTimelineDataProvider } from "../timeline/ScheduleAnimationProvider.js";
/** @internal */
function useSupportsScheduleScript(viewport) {
    const [supportsScheduleScript, setSupportsScheduleScript] = React.useState(!!viewport?.view?.scheduleScript);
    React.useEffect(() => {
        setSupportsScheduleScript(!!viewport?.view?.scheduleScript);
    }, [viewport]);
    React.useEffect(() => {
        return viewport?.onChangeView.addListener((vp) => {
            const hasScheduleScript = !!vp?.view?.scheduleScript;
            if (hasScheduleScript !== supportsScheduleScript)
                setSupportsScheduleScript(hasScheduleScript);
        });
    }, [supportsScheduleScript, viewport]);
    React.useEffect(() => {
        return viewport?.onDisplayStyleChanged.addListener((vp) => {
            const hasScheduleScript = !!vp?.view?.scheduleScript;
            if (hasScheduleScript !== supportsScheduleScript)
                setSupportsScheduleScript(hasScheduleScript);
        });
    }, [viewport, supportsScheduleScript]);
    return supportsScheduleScript;
}
/** Hook that returns either a ScheduleAnimationTimelineDataProvider or undefined based on if the supplied viewport contains schedule script.
 * @public
 **/
export function useScheduleAnimationDataProvider(viewport) {
    const supportsScheduleScript = useSupportsScheduleScript(viewport);
    const [dataProvider, setDataProvider] = React.useState();
    React.useEffect(() => {
        async function fetchNewDataProvider(vp) {
            let newProvider = new ScheduleAnimationTimelineDataProvider(vp.view, vp);
            if (newProvider?.supportsTimelineAnimation) {
                const dataLoaded = await newProvider.loadTimelineData();
                if (!dataLoaded)
                    newProvider = undefined;
            }
            setDataProvider(newProvider);
        }
        if (supportsScheduleScript && viewport)
            void fetchNewDataProvider(viewport);
        else
            setDataProvider(undefined);
    }, [supportsScheduleScript, viewport]);
    return dataProvider;
}
//# sourceMappingURL=useScheduleAnimationDataProvider.js.map