"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 AccuDraw
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.FrameworkAccuDraw = exports.AccuDrawUiSettingsChangedEvent = exports.AccuDrawGrabInputFocusEvent = exports.AccuDrawSetCompassModeEvent = exports.AccuDrawSetFieldLockEvent = exports.AccuDrawSetFieldValueFromUiEvent = exports.AccuDrawSetFieldValueToUiEvent = exports.AccuDrawSetFieldFocusEvent = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_frontend_1 = require("@itwin/core-frontend");
const appui_abstract_1 = require("@itwin/appui-abstract");
const UiFramework_1 = require("../UiFramework");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const UiStateStorage_1 = require("../uistate/UiStateStorage");
// cspell:ignore dont
const compassModeToKeyMap = new Map([
    [core_frontend_1.CompassMode.Polar, "polar"],
    [core_frontend_1.CompassMode.Rectangular, "rectangular"],
]);
const rotationModeToKeyMap = new Map([
    [core_frontend_1.RotationMode.Top, "top"],
    [core_frontend_1.RotationMode.Front, "front"],
    [core_frontend_1.RotationMode.Side, "side"],
    [core_frontend_1.RotationMode.View, "view"],
    [core_frontend_1.RotationMode.ACS, "ACS"],
    [core_frontend_1.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 deprecation/deprecation
class AccuDrawSetFieldFocusEvent extends core_bentley_1.BeUiEvent {
}
exports.AccuDrawSetFieldFocusEvent = AccuDrawSetFieldFocusEvent;
/** 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 deprecation/deprecation
class AccuDrawSetFieldValueToUiEvent extends core_bentley_1.BeUiEvent {
}
exports.AccuDrawSetFieldValueToUiEvent = AccuDrawSetFieldValueToUiEvent;
/** 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 deprecation/deprecation
class AccuDrawSetFieldValueFromUiEvent extends core_bentley_1.BeUiEvent {
}
exports.AccuDrawSetFieldValueFromUiEvent = AccuDrawSetFieldValueFromUiEvent;
/** 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 deprecation/deprecation
class AccuDrawSetFieldLockEvent extends core_bentley_1.BeUiEvent {
}
exports.AccuDrawSetFieldLockEvent = AccuDrawSetFieldLockEvent;
/** 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 deprecation/deprecation
class AccuDrawSetCompassModeEvent extends core_bentley_1.BeUiEvent {
}
exports.AccuDrawSetCompassModeEvent = AccuDrawSetCompassModeEvent;
/** AccuDraw Grab Input Focus event
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
class AccuDrawGrabInputFocusEvent extends core_bentley_1.BeUiEvent {
}
exports.AccuDrawGrabInputFocusEvent = AccuDrawGrabInputFocusEvent;
/** AccuDraw Ui Settings Changed event
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
class AccuDrawUiSettingsChangedEvent extends core_bentley_1.BeUiEvent {
}
exports.AccuDrawUiSettingsChangedEvent = AccuDrawUiSettingsChangedEvent;
/** 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
 */
class FrameworkAccuDraw extends core_frontend_1.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_1.UiFramework.getUiStateStorage().saveSetting(this._settingsNamespace, this._notificationsKey, v);
    }
    /* @internal */
    async loadUserSettings(storage) {
        const result = await storage.getSetting(FrameworkAccuDraw._settingsNamespace, FrameworkAccuDraw._notificationsKey);
        if (result.status === UiStateStorage_1.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 deprecation/deprecation
        ) => {
            return this.processFieldInput(args.field, args.stringValue, false);
        };
        FrameworkAccuDraw.onAccuDrawSetFieldValueFromUiEvent.addListener(this.handleSetFieldValueFromUiEvent);
        UiFramework_1.UiFramework.registerUserSettingsProvider(this);
    }
    onCompassModeChange() {
        FrameworkAccuDraw.onAccuDrawSetCompassModeEvent.emit({
            mode: this.compassMode,
        });
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(SyncUiEventDispatcher_1.SyncUiEventId.AccuDrawCompassModeChanged);
        this.outputCompassModeMessage();
    }
    onRotationModeChange() {
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(SyncUiEventDispatcher_1.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() {
        this.onFieldValueChange(core_frontend_1.ItemField.X_Item);
        this.onFieldValueChange(core_frontend_1.ItemField.Y_Item);
        this.onFieldValueChange(core_frontend_1.ItemField.Z_Item);
        this.onFieldValueChange(core_frontend_1.ItemField.ANGLE_Item);
        this.onFieldValueChange(core_frontend_1.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_1.UiFramework.isContextMenuOpen)
            return;
        this.fieldValuesChanged();
        if (!this.dontMoveFocus)
            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 = core_frontend_1.IModelApp.accuDraw.getValueByIndex(index);
        let formattedValue = value.toString();
        const formatterSpec = core_frontend_1.IModelApp.quantityFormatter.findFormatterSpecByQuantityType(core_frontend_1.ItemField.ANGLE_Item === index
            ? core_frontend_1.QuantityType.Angle
            : core_frontend_1.QuantityType.Length);
        if (formatterSpec)
            formattedValue = core_frontend_1.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)
            core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Info, message));
    }
    outputCompassModeMessage() {
        if (FrameworkAccuDraw.displayNotifications) {
            const modeKey = compassModeToKeyMap.get(this.compassMode) ?? "polar";
            const modeString = UiFramework_1.UiFramework.translate(`accuDraw.compassMode.${modeKey}`);
            const modeMessage = UiFramework_1.UiFramework.translate("accuDraw.compassModeSet", {
                modeString,
            });
            this.outputInfoMessage(modeMessage);
        }
    }
    outputRotationMessage() {
        if (FrameworkAccuDraw.displayNotifications) {
            const rotationKey = rotationModeToKeyMap.get(this.rotationMode) ?? "top";
            const rotationString = UiFramework_1.UiFramework.translate(`accuDraw.rotation.${rotationKey}`);
            const rotationMessage = UiFramework_1.UiFramework.translate("accuDraw.rotationSet", {
                rotationString,
            });
            this.outputInfoMessage(rotationMessage);
        }
    }
}
exports.FrameworkAccuDraw = FrameworkAccuDraw;
FrameworkAccuDraw._displayNotifications = false;
FrameworkAccuDraw._settingsNamespace = "AppUiSettings";
FrameworkAccuDraw._notificationsKey = "AccuDrawNotifications";
/** AccuDraw Set Field Focus event. */
FrameworkAccuDraw.onAccuDrawSetFieldFocusEvent = new AccuDrawSetFieldFocusEvent(); // eslint-disable-line deprecation/deprecation
/** AccuDraw Set Field Value to Ui event. */
FrameworkAccuDraw.onAccuDrawSetFieldValueToUiEvent = new AccuDrawSetFieldValueToUiEvent(); // eslint-disable-line deprecation/deprecation
/** AccuDraw Set Field Value from Ui event. */
FrameworkAccuDraw.onAccuDrawSetFieldValueFromUiEvent = new AccuDrawSetFieldValueFromUiEvent(); // eslint-disable-line deprecation/deprecation
/** AccuDraw Set Field Lock event. */
FrameworkAccuDraw.onAccuDrawSetFieldLockEvent = new AccuDrawSetFieldLockEvent(); // eslint-disable-line deprecation/deprecation
/** AccuDraw Set Mode event. */
FrameworkAccuDraw.onAccuDrawSetCompassModeEvent = new AccuDrawSetCompassModeEvent(); // eslint-disable-line deprecation/deprecation
/** AccuDraw Grab Input Focus event. */
FrameworkAccuDraw.onAccuDrawGrabInputFocusEvent = new AccuDrawGrabInputFocusEvent(); // eslint-disable-line deprecation/deprecation
/** Determines if AccuDraw.rotationMode === RotationMode.Top */
FrameworkAccuDraw.isTopRotationConditional = new appui_abstract_1.ConditionalBooleanValue(() => core_frontend_1.IModelApp.accuDraw.rotationMode === core_frontend_1.RotationMode.Top, [SyncUiEventDispatcher_1.SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.Front */
FrameworkAccuDraw.isFrontRotationConditional = new appui_abstract_1.ConditionalBooleanValue(() => core_frontend_1.IModelApp.accuDraw.rotationMode === core_frontend_1.RotationMode.Front, [SyncUiEventDispatcher_1.SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.Side */
FrameworkAccuDraw.isSideRotationConditional = new appui_abstract_1.ConditionalBooleanValue(() => core_frontend_1.IModelApp.accuDraw.rotationMode === core_frontend_1.RotationMode.Side, [SyncUiEventDispatcher_1.SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.View */
FrameworkAccuDraw.isViewRotationConditional = new appui_abstract_1.ConditionalBooleanValue(() => core_frontend_1.IModelApp.accuDraw.rotationMode === core_frontend_1.RotationMode.View, [SyncUiEventDispatcher_1.SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.ACS */
FrameworkAccuDraw.isACSRotationConditional = new appui_abstract_1.ConditionalBooleanValue(() => core_frontend_1.IModelApp.accuDraw.rotationMode === core_frontend_1.RotationMode.ACS, [SyncUiEventDispatcher_1.SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.rotationMode === RotationMode.Context */
FrameworkAccuDraw.isContextRotationConditional = new appui_abstract_1.ConditionalBooleanValue(() => core_frontend_1.IModelApp.accuDraw.rotationMode === core_frontend_1.RotationMode.Context, [SyncUiEventDispatcher_1.SyncUiEventId.AccuDrawRotationChanged]);
/** Determines if AccuDraw.compassMode === CompassMode.Polar */
FrameworkAccuDraw.isPolarModeConditional = new appui_abstract_1.ConditionalBooleanValue(() => core_frontend_1.IModelApp.accuDraw.compassMode === core_frontend_1.CompassMode.Polar, [SyncUiEventDispatcher_1.SyncUiEventId.AccuDrawCompassModeChanged]);
/** Determines if AccuDraw.compassMode === CompassMode.Rectangular */
FrameworkAccuDraw.isRectangularModeConditional = new appui_abstract_1.ConditionalBooleanValue(() => core_frontend_1.IModelApp.accuDraw.compassMode === core_frontend_1.CompassMode.Rectangular, [SyncUiEventDispatcher_1.SyncUiEventId.AccuDrawCompassModeChanged]);
/** AccuDraw Grab Input Focus event. */
FrameworkAccuDraw.onAccuDrawUiSettingsChangedEvent = new AccuDrawUiSettingsChangedEvent(); // eslint-disable-line deprecation/deprecation
//# sourceMappingURL=FrameworkAccuDraw.js.map