/*---------------------------------------------------------------------------------------------
 * 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
 */
import * as React from "react";
import { connect } from "react-redux";
import { SnapMode } from "@itwin/core-frontend";
import { Snap, SnapModePanel } from "@itwin/appui-layout-react";
import { UiFramework } from "../UiFramework";
import { Icon } from "@itwin/core-react";
import { StatusBarLabelIndicator } from "../statusbar/LabelIndicator";
import { IconSpecUtilities } from "@itwin/appui-abstract";
import snapModeKeypoint from "@bentley/icons-generic/icons/snaps.svg";
import snapModeIntersection from "@bentley/icons-generic/icons/snaps-intersection.svg";
import snapModeCenter from "@bentley/icons-generic/icons/snaps-center.svg";
import snapModeNearest from "@bentley/icons-generic/icons/snaps-nearest.svg";
import snapModeOrigin from "@bentley/icons-generic/icons/snaps-origin.svg";
import snapModeMidpoint from "@bentley/icons-generic/icons/snaps-midpoint.svg";
import snapModeBisector from "@bentley/icons-generic/icons/snaps-bisector.svg";
function getIconFromIconName(iconName) {
    let iconSpec = IconSpecUtilities.createWebComponentIconSpec(snapModeKeypoint);
    switch (iconName) {
        case "snaps":
            iconSpec = IconSpecUtilities.createWebComponentIconSpec(snapModeKeypoint);
            break;
        case "snaps-intersection":
            iconSpec =
                IconSpecUtilities.createWebComponentIconSpec(snapModeIntersection);
            break;
        case "snaps-center":
            iconSpec = IconSpecUtilities.createWebComponentIconSpec(snapModeCenter);
            break;
        case "snaps-nearest":
            iconSpec = IconSpecUtilities.createWebComponentIconSpec(snapModeNearest);
            break;
        case "snaps-origin":
            iconSpec = IconSpecUtilities.createWebComponentIconSpec(snapModeOrigin);
            break;
        case "snaps-midpoint":
            iconSpec = IconSpecUtilities.createWebComponentIconSpec(snapModeMidpoint);
            break;
        case "snaps-bisector":
            iconSpec = IconSpecUtilities.createWebComponentIconSpec(snapModeBisector);
            break;
    }
    return iconSpec;
}
/**
 * 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.
 */
function SnapModeFieldComponent(props) {
    const snapModes = React.useMemo(() => [
        {
            label: UiFramework.translate("snapModeField.keypoint"),
            value: SnapMode.NearestKeypoint,
            iconName: "snaps",
        },
        {
            label: UiFramework.translate("snapModeField.intersection"),
            value: SnapMode.Intersection,
            iconName: "snaps-intersection",
        },
        {
            label: UiFramework.translate("snapModeField.center"),
            value: SnapMode.Center,
            iconName: "snaps-center",
        },
        {
            label: UiFramework.translate("snapModeField.nearest"),
            value: SnapMode.Nearest,
            iconName: "snaps-nearest",
        },
        {
            label: UiFramework.translate("snapModeField.origin"),
            value: SnapMode.Origin,
            iconName: "snaps-origin",
        },
        {
            label: UiFramework.translate("snapModeField.midpoint"),
            value: SnapMode.MidPoint,
            iconName: "snaps-midpoint",
        },
        {
            label: UiFramework.translate("snapModeField.bisector"),
            value: SnapMode.Bisector,
            iconName: "snaps-bisector",
        },
    ], []);
    /** Return icon class name for a specific snapMode. */
    const getSnapModeIconNameFromMode = React.useCallback(function (snapMode) {
        for (const mode of snapModes) {
            if (mode.value === snapMode)
                return mode.iconName;
        }
        /* istanbul ignore else */
        if (snapMode > 0)
            return "snaps-multione";
        /* istanbul ignore next */
        return "placeholder";
    }, [snapModes]);
    const title = UiFramework.translate("snapModeField.snapMode");
    return (React.createElement(StatusBarLabelIndicator, { iconSpec: getIconFromIconName(getSnapModeIconNameFromMode(props.snapMode)), title: title, label: title, popup: React.createElement(SnapModePanel, { title: title }, snapModes.map((item, index) => (React.createElement(Snap, { key: `SM_${index}`, onClick: () => UiFramework.setAccudrawSnapMode(item.value), isActive: (props.snapMode & item.value) === item.value, icon: React.createElement(Icon, { className: `icon`, iconSpec: getIconFromIconName(item.iconName) }) }, item.label)))) }));
}
/** 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.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
 */
export const SnapModeField = connect(mapStateToProps)(SnapModeFieldComponent);
//# sourceMappingURL=SnapMode.js.map