"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.SnapModeField = void 0;
const React = require("react");
const react_redux_1 = require("react-redux");
const core_frontend_1 = require("@itwin/core-frontend");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const state_1 = require("../configurableui/state");
const UiFramework_1 = require("../UiFramework");
const Indicator_1 = require("./Indicator");
/**
 * Snap Mode Field React component. This component is designed to be specified in a status bar definition. It will
 * display the active snap mode that AccuSnap will use and allow the user to select a new snap mode.
 */
class SnapModeFieldComponent extends React.Component {
    constructor(props) {
        super(props);
        this._snapModeFieldArray = [
            { label: UiFramework_1.UiFramework.translate("snapModeField.keypoint"), value: core_frontend_1.SnapMode.NearestKeypoint, iconName: "snaps" },
            { label: UiFramework_1.UiFramework.translate("snapModeField.intersection"), value: core_frontend_1.SnapMode.Intersection, iconName: "snaps-intersection" },
            { label: UiFramework_1.UiFramework.translate("snapModeField.center"), value: core_frontend_1.SnapMode.Center, iconName: "snaps-center" },
            { label: UiFramework_1.UiFramework.translate("snapModeField.nearest"), value: core_frontend_1.SnapMode.Nearest, iconName: "snaps-nearest" },
            { label: UiFramework_1.UiFramework.translate("snapModeField.origin"), value: core_frontend_1.SnapMode.Origin, iconName: "snaps-origin" },
            { label: UiFramework_1.UiFramework.translate("snapModeField.midpoint"), value: core_frontend_1.SnapMode.MidPoint, iconName: "snaps-midpoint" },
            { label: UiFramework_1.UiFramework.translate("snapModeField.bisector"), value: core_frontend_1.SnapMode.Bisector, iconName: "snaps-bisector" },
        ];
        this._title = UiFramework_1.UiFramework.translate("snapModeField.snapMode");
        this.state = {
            target: null,
        };
        /** Called when user clicks on a Snap Mode entry in the pop-up window. */
        this._handleSnapModeFieldClick = (snapModeField) => {
            this.props.setSnapMode(snapModeField);
        };
    }
    /** Return icon class name for a specific snapMode. */
    getSnapModeIconNameFromMode(snapMode) {
        for (const mode of this._snapModeFieldArray) {
            if (mode.value === snapMode)
                return mode.iconName;
        }
        /* istanbul ignore else */
        if (snapMode > 0)
            return "snaps-multione";
        /* istanbul ignore next */
        return "placeholder";
    }
    /** Standard React render method. */
    render() {
        return (React.createElement(React.Fragment, null,
            React.createElement(Indicator_1.Indicator // eslint-disable-line deprecation/deprecation
            , { iconName: `icon-${this.getSnapModeIconNameFromMode(this.props.snapMode)}`, toolTip: this._title, label: this._title, isLabelVisible: true, dialog: React.createElement(appui_layout_react_1.SnapModePanel, { title: this._title }, this.getSnapEntries()) })));
    }
    /** Return array of SnapRow elements, one for each support snap mode. This array will populate the pop-up used
      * to select a SnapMode.
      */
    getSnapEntries() {
        return this._snapModeFieldArray.map((item, index) => {
            return (React.createElement(appui_layout_react_1.Snap, { key: `SM_${index}`, onClick: () => this._handleSnapModeFieldClick(item.value), isActive: (this.props.snapMode & item.value) === item.value, icon: React.createElement("i", { className: `icon icon-${item.iconName}` }) }, item.label));
        });
    }
}
// Used by Redux to map dispatch functions to props entry. This requires SnapModeFieldProps interface above to include a setSnapMode entry */
const mapDispatch = {
    setSnapMode: state_1.ConfigurableUiActions.setSnapMode,
};
/** Function used by Redux to map state data in Redux store to props that are used to render this component. */
function mapStateToProps(state) {
    const frameworkState = state[UiFramework_1.UiFramework.frameworkStateKey]; // since app sets up key, don't hard-code name
    /* istanbul ignore next */
    if (!frameworkState)
        return undefined;
    return { snapMode: frameworkState.configurableUiState.snapMode };
}
// we declare the variable and export that rather than using export default.
/**
 * Snap Mode Field React component. This component is designed to be specified in a status bar definition. It will
 * display the active snap mode that AccuSnap will use and allow the user to select a new snap mode.
 * This Field React component is Redux connected.
 * @public
 */ // eslint-disable-next-line @typescript-eslint/naming-convention
exports.SnapModeField = (0, react_redux_1.connect)(mapStateToProps, mapDispatch)(SnapModeFieldComponent);
//# sourceMappingURL=SnapMode.js.map