"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.useSolarDataProvider = void 0;
const React = require("react");
const core_frontend_1 = require("@itwin/core-frontend");
const SolarTimelineDataProvider_1 = require("../timeline/SolarTimelineDataProvider");
/** @internal */
function useSupportsShadowDisplay(viewport) {
    const [supportsShadows, setSupportsShadows] = React.useState(!!viewport?.displayStyle?.wantShadows &&
        !!core_frontend_1.IModelApp.renderSystem.options.displaySolarShadows);
    React.useEffect(() => {
        setSupportsShadows(!!viewport?.displayStyle?.wantShadows &&
            !!core_frontend_1.IModelApp.renderSystem.options.displaySolarShadows);
    }, [viewport]);
    React.useEffect(() => {
        const handleViewChanged = (vp) => {
            setSupportsShadows(!!vp?.displayStyle?.wantShadows &&
                !!core_frontend_1.IModelApp.renderSystem.options.displaySolarShadows);
        };
        return viewport?.onChangeView.addListener(handleViewChanged);
    }, [viewport]);
    React.useEffect(() => {
        const handleViewChanged = (vp) => {
            const wantShadows = !!vp.displayStyle?.wantShadows &&
                !!core_frontend_1.IModelApp.renderSystem.options.displaySolarShadows;
            if (wantShadows !== supportsShadows)
                setSupportsShadows(wantShadows);
        };
        return viewport?.onChangeView.addListener(handleViewChanged);
    }, [viewport, supportsShadows]);
    React.useEffect(() => {
        const handleDisplayStyleChange = (vp) => {
            const wantShadows = !!vp.displayStyle?.wantShadows &&
                !!core_frontend_1.IModelApp.renderSystem.options.displaySolarShadows;
            if (wantShadows !== supportsShadows)
                setSupportsShadows(wantShadows);
        };
        return viewport?.onDisplayStyleChanged.addListener(handleDisplayStyleChange);
    }, [viewport, supportsShadows]);
    return supportsShadows;
}
/** Hook that returns either a SolarTimelineDataProvider or undefined based on if the supplied viewport's display style is set to display shadows.
 * @beta
 **/
function useSolarDataProvider(viewport) {
    const supportsShadowDisplay = useSupportsShadowDisplay(viewport);
    const [solarDataProvider, setSolarDataProvider] = React.useState(() => {
        return supportsShadowDisplay && viewport
            ? new SolarTimelineDataProvider_1.SolarTimelineDataProvider(viewport.view, viewport)
            : undefined;
    });
    React.useEffect(() => {
        const newSolarDataProvider = supportsShadowDisplay && viewport
            ? new SolarTimelineDataProvider_1.SolarTimelineDataProvider(viewport.view, viewport)
            : undefined;
        setSolarDataProvider(newSolarDataProvider);
    }, [supportsShadowDisplay, viewport]);
    return solarDataProvider;
}
exports.useSolarDataProvider = useSolarDataProvider;
//# sourceMappingURL=useSolarDataProvider.js.map