"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 StatusBar
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TileLoadingIndicator = void 0;
require("./TileLoadingIndicator.scss");
const classnames_1 = require("classnames");
const React = require("react");
const core_bentley_1 = require("@itwin/core-bentley");
const core_frontend_1 = require("@itwin/core-frontend");
const itwinui_react_1 = require("@itwin/itwinui-react");
const UiFramework_1 = require("../../UiFramework");
/** TileLoadingIndicator React component
 * @public
 */
class TileLoadingIndicator extends React.PureComponent {
    // eslint-disable-next-line deprecation/deprecation
    constructor(props) {
        super(props);
        this._refreshState = (vp) => {
            const requested = vp.numRequestedTiles;
            const ready = vp.numReadyTiles;
            const total = ready + requested;
            const pctComplete = total > 0 ? (ready / total) * 100 : 100;
            let enabled = this.state.enabled;
            let finished = this.state.finished;
            if (!enabled && total !== 0 && pctComplete !== 100)
                enabled = true;
            if (enabled && (total === 0 || pctComplete === 100))
                enabled = false;
            if (pctComplete === 100 && !finished) {
                finished = true;
                core_bentley_1.Logger.logTrace(UiFramework_1.UiFramework.loggerCategory(this), `Tiles Finished Loading`);
                core_bentley_1.Logger.logTrace(UiFramework_1.UiFramework.loggerCategory(this), `Tiles Load Report (tiles finished / tiles requested):  ${ready} / ${total}`);
            }
            if (pctComplete !== 100 && finished)
                finished = false;
            this.setState({
                label: `${ready} / ${total}`,
                progress: pctComplete,
                enabled,
                finished,
            });
        };
        this._handleRender = (vp) => {
            // set progress animation before the next repaint.
            setTimeout(() => this._refreshState(vp));
        };
        this._onViewOpen = (vp) => {
            this._removeOnRenderListener && this._removeOnRenderListener();
            this._removeOnRenderListener = vp.onRender.addListener(this._handleRender);
        };
        this.state = { label: "", progress: 0, enabled: false, finished: true };
    }
    componentDidMount() {
        if (!core_frontend_1.IModelApp.viewManager)
            return;
        // get selected viewport
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        // if view exists bind update routine to onRender loop, otherwise do so once the onViewOpen event runs
        if (vp) {
            this._onViewOpen(vp);
        }
        else {
            this._removeViewOpenListener =
                core_frontend_1.IModelApp.viewManager.onViewOpen.addListener(this._onViewOpen);
        }
    }
    componentWillUnmount() {
        if (!core_frontend_1.IModelApp.viewManager)
            return;
        if (this._removeViewOpenListener)
            this._removeViewOpenListener();
        if (this._removeOnRenderListener)
            this._removeOnRenderListener();
    }
    /** Renders TileLoadingIndicator */
    render() {
        const classes = (0, classnames_1.default)("uifw-tile-loading-bar", this.state.enabled && "uifw-tile-loading-bar-visible", this.props.className);
        return (React.createElement("div", { className: classes, style: this.props.style },
            React.createElement("span", null, this.state.label),
            React.createElement(itwinui_react_1.ProgressLinear, { className: "uifw-progress-indicator", value: this.state.progress })));
    }
}
exports.TileLoadingIndicator = TileLoadingIndicator;
//# sourceMappingURL=TileLoadingIndicator.js.map