/*---------------------------------------------------------------------------------------------
 * 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 { IModelApp } from "@itwin/core-frontend";
import { SolarTimelineDataProvider } from "../timeline/SolarTimelineDataProvider";
/** @internal */
// istanbul ignore next
function useSupportsShadowDisplay(viewport) {
    var _a;
    const [supportsShadows, setSupportsShadows] = React.useState(!!((_a = viewport === null || viewport === void 0 ? void 0 : viewport.displayStyle) === null || _a === void 0 ? void 0 : _a.wantShadows) &&
        !!IModelApp.renderSystem.options.displaySolarShadows);
    React.useEffect(() => {
        var _a;
        setSupportsShadows(!!((_a = viewport === null || viewport === void 0 ? void 0 : viewport.displayStyle) === null || _a === void 0 ? void 0 : _a.wantShadows) &&
            !!IModelApp.renderSystem.options.displaySolarShadows);
    }, [viewport]);
    React.useEffect(() => {
        const handleViewChanged = (vp) => {
            var _a;
            setSupportsShadows(!!((_a = vp === null || vp === void 0 ? void 0 : vp.displayStyle) === null || _a === void 0 ? void 0 : _a.wantShadows) &&
                !!IModelApp.renderSystem.options.displaySolarShadows);
        };
        return viewport === null || viewport === void 0 ? void 0 : viewport.onChangeView.addListener(handleViewChanged);
    }, [viewport]);
    React.useEffect(() => {
        const handleViewChanged = (vp) => {
            var _a;
            const wantShadows = !!((_a = vp.displayStyle) === null || _a === void 0 ? void 0 : _a.wantShadows) &&
                !!IModelApp.renderSystem.options.displaySolarShadows;
            if (wantShadows !== supportsShadows)
                setSupportsShadows(wantShadows);
        };
        return viewport === null || viewport === void 0 ? void 0 : viewport.onChangeView.addListener(handleViewChanged);
    }, [viewport, supportsShadows]);
    React.useEffect(() => {
        const handleDisplayStyleChange = (vp) => {
            var _a;
            const wantShadows = !!((_a = vp.displayStyle) === null || _a === void 0 ? void 0 : _a.wantShadows) &&
                !!IModelApp.renderSystem.options.displaySolarShadows;
            if (wantShadows !== supportsShadows)
                setSupportsShadows(wantShadows);
        };
        return viewport === null || viewport === void 0 ? void 0 : 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
 **/
// istanbul ignore next
export function useSolarDataProvider(viewport) {
    const supportsShadowDisplay = useSupportsShadowDisplay(viewport);
    const [solarDataProvider, setSolarDataProvider] = React.useState(() => {
        return supportsShadowDisplay && viewport
            ? new SolarTimelineDataProvider(viewport.view, viewport)
            : undefined;
    });
    React.useEffect(() => {
        const newSolarDataProvider = supportsShadowDisplay && viewport
            ? new SolarTimelineDataProvider(viewport.view, viewport)
            : undefined;
        setSolarDataProvider(newSolarDataProvider);
    }, [supportsShadowDisplay, viewport]);
    return solarDataProvider;
}
//# sourceMappingURL=useSolarDataProvider.js.map