"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 Tools
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectionContextToolDefinitions = exports.selectionContextStateFunc = exports.featureOverridesActiveStateFunc = exports.getIsHiddenIfSelectionNotActive = exports.getIsHiddenIfFeatureOverridesActive = exports.areNoFeatureOverridesActive = exports.isNoSelectionActive = exports.getSelectionContextSyncEventIds = exports.getFeatureOverrideSyncEventIds = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const SessionState_1 = require("../redux/SessionState");
const CommandItemDef_1 = require("../shared/CommandItemDef");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const GroupItem_1 = require("../toolbar/GroupItem");
const UiFramework_1 = require("../UiFramework");
const model_isolate_svg_1 = require("@bentley/icons-generic/icons/model-isolate.svg");
const layers_isolate_svg_1 = require("@bentley/icons-generic/icons/layers-isolate.svg");
const asset_isolate_svg_1 = require("@bentley/icons-generic/icons/asset-isolate.svg");
const isolate_svg_1 = require("@bentley/icons-generic/icons/isolate.svg");
const model_hide_svg_1 = require("@bentley/icons-generic/icons/model-hide.svg");
const layers_hide_svg_1 = require("@bentley/icons-generic/icons/layers-hide.svg");
const asset_classification_hide_svg_1 = require("@bentley/icons-generic/icons/asset-classification-hide.svg");
const visibility_semi_transparent_svg_1 = require("@bentley/icons-generic/icons/visibility-semi-transparent.svg");
const visibility_hide_2_svg_1 = require("@bentley/icons-generic/icons/visibility-hide_2.svg");
const visibility_svg_1 = require("@bentley/icons-generic/icons/visibility.svg");
/** return SyncEventIds that trigger selection state function refresh.
 * @beta
 */
function getFeatureOverrideSyncEventIds() {
    return [
        SyncUiEventDispatcher_1.SyncUiEventId.ActiveContentChanged,
        SyncUiEventDispatcher_1.SyncUiEventId.ActiveViewportChanged,
        SyncUiEventDispatcher_1.SyncUiEventId.FeatureOverridesChanged,
        SyncUiEventDispatcher_1.SyncUiEventId.ViewedModelsChanged,
    ];
}
exports.getFeatureOverrideSyncEventIds = getFeatureOverrideSyncEventIds;
/** return SyncEventIds that trigger selection state function refresh.
 * @beta
 */
function getSelectionContextSyncEventIds() {
    return [
        SyncUiEventDispatcher_1.SyncUiEventId.SelectionSetChanged,
        SyncUiEventDispatcher_1.SyncUiEventId.ActiveContentChanged,
        SyncUiEventDispatcher_1.SyncUiEventId.ActiveViewportChanged,
        SessionState_1.SessionStateActionId.SetNumItemsSelected,
        SyncUiEventDispatcher_1.SyncUiEventId.FeatureOverridesChanged,
        SyncUiEventDispatcher_1.SyncUiEventId.ViewedModelsChanged,
    ];
}
exports.getSelectionContextSyncEventIds = getSelectionContextSyncEventIds;
/** return SyncEventIds that trigger selection state function refresh.
 * @beta
 */
function isNoSelectionActive() {
    const activeContentControl = UiFramework_1.UiFramework.content.getActiveContentControl();
    let selectionCount = 0;
    // istanbul ignore if
    if (!UiFramework_1.UiFramework.frameworkStateKey)
        selectionCount =
            UiFramework_1.UiFramework.store.getState()[UiFramework_1.UiFramework.frameworkStateKey].frameworkState
                .sessionState.numItemsSelected;
    // istanbul ignore if
    if (activeContentControl === null || activeContentControl === void 0 ? void 0 : activeContentControl.viewport) {
        const hiddenElementsSet = activeContentControl.viewport.neverDrawn;
        const selectedElementsSet = activeContentControl.viewport.view.iModel.selectionSet.elements;
        // TODO: add check for categories, subcategories and models
        if (selectionCount > 0 ||
            ![...selectedElementsSet].every((value) => hiddenElementsSet === null || hiddenElementsSet === void 0 ? void 0 : hiddenElementsSet.has(value)))
            return false;
    }
    return true;
}
exports.isNoSelectionActive = isNoSelectionActive;
/** return ConditionalBooleanValue object used to show items if selection set is active.
 * @beta
 */
function areNoFeatureOverridesActive() {
    const activeContentControl = UiFramework_1.UiFramework.content.getActiveContentControl();
    // istanbul ignore next
    if (activeContentControl && activeContentControl.viewport)
        return !UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.areFeatureOverridesActive(activeContentControl.viewport);
    return true;
}
exports.areNoFeatureOverridesActive = areNoFeatureOverridesActive;
/** return ConditionalBooleanValue object used to show item if feature overrides are active.
 * @beta
 */
function getIsHiddenIfFeatureOverridesActive() {
    return new appui_abstract_1.ConditionalBooleanValue(areNoFeatureOverridesActive, getFeatureOverrideSyncEventIds());
}
exports.getIsHiddenIfFeatureOverridesActive = getIsHiddenIfFeatureOverridesActive;
/** return ConditionalBooleanValue object used to show items if selection set is active.
 * @beta
 */
function getIsHiddenIfSelectionNotActive() {
    return new appui_abstract_1.ConditionalBooleanValue(isNoSelectionActive, getSelectionContextSyncEventIds());
}
exports.getIsHiddenIfSelectionNotActive = getIsHiddenIfSelectionNotActive;
/** return state with isVisible set to true is SectionSet is active.
 * @beta
 */
// istanbul ignore next
function featureOverridesActiveStateFunc(state) {
    const activeContentControl = UiFramework_1.UiFramework.content.getActiveContentControl();
    let isVisible = false;
    // istanbul ignore next
    if (activeContentControl && activeContentControl.viewport)
        isVisible =
            UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.areFeatureOverridesActive(activeContentControl.viewport);
    return { ...state, isVisible };
}
exports.featureOverridesActiveStateFunc = featureOverridesActiveStateFunc;
/** return state with isVisible set to true is SectionSet is active.
 * @beta
 */
// istanbul ignore next
function selectionContextStateFunc(state) {
    const activeContentControl = UiFramework_1.UiFramework.content.getActiveContentControl();
    let isVisible = false;
    let selectionCount = 0;
    if (!UiFramework_1.UiFramework.frameworkStateKey)
        selectionCount =
            UiFramework_1.UiFramework.store.getState()[UiFramework_1.UiFramework.frameworkStateKey].frameworkState
                .sessionState.numItemsSelected;
    if (activeContentControl &&
        activeContentControl.viewport &&
        (activeContentControl.viewport.view.iModel.selectionSet.size > 0 ||
            selectionCount > 0))
        isVisible = true;
    return { ...state, isVisible };
}
exports.selectionContextStateFunc = selectionContextStateFunc;
/** Utility Class that provides definitions for tools dependent on current selection. These definitions can be used to populate toolbars.
 * @public
 */
// istanbul ignore next
class SelectionContextToolDefinitions {
    static get isolateModelsInSelectionItemDef() {
        return new CommandItemDef_1.CommandItemDef({
            commandId: "UiFramework.IsolateModel",
            iconSpec: model_isolate_svg_1.default,
            labelKey: "UiFramework:tools.isolateModels",
            execute: async () => UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processIsolateSelectedElementsModel(),
        });
    }
    static get isolateCategoriesInSelectionItemDef() {
        return new CommandItemDef_1.CommandItemDef({
            commandId: "UiFramework.IsolateCategory",
            iconSpec: layers_isolate_svg_1.default,
            labelKey: "UiFramework:tools.isolateCategories",
            execute: async () => UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processIsolateSelectedElementsCategory(),
        });
    }
    static get isolateElementsItemDef() {
        return new CommandItemDef_1.CommandItemDef({
            commandId: "UiFramework.IsolateSelected",
            iconSpec: asset_isolate_svg_1.default,
            labelKey: "UiFramework:tools.isolateSelected",
            isHidden: getIsHiddenIfSelectionNotActive(),
            execute: async () => UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processIsolateSelected(),
        });
    }
    static get isolateSelectionToolGroup() {
        return new GroupItem_1.GroupItemDef({
            groupId: "UiFramework.IsolateSelectionGroup",
            labelKey: "UiFramework:tools.isolate",
            iconSpec: isolate_svg_1.default,
            isHidden: getIsHiddenIfSelectionNotActive(),
            items: [
                this.isolateElementsItemDef,
                this.isolateCategoriesInSelectionItemDef,
                this.isolateModelsInSelectionItemDef,
            ],
            itemsInColumn: 3,
        });
    }
    static get hideModelsInSelectionItemDef() {
        return new CommandItemDef_1.CommandItemDef({
            commandId: "UiFramework.HideModel",
            iconSpec: model_hide_svg_1.default,
            labelKey: "UiFramework:tools.hideModels",
            execute: async () => UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processHideSelectedElementsModel(),
        });
    }
    static get hideCategoriesInSelectionItemDef() {
        return new CommandItemDef_1.CommandItemDef({
            commandId: "UiFramework.HideCategory",
            iconSpec: layers_hide_svg_1.default,
            labelKey: "UiFramework:tools.hideCategories",
            execute: async () => UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processHideSelectedElementsCategory(),
        });
    }
    static get hideElementsItemDef() {
        return new CommandItemDef_1.CommandItemDef({
            commandId: "UiFramework.HideSelected",
            iconSpec: asset_classification_hide_svg_1.default,
            labelKey: "UiFramework:tools.hideSelected",
            isHidden: getIsHiddenIfSelectionNotActive(),
            execute: async () => UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processHideSelected(),
        });
    }
    static get hideSectionToolGroup() {
        return new GroupItem_1.GroupItemDef({
            groupId: "UiFramework.HideSelectionGroup",
            labelKey: "UiFramework:tools.hide",
            iconSpec: visibility_hide_2_svg_1.default,
            isHidden: getIsHiddenIfSelectionNotActive(),
            items: [
                this.hideElementsItemDef,
                this.hideCategoriesInSelectionItemDef,
                this.hideModelsInSelectionItemDef,
            ],
            itemsInColumn: 3,
        });
    }
    static get emphasizeElementsItemDef() {
        return new CommandItemDef_1.CommandItemDef({
            commandId: "UiFramework.EmphasizeSelected",
            iconSpec: visibility_semi_transparent_svg_1.default,
            labelKey: "UiFramework:tools.emphasizeSelected",
            isHidden: getIsHiddenIfSelectionNotActive(),
            execute: async () => UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processEmphasizeSelected(),
        });
    }
    static get clearHideIsolateEmphasizeElementsItemDef() {
        return new CommandItemDef_1.CommandItemDef({
            commandId: "UiFramework.ClearHideIsolateEmphasize",
            iconSpec: visibility_svg_1.default,
            labelKey: "UiFramework:tools.clearVisibility",
            isHidden: getIsHiddenIfFeatureOverridesActive(),
            execute: async () => {
                await UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processClearEmphasize();
                await UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processClearOverrideModels();
                await UiFramework_1.UiFramework.hideIsolateEmphasizeActionHandler.processClearOverrideCategories();
            },
        });
    }
}
exports.SelectionContextToolDefinitions = SelectionContextToolDefinitions;
//# sourceMappingURL=SelectionContextItemDef.js.map