"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.useScheduleAnimationDataProvider = void 0;
const React = require("react");
const ScheduleAnimationProvider_1 = require("../timeline/ScheduleAnimationProvider");
/** @internal */
// istanbul ignore next
function useSupportsScheduleScript(viewport) {
    var _a;
    const [supportsScheduleScript, setSupportsScheduleScript] = React.useState(!!((_a = viewport === null || viewport === void 0 ? void 0 : viewport.view) === null || _a === void 0 ? void 0 : _a.scheduleScript));
    React.useEffect(() => {
        var _a;
        setSupportsScheduleScript(!!((_a = viewport === null || viewport === void 0 ? void 0 : viewport.view) === null || _a === void 0 ? void 0 : _a.scheduleScript));
    }, [viewport]);
    React.useEffect(() => {
        const handleViewChanged = (vp) => {
            var _a;
            const hasScheduleScript = !!((_a = vp === null || vp === void 0 ? void 0 : vp.view) === null || _a === void 0 ? void 0 : _a.scheduleScript);
            if (hasScheduleScript !== supportsScheduleScript)
                setSupportsScheduleScript(hasScheduleScript);
        };
        return viewport === null || viewport === void 0 ? void 0 : viewport.onChangeView.addListener(handleViewChanged);
    }, [supportsScheduleScript, viewport]);
    React.useEffect(() => {
        const handleDisplayStyleChange = (vp) => {
            var _a;
            const hasScheduleScript = !!((_a = vp === null || vp === void 0 ? void 0 : vp.view) === null || _a === void 0 ? void 0 : _a.scheduleScript);
            if (hasScheduleScript !== supportsScheduleScript)
                setSupportsScheduleScript(hasScheduleScript);
        };
        return viewport === null || viewport === void 0 ? void 0 : viewport.onDisplayStyleChanged.addListener(handleDisplayStyleChange);
    }, [viewport, supportsScheduleScript]);
    return supportsScheduleScript;
}
/** Hook that returns either a ScheduleAnimationTimelineDataProvider or undefined based on if the supplied viewport contains schedule script.
 * @public
 **/
// istanbul ignore next
function useScheduleAnimationDataProvider(viewport) {
    const supportsScheduleScript = useSupportsScheduleScript(viewport);
    const [scheduleAnimationTimelineDataProvider, setScheduleAnimationTimelineDataProvider,] = 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 ScheduleAnimationProvider_1.ScheduleAnimationTimelineDataProvider(vp.view, vp);
            if (newProvider === null || newProvider === void 0 ? void 0 : newProvider.supportsTimelineAnimation) {
                const dataLoaded = await newProvider.loadTimelineData();
                if (!dataLoaded)
                    newProvider = undefined;
            }
            isMountedRef.current &&
                setScheduleAnimationTimelineDataProvider(newProvider);
        }
        if (supportsScheduleScript && viewport)
            void fetchNewDataProvider(viewport);
        else
            isMountedRef.current &&
                setScheduleAnimationTimelineDataProvider(undefined);
    }, [supportsScheduleScript, viewport]);
    return scheduleAnimationTimelineDataProvider;
}
exports.useScheduleAnimationDataProvider = useScheduleAnimationDataProvider;
//# sourceMappingURL=useScheduleAnimationDataProvider.js.map