/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module AccuDraw
 */
import { BeUiEvent } from "@itwin/core-bentley";
import { AccuDraw, CompassMode, IModelApp, ItemField, NotifyMessageDetails, OutputMessagePriority, QuantityType, RotationMode, } from "@itwin/core-frontend";
import { ConditionalBooleanValue } from "@itwin/appui-abstract";
import { UiFramework } from "../UiFramework.js";
import { SyncUiEventDispatcher } from "../syncui/SyncUiEventDispatcher.js";
import { UiStateStorageStatus } from "../uistate/UiStateStorage.js";
import { SyncUiEventId } from "../syncui/UiSyncEvent.js";
const compassModeToKeyMap = new Map([
    [CompassMode.Polar, "polar"],
    [CompassMode.Rectangular, "rectangular"],
]);
const rotationModeToKeyMap = new Map([
    [RotationMode.Top, "top"],
    [RotationMode.Front, "front"],
    [RotationMode.Side, "side"],
    [RotationMode.View, "view"],
    [RotationMode.ACS, "ACS"],
    [RotationMode.Context, "context"],
]);
/** AccuDraw Set Field Focus event
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
// eslint-disable-next-line @typescript-eslint/no-deprecated
export class AccuDrawSetFieldFocusEvent extends BeUiEvent {
}
/** AccuDraw Set Field Value to Ui event
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
// eslint-disable-next-line @typescript-eslint/no-deprecated
export class AccuDrawSetFieldValueToUiEvent extends BeUiEvent {
}
/** AccuDraw Set Field Value from Ui event
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
// eslint-disable-next-line @typescript-eslint/no-deprecated
export class AccuDrawSetFieldValueFromUiEvent extends BeUiEvent {
}
/** AccuDraw Set Field Lock event
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
// eslint-disable-next-line @typescript-eslint/no-deprecated
export class AccuDrawSetFieldLockEvent extends BeUiEvent {
}
/** AccuDraw Set Compass Mode event
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
// eslint-disable-next-line @typescript-eslint/no-deprecated
export class AccuDrawSetCompassModeEvent extends BeUiEvent {
}
/** AccuDraw Grab Input Focus event
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
export class AccuDrawGrabInputFocusEvent extends BeUiEvent {
}
/** AccuDraw Ui Settings Changed event
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
export class AccuDrawUiSettingsChangedEvent extends BeUiEvent {
}
/** Subclass of `AccuDraw` in `@itwin/core-frontend` to be used to initialize `IModelApp`.
 *
 * This implementation will generate the required events for the AppUI provided UI to update correctly.
 *
 * ```ts
 * await IModelApp.startup({
 *   accuDraw: new FrameworkAccuDraw()
 * });
 * ```
 * @public
 */
export class FrameworkAccuDraw extends AccuDraw {
    /** Determines if notifications should be displayed for AccuDraw changes */
    static get displayNotifications() {
        return FrameworkAccuDraw._displayNotifications;
    }
    static set displayNotifications(v) {
        FrameworkAccuDraw._displayNotifications = v;
        void UiFramework.getUiStateStorage().saveSetting(this._settingsNamespace, this._notificationsKey, v);
    }
    /* @internal */
    async loadUserSettings(storage) {
        const result = await storage.getSetting(FrameworkAccuDraw._settingsNamespace, FrameworkAccuDraw._notificationsKey);
        if (result.status === UiStateStorageStatus.Success)
            FrameworkAccuDraw._displayNotifications = result.setting;
    }
    /** AccuDraw User Interface settings */
    static get uiStateStorage() {
        return FrameworkAccuDraw._uiSettings;
    }
    static set uiStateStorage(v) {
        FrameworkAccuDraw._uiSettings = v;
        FrameworkAccuDraw.onAccuDrawUiSettingsChangedEvent.emit({});
    }
    constructor() {
        super();
        this.providerId = "FrameworkAccuDraw";
        this.handleSetFieldValueFromUiEvent = async (args // eslint-disable-line @typescript-eslint/no-deprecated
        ) => {
            return this.processFieldInput(args.field, args.stringValue, false);
        };
        FrameworkAccuDraw.onAccuDrawSetFieldValueFromUiEvent.addListener(this.handleSetFieldValueFromUiEvent);
        UiFramework.registerUserSettingsProvider(this);
    }
    onCompassModeChange() {
        FrameworkAccuDraw.onAccuDrawSetCompassModeEvent.emit({
            mode: this.compassMode,
        });
        SyncUiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.AccuDrawCompassModeChanged);
        this.outputCompassModeMessage();
    }
    onRotationModeChange() {
        SyncUiEventDispatcher.dispatchSyncUiEvent(SyncUiEventId.AccuDrawRotationChanged);
        this.outputRotationMessage();
    }
    onFieldLockChange(index) {
        FrameworkAccuDraw.onAccuDrawSetFieldLockEvent.emit({
            field: index,
            lock: this.getFieldLock(index),
        });
    }
    onFieldValueChange(index) {
        const value = this.getValueByIndex(index);
        const formattedValue = FrameworkAccuDraw.getFieldDisplayValue(index);
        FrameworkAccuDraw.onAccuDrawSetFieldValueToUiEvent.emit({
            field: index,
            value,
            formattedValue,
        });
    }
    fieldValuesChanged() {
        // Only change the value when in Dynamic mode. Other mode are "DontUpdate" when the input is locked and "Partial" when the user is typing.
        this.onFieldValueChange(ItemField.X_Item);
        this.onFieldValueChange(ItemField.Y_Item);
        this.onFieldValueChange(ItemField.Z_Item);
        this.onFieldValueChange(ItemField.ANGLE_Item);
        this.onFieldValueChange(ItemField.DIST_Item);
    }
    setFocusItem(index) {
        FrameworkAccuDraw.onAccuDrawSetFieldFocusEvent.emit({ field: index });
    }
    /** Implemented by sub-classes to update ui fields to show current deltas or coordinates when inactive.
     * Should also choose active x or y input field in rectangular mode based on cursor position when
     * axis isn't locked to support "smart lock".
     */
    onMotion(_ev) {
        if (!this.isEnabled || this.isDeactivated || UiFramework.isContextMenuOpen)
            return;
        this.fieldValuesChanged();
        if (!this.dontMoveFocus && this.compassMode === CompassMode.Rectangular) {
            // Changes the focus between X and Y axis depending on the cursor location, in rectangular mode.
            // Example : this.newFocus is Y when the cursor is closer to the Y axis.
            this.setFocusItem(this.newFocus);
        }
    }
    /** Determine if the AccuDraw UI has focus. */
    get hasInputFocus() {
        let hasFocus = false;
        const el = document.querySelector("div.uifw-accudraw-field-container");
        if (el)
            hasFocus = el.contains(document.activeElement);
        return hasFocus;
    }
    /** Implement this method to set focus to the AccuDraw UI. */
    grabInputFocus() {
        FrameworkAccuDraw.onAccuDrawGrabInputFocusEvent.emit({});
    }
    /** Gets the display value for an AccuDraw field */
    static getFieldDisplayValue(index) {
        const value = IModelApp.accuDraw.getValueByIndex(index);
        let formattedValue = value.toString();
        const formatterSpec = IModelApp.quantityFormatter.findFormatterSpecByQuantityType(ItemField.ANGLE_Item === index
            ? QuantityType.Angle
            : QuantityType.Length);
        if (formatterSpec)
            formattedValue = IModelApp.quantityFormatter.formatQuantity(value, formatterSpec);
        return formattedValue;
    }
    /** AccuDraw Set Field Value from Ui. */
    static setFieldValueFromUi(field, stringValue) {
        FrameworkAccuDraw.onAccuDrawSetFieldValueFromUiEvent.emit({
            field,
            stringValue,
        });
    }
    outputInfoMessage(message) {
        if (FrameworkAccuDraw.displayNotifications)
            IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, message));
    }
    outputCompassModeMessage() {
        if (FrameworkAccuDraw.displayNotifications) {
            const modeKey = compassModeToKeyMap.get(this.compassMode) ?? "polar";
            const modeString = UiFramework.translate(`accuDraw.compassMode.${modeKey}`);
            const modeMessage = UiFramework.translate("accuDraw.compassModeSet", {
                modeString,
            });
            this.outputInfoMessage(modeMessage);
        }
    }
    outputRotationMessage() {
        if (FrameworkAccuDraw.displayNotifications) {
            const rotationKey = rotationModeToKeyMap.get(this.rotationMode) ?? "top";
            const rotationString = UiFramework.translate(`accuDraw.rotation.${rotationKey}`);
            const rotationMessage = UiFramework.translate("accuDraw.rotationSet", {
                rotationString,
            });
            this.outputInfoMessage(rotationMessage);
        }
    }
}
FrameworkAccuDraw._displayNotifications = false;
FrameworkAccuDraw._settingsNamespace = "AppUiSettings";
FrameworkAccuDraw._notificationsKey = "AccuDrawNotifications";
/** AccuDraw Set Field Focus event. */
FrameworkAccuDraw.onAccuDrawSetFieldFocusEvent = new AccuDrawSetFieldFocusEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
/** AccuDraw Set Field Value to Ui event. */
FrameworkAccuDraw.onAccuDrawSetFieldValueToUiEvent = new AccuDrawSetFieldValueToUiEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
/** AccuDraw Set Field Value from Ui event. */
FrameworkAccuDraw.onAccuDrawSetFieldValueFromUiEvent = new AccuDrawSetFieldValueFromUiEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
/** AccuDraw Set Field Lock event. */
FrameworkAccuDraw.onAccuDrawSetFieldLockEvent = new AccuDrawSetFieldLockEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
/** AccuDraw Set Mode event. */
FrameworkAccuDraw.onAccuDrawSetCompassModeEvent = new AccuDrawSetCompassModeEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
/** AccuDraw Grab Input Focus event. */
FrameworkAccuDraw.onAccuDrawGrabInputFocusEvent = new AccuDrawGrabInputFocusEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
/** Determines if AccuDraw.rotationMode === RotationMode.Top */
FrameworkAccuDraw.isTopRotationConditional = new ConditionalBooleanValue(() => IModelApp.accuDraw.rotationMode === RotationMode.Top, [SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.Front */
FrameworkAccuDraw.isFrontRotationConditional = new ConditionalBooleanValue(() => IModelApp.accuDraw.rotationMode === RotationMode.Front, [SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.Side */
FrameworkAccuDraw.isSideRotationConditional = new ConditionalBooleanValue(() => IModelApp.accuDraw.rotationMode === RotationMode.Side, [SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.View */
FrameworkAccuDraw.isViewRotationConditional = new ConditionalBooleanValue(() => IModelApp.accuDraw.rotationMode === RotationMode.View, [SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.ACS */
FrameworkAccuDraw.isACSRotationConditional = new ConditionalBooleanValue(() => IModelApp.accuDraw.rotationMode === RotationMode.ACS, [SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.Context */
FrameworkAccuDraw.isContextRotationConditional = new ConditionalBooleanValue(() => IModelApp.accuDraw.rotationMode === RotationMode.Context, [SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.compassMode === CompassMode.Polar */
FrameworkAccuDraw.isPolarModeConditional = new ConditionalBooleanValue(() => IModelApp.accuDraw.compassMode === CompassMode.Polar, [SyncUiEventId.AccuDrawCompassModeChanged]);
/** Determines if AccuDraw.compassMode === CompassMode.Rectangular */
FrameworkAccuDraw.isRectangularModeConditional = new ConditionalBooleanValue(() => IModelApp.accuDraw.compassMode === CompassMode.Rectangular, [SyncUiEventId.AccuDrawCompassModeChanged]);
/** AccuDraw Grab Input Focus event. */
FrameworkAccuDraw.onAccuDrawUiSettingsChangedEvent = new AccuDrawUiSettingsChangedEvent(); // eslint-disable-line @typescript-eslint/no-deprecated
//# sourceMappingURL=FrameworkAccuDraw.js.map