/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module NavigationAids
 */
import "./SheetNavigationAid.scss";
import classnames from "classnames";
import * as React from "react";
import { IModelApp } from "@itwin/core-frontend";
import { ViewportComponentEvents } from "@itwin/imodel-components-react";
import { UiFramework } from "../UiFramework.js";
import { ViewUtilities } from "../utils/ViewUtilities.js";
import { NavigationAidControl } from "./NavigationAidControl.js";
import { CardContainer, SheetsModalFrontstage, } from "./SheetsModalFrontstage.js";
import { IconButton, ProgressRadial } from "@itwin/itwinui-react";
import { SvgChevronLeft, SvgChevronRight } from "@itwin/itwinui-icons-react";
/** A Sheet Navigation Aid control.
 * @public
 * @deprecated in 4.16.0. Use {@link SheetNavigationAid} component instead.
 */
// eslint-disable-next-line @typescript-eslint/no-deprecated
export class SheetNavigationAidControl extends NavigationAidControl {
    static navigationAidId = "SheetNavigationAid";
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    constructor(info, options) {
        // eslint-disable-next-line @typescript-eslint/no-deprecated
        super(info, options);
        this.reactNode = React.createElement(SheetNavigationAid, { iModelConnection: options.imodel });
    }
    getSize() {
        return "96px";
    }
}
/** A Sheet Navigation Aid.
 * @public
 */
export class SheetNavigationAid extends React.Component {
    _isMounted = false;
    state = {
        index: 0,
        sheetData: [],
    };
    _viewport;
    constructor(props) {
        super(props);
        this._viewport = IModelApp.viewManager.selectedView;
    }
    /** Adds listeners when components mounts */
    async componentDidMount() {
        this._isMounted = true;
        CardContainer.onCardSelectedEvent.addListener(this._handleCardSelected);
        ViewportComponentEvents.onViewIdChangedEvent.addListener(this._handleViewIdChanged);
        IModelApp.viewManager.onSelectedViewportChanged.addListener(this._handleSelectedViewportChanged);
        const stateData = await this._setupSheets();
        if (this._isMounted)
            this.setState(stateData);
    }
    /** Removes listeners when component will unmount */
    componentWillUnmount() {
        this._isMounted = false;
        CardContainer.onCardSelectedEvent.removeListener(this._handleCardSelected);
        ViewportComponentEvents.onViewIdChangedEvent.removeListener(this._handleViewIdChanged);
        IModelApp.viewManager.onSelectedViewportChanged.removeListener(this._handleSelectedViewportChanged);
    }
    /** Queries for sheet info and sets as sheetData */
    async _setupSheets() {
        const stateData = {
            index: 0,
            sheetData: [],
        };
        if (!this.props.iModelConnection ||
            !this.props.iModelConnection.views.getViewList)
            return stateData;
        let viewId = "";
        if (this._viewport) {
            viewId = this._viewport.view.id.toString();
        }
        const sheets = await this.props.iModelConnection.views.getViewList({
            from: "BisCore.SheetViewDefinition",
        });
        sheets.forEach((viewSpec, index) => {
            stateData.sheetData.push({ name: viewSpec.name, viewId: viewSpec.id });
            if (viewSpec.id === viewId)
                stateData.index = index;
        });
        return stateData;
    }
    render() {
        const name = this.state.sheetData.length > 0
            ? this.state.sheetData[this.state.index].name
            : "";
        const leftIndex = this.state.index === 0
            ? this.state.sheetData.length - 1
            : this.state.index - 1;
        const rightIndex = this.state.index >= this.state.sheetData.length - 1
            ? 0
            : this.state.index + 1;
        const leftTitle = this.state.sheetData[leftIndex]
            ? this.state.sheetData[leftIndex].name
            : undefined;
        const rightTitle = this.state.sheetData[rightIndex]
            ? this.state.sheetData[rightIndex].name
            : undefined;
        let content;
        if (this.state.sheetData.length > 0) {
            content = (React.createElement(React.Fragment, null,
                React.createElement("div", { className: "sheet-title" }, UiFramework.translate("general.sheet")),
                React.createElement("div", { className: "sheet-name", title: name, onClick: this._handleOnClickSheetName, role: "button", tabIndex: -1 }, name),
                React.createElement("div", { className: "sheet-container" },
                    React.createElement(IconButton, { size: "small", styleType: "borderless", onClick: this._handleOnClickLeftArrow, tabIndex: -1, label: leftTitle },
                        React.createElement(SvgChevronLeft, null)),
                    React.createElement("div", null,
                        this.state.index + 1,
                        " ",
                        UiFramework.translate("general.of"),
                        this.state.sheetData.length),
                    React.createElement(IconButton, { size: "small", styleType: "borderless", onClick: this._handleOnClickRightArrow, role: "button", tabIndex: -1, label: rightTitle },
                        React.createElement(SvgChevronRight, null)))));
        }
        else {
            content = React.createElement(ProgressRadial, { size: "x-small", indeterminate: true });
        }
        return (React.createElement("div", { className: classnames("uifw-sheet-navigation", this.props.className), style: this.props.style }, content));
    }
    /** Sets index of newly selected card */
    _handleCardSelected = (event) => {
        event &&
            this.setState({
                index: event.index,
            });
    };
    /** Updates view to the next lowest index in sheetData */
    _handleOnClickLeftArrow = () => {
        this.setState((prevState) => ({
            index: prevState.index <= 0
                ? prevState.sheetData.length - 1
                : prevState.index - 1,
        }), async () => this._updateView());
    };
    /** Updates view to next highest index in sheetData */
    _handleOnClickRightArrow = () => {
        this.setState((prevState) => ({
            index: (prevState.index + 1) % prevState.sheetData.length,
        }), async () => this._updateView());
    };
    /** Handles a Viewport change & synchs the index */
    _handleSelectedViewportChanged = (args) => {
        if (args.current) {
            this._handleViewportChanged(args.current);
        }
    };
    _handleViewIdChanged = (args) => {
        if (this._viewport === args.viewport)
            this._handleViewportChanged(args.viewport);
    };
    /** Handles a Viewport change & synchs the index */
    _handleViewportChanged = (viewport) => {
        const className = ViewUtilities.getBisBaseClass(viewport.view.classFullName);
        if (ViewUtilities.isSheet(className)) {
            this._viewport = viewport;
            const viewId = this._viewport.view.id.toString();
            const index = this.state.sheetData.findIndex((sheetData) => {
                return viewId === sheetData.viewId;
            });
            if (index >= 0)
                this.setState({ index });
        }
    };
    /** Updates view to currently set sheet */
    async _updateView() {
        const viewState = await this.props.iModelConnection.views.load(this.state.sheetData[this.state.index].viewId);
        if (this._viewport)
            this._viewport.changeView(viewState);
    }
    /** Creates a new SheetsModalFrontstage */
    modalFrontstage() {
        return new SheetsModalFrontstage(this.state.sheetData, this.props.iModelConnection, this.state.index);
    }
    /** Opens a new SheetsModelFrontstage on sheetName click */
    _handleOnClickSheetName = () => {
        UiFramework.frontstages.openModalFrontstage(this.modalFrontstage());
    };
}
//# sourceMappingURL=SheetNavigationAid.js.map