/*---------------------------------------------------------------------------------------------
 * 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 { SnapMode } from "@itwin/core-frontend";
import * as React from "react";
import { UiFramework } from "../UiFramework.js";
import { SnapModePanel } from "../layout/footer/snap-mode/Panel.js";
import { Snap } from "../layout/footer/snap-mode/Snap.js";
import { useTranslation } from "../hooks/useTranslation.js";
import { useReduxFrameworkState } from "../uistate/useReduxFrameworkState.js";
import { Button, Icon } from "@itwin/itwinui-react";
import { StatusBarPopover } from "../statusbar/popup/StatusBarPopover.js";
import { SvgSnapsBisector } from "../icons/snaps/SvgSnapsBisector.js";
import { SvgSnapsCenter } from "../icons/snaps/SvgSnapsCenter.js";
import { SvgSnapsIntersection } from "../icons/snaps/SvgSnapsIntersection.js";
import { SvgSnapsMidpoint } from "../icons/snaps/SvgSnapsMidpoint.js";
import { SvgSnapsNearest } from "../icons/snaps/SvgSnapsNearest.js";
import { SvgSnapsOrigin } from "../icons/snaps/SvgSnapsOrigin.js";
import { SvgSnaps } from "../icons/snaps/SvgSnaps.js";
import { SvgSnapsPerpendicular } from "../icons/snaps/SvgSnapsPerpendicular.js";
import { SvgSnapsTangent } from "../icons/snaps/SvgSnapsTangent.js";
// Field entry of all possible snap modes.
const allSnapModeFieldEntries = [
    {
        labelKey: "snapModeField.keypoint",
        value: SnapMode.NearestKeypoint,
        icon: React.createElement(SvgSnaps, null),
    },
    {
        labelKey: "snapModeField.intersection",
        value: SnapMode.Intersection,
        icon: React.createElement(SvgSnapsIntersection, null),
    },
    {
        labelKey: "snapModeField.center",
        value: SnapMode.Center,
        icon: React.createElement(SvgSnapsCenter, null),
    },
    {
        labelKey: "snapModeField.nearest",
        value: SnapMode.Nearest,
        icon: React.createElement(SvgSnapsNearest, null),
    },
    {
        labelKey: "snapModeField.origin",
        value: SnapMode.Origin,
        icon: React.createElement(SvgSnapsOrigin, null),
    },
    {
        labelKey: "snapModeField.midpoint",
        value: SnapMode.MidPoint,
        icon: React.createElement(SvgSnapsMidpoint, null),
    },
    {
        labelKey: "snapModeField.bisector",
        value: SnapMode.Bisector,
        icon: React.createElement(SvgSnapsBisector, null),
    },
    // @ts-ignore Introduced in @itwin/core-frontend@5.0.0
    ...(SnapMode.PerpendicularPoint === undefined
        ? []
        : [
            {
                labelKey: "snapModeField.perpendicularPoint",
                // @ts-ignore Introduced in @itwin/core-frontend@5.0.0
                value: SnapMode.PerpendicularPoint,
                icon: React.createElement(SvgSnapsPerpendicular, null),
            },
        ]),
    // @ts-ignore Introduced in @itwin/core-frontend@5.0.0
    ...(SnapMode.TangentPoint === undefined
        ? []
        : [
            {
                labelKey: "snapModeField.tangentPoint",
                // @ts-ignore Introduced in @itwin/core-frontend@5.0.0
                value: SnapMode.TangentPoint,
                icon: React.createElement(SvgSnapsTangent, null),
            },
        ]),
];
/** `SnapModeField` component designed to be specified in a status bar. It will
 * display the active snap mode that AccuSnap will use and allow the user to select a new snap mode.
 * @public
 */
export function SnapModeField(props) {
    const { translate } = useTranslation();
    const snapModeEntries = props.availableSnapModes
        ? allSnapModeFieldEntries.filter((entry) => {
            return (props.availableSnapModes &&
                props.availableSnapModes.some((availableSnap) => availableSnap === entry.value));
        })
        : allSnapModeFieldEntries;
    const reduxSnapMode = useReduxFrameworkState(
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    (state) => state?.configurableUiState.snapMode);
    const snapMode = props.snapMode ?? reduxSnapMode ?? SnapMode.NearestKeypoint;
    // Get all the modes present in the bitmask.
    const enabledSnaps = allSnapModeFieldEntries.filter((entry) => {
        return (entry.value & snapMode) === entry.value;
    });
    const title = translate("snapModeField.snapMode");
    return (React.createElement(StatusBarPopover, { content: React.createElement(SnapModePanel, { title: title }, snapModeEntries.map((entry, index) => (React.createElement(Snap, { key: `SM_${index}`, onClick: () => {
                if (props.onChange) {
                    props.onChange(entry.value);
                    return;
                }
                // eslint-disable-next-line @typescript-eslint/no-deprecated
                UiFramework.setAccudrawSnapMode(entry.value);
            }, isActive: (snapMode & entry.value) === entry.value, icon: React.createElement(Icon, null, entry.icon) }, translate(entry.labelKey))))) },
        React.createElement(Button, { styleType: "borderless", title: title, startIcon: enabledSnaps.length === 1 ? enabledSnaps[0].icon : React.createElement(SvgSnaps, null) },
            title,
            React.createElement(StatusBarPopover.ExpandIndicator, null))));
}
//# sourceMappingURL=SnapMode.js.map