"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.HideIsolateEmphasizeManager = exports.HideIsolateEmphasizeActionHandler = exports.HideIsolateEmphasizeAction = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const core_frontend_1 = require("@itwin/core-frontend");
const presentation_frontend_1 = require("@itwin/presentation-frontend");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const UiFramework_1 = require("../UiFramework");
/** Supported Hide, Isolate, and Emphasize Actions. These also serve as FeatureTracking Ids.
 * @public
 */
var HideIsolateEmphasizeAction;
(function (HideIsolateEmphasizeAction) {
    HideIsolateEmphasizeAction["EmphasizeSelectedElements"] = "EmphasizeSelectedElements";
    HideIsolateEmphasizeAction["IsolateSelectedElements"] = "IsolateSelectedElements";
    HideIsolateEmphasizeAction["IsolateSelectedCategories"] = "IsolateSelectedCategories";
    HideIsolateEmphasizeAction["IsolateSelectedModels"] = "IsolateSelectedModels";
    HideIsolateEmphasizeAction["HideSelectedElements"] = "HideSelectedElements";
    HideIsolateEmphasizeAction["HideSelectedModels"] = "HideSelectedModels";
    HideIsolateEmphasizeAction["HideSelectedCategories"] = "HideSelectedCategories";
    HideIsolateEmphasizeAction["ClearHiddenIsolatedEmphasized"] = "ClearHiddenIsolatedEmphasized";
    HideIsolateEmphasizeAction["ClearOverrideModels"] = "ClearOverrideModels";
    HideIsolateEmphasizeAction["ClearOverrideCategories"] = "ClearOverrideCategories";
})(HideIsolateEmphasizeAction = exports.HideIsolateEmphasizeAction || (exports.HideIsolateEmphasizeAction = {}));
const featureIdMap = new Map([
    [HideIsolateEmphasizeAction.EmphasizeSelectedElements, "d74eb93f-deae-4700-8da6-1013a8a7aa26"],
    [HideIsolateEmphasizeAction.IsolateSelectedElements, "24327638-1611-45fa-a379-fa73329098ec"],
    [HideIsolateEmphasizeAction.IsolateSelectedCategories, "e58081ab-2c33-4a15-924f-71082b58ca3b"],
    [HideIsolateEmphasizeAction.IsolateSelectedModels, "3475921e-7dd1-4547-993e-a3e284ef8b62"],
    [HideIsolateEmphasizeAction.HideSelectedElements, "2ca673ec-001a-4890-bc25-18bc88358fe0"],
    [HideIsolateEmphasizeAction.HideSelectedModels, "8b41e859-ae17-4e19-b220-87a5cf9f8242"],
    [HideIsolateEmphasizeAction.HideSelectedCategories, "c5d6916b-e8d7-4796-bae9-a5303712d46b"],
    [HideIsolateEmphasizeAction.ClearHiddenIsolatedEmphasized, "7b135c8a-3f3c-4297-b36c-b0ac51f1d8de"],
    [HideIsolateEmphasizeAction.ClearOverrideModels, "6e519b94-edab-4b13-9ce7-0bcdfa27ccfe"],
    [HideIsolateEmphasizeAction.ClearOverrideCategories, "9d2be3c6-6992-4c30-84eb-10f6b9379d06"],
]);
/** Overrides given models to provide emphasize functionality
 * @public
 */
// istanbul ignore next
class ModelOverrideProvider {
    constructor(modelIds, defaultAppearance) {
        this.modelIds = modelIds;
        this.defaultAppearance = defaultAppearance;
    }
    addFeatureOverrides(overrides, _viewport) {
        overrides.setDefaultOverrides(this.defaultAppearance, true);
        // Override with nothing so that we keep the model looking normal and override the default appearance of everything else
        const appearance = core_common_1.FeatureAppearance.fromJSON({});
        this.modelIds.forEach((modelId) => {
            overrides.override({ modelId, appearance, onConflict: "replace" });
        });
    }
}
/** Overrides given categories to provide emphasize functionality
 *  @public
 */
// istanbul ignore next
class SubCategoryOverrideProvider {
    constructor(subCategoryIds, defaultAppearance) {
        this.subCategoryIds = subCategoryIds;
        this.defaultAppearance = defaultAppearance;
    }
    addFeatureOverrides(overrides, _viewport) {
        overrides.setDefaultOverrides(this.defaultAppearance, true);
        // Override with nothing so that we keep the category looking normal and override the default appearance of everything else
        const appearance = core_common_1.FeatureAppearance.fromJSON({});
        this.subCategoryIds.forEach((subCategoryId) => {
            overrides.override({ subCategoryId, appearance, onConflict: "replace" });
        });
    }
}
/** Cache of Models that are inside of subjects */
// istanbul ignore next
class SubjectModelIdsCache {
    constructor(imodel) {
        this._imodel = imodel;
    }
    async initSubjectsHierarchy() {
        this._subjectsHierarchy = new Map();
        const ecsql = `SELECT ECInstanceId id, Parent.Id parentId FROM bis.Subject WHERE Parent IS NOT NULL`;
        // eslint-disable-next-line deprecation/deprecation
        const result = this._imodel.query(ecsql, undefined, { limit: { count: 1000 } });
        for await (const row of result) {
            let list = this._subjectsHierarchy.get(row.parentId);
            if (!list) {
                list = [];
                this._subjectsHierarchy.set(row.parentId, list);
            }
            list.push(row.id);
        }
    }
    async initSubjectModels() {
        this._subjectModels = new Map();
        const ecsql = `SELECT p.ECInstanceId id, p.Parent.Id subjectId FROM bis.InformationPartitionElement p JOIN bis.Model m ON m.ModeledElement.Id = p.ECInstanceId`;
        // eslint-disable-next-line deprecation/deprecation
        const result = this._imodel.query(ecsql, undefined, { limit: { count: 1000 } });
        for await (const row of result) {
            let list = this._subjectModels.get(row.subjectId);
            if (!list) {
                list = [];
                this._subjectModels.set(row.subjectId, list);
            }
            list.push(row.id);
        }
    }
    async initCache() {
        if (!this._init) {
            this._init = Promise.all([this.initSubjectModels(), this.initSubjectsHierarchy()]).then(() => { });
        }
        return this._init;
    }
    appendSubjectModelsRecursively(modelIds, subjectId) {
        const subjectModelIds = this._subjectModels.get(subjectId);
        if (subjectModelIds)
            modelIds.push(...subjectModelIds);
        const childSubjectIds = this._subjectsHierarchy.get(subjectId);
        if (childSubjectIds)
            childSubjectIds.forEach((cs) => this.appendSubjectModelsRecursively(modelIds, cs));
    }
    async getSubjectModelIds(subjectId) {
        await this.initCache();
        const modelIds = new Array();
        this.appendSubjectModelsRecursively(modelIds, subjectId);
        return modelIds;
    }
}
/**
 * Interface for class that handles Hide, Isolate, and Emphasize Actions
 * @public
 */
class HideIsolateEmphasizeActionHandler {
    /** String Id sent to allow UI to refresh its display state.  */
    static get hideIsolateEmphasizeUiSyncId() {
        return "selection-context-emphasize-elements-changed";
    }
}
exports.HideIsolateEmphasizeActionHandler = HideIsolateEmphasizeActionHandler;
HideIsolateEmphasizeActionHandler.emphasizeElementsChanged = new core_bentley_1.BeEvent();
/** Provides helper functions for doing commands on logical selection like categories and subjects.
 * @public
 */
// istanbul ignore next
class HideIsolateEmphasizeManager extends HideIsolateEmphasizeActionHandler {
    /**
     * Initialize the subject model cache
     * @param iModelConnection iModel to use for querying subject's models
     */
    static initializeSubjectModelCache(iModelConnection) {
        HideIsolateEmphasizeManager._subjectModelIdsCache = new SubjectModelIdsCache(iModelConnection);
    }
    /**
     * Returns true if the selection in presentation layer only has a single selection class and it is the given one
     * @param className ECClass name to check for
     */
    static _checkClassSelected(className) {
        if (!UiFramework_1.UiFramework.getIModelConnection())
            throw new Error("Undefined iModelConnection");
        const selection = presentation_frontend_1.Presentation.selection.getSelection(UiFramework_1.UiFramework.getIModelConnection());
        return selection.size === 1 && selection.instanceKeys.has(className);
    }
    /**
     * Gets the Ids of all elements selected that have the given class name
     * @param className ECClass name to check for
     */
    static _getIdsOfClassName(className) {
        if (!UiFramework_1.UiFramework.getIModelConnection())
            throw new Error("Undefined iModelConnection");
        const selection = presentation_frontend_1.Presentation.selection.getSelection(UiFramework_1.UiFramework.getIModelConnection());
        const ids = [];
        selection.instanceKeys.forEach((currentIds, key) => {
            if (key === className)
                ids.push(...currentIds);
        });
        return ids;
    }
    /** Returns true if there are only categories selected in presentation's logical selection */
    static categorySelected() {
        return HideIsolateEmphasizeManager._checkClassSelected(HideIsolateEmphasizeManager._categoryClassName);
    }
    /** Returns true if there are only subjects selected in presentation's logical selection */
    static subjectSelected() {
        return HideIsolateEmphasizeManager._checkClassSelected(HideIsolateEmphasizeManager._subjectClassName);
    }
    /** Returns true if a model is selected in presentation's logical selection */
    static modelSelected() {
        return HideIsolateEmphasizeManager._checkClassSelected(HideIsolateEmphasizeManager._modelClassName);
    }
    /**
     * Hide the selected category found in presentation layer's logical selection
     * @param vp Viewport to affect
     */
    static hideSelectedCategory(vp) {
        const ids = HideIsolateEmphasizeManager._getIdsOfClassName(HideIsolateEmphasizeManager._categoryClassName);
        vp.changeCategoryDisplay(ids, false);
    }
    /** Get sub categories that relate to the category Id */
    static async _getSubCategories(iModelConnection, categoryIds) {
        const allSubcats = [];
        const request = iModelConnection.subcategories.load(categoryIds);
        if (request)
            await request.promise;
        for (const categoryId of categoryIds) {
            const subcats = iModelConnection.subcategories.getSubCategories(categoryId);
            if (subcats)
                allSubcats.push(...subcats);
        }
        return allSubcats;
    }
    /**
     * Emphasize the selected category found in presentation layer's logical selection
     * @param vp Viewport to affect
     */
    static async emphasizeSelectedCategory(vp) {
        const ids = HideIsolateEmphasizeManager._getIdsOfClassName(HideIsolateEmphasizeManager._categoryClassName);
        if (ids.length === 0)
            return;
        const defaultAppearance = core_frontend_1.EmphasizeElements.getOrCreate(vp).createDefaultAppearance();
        core_frontend_1.EmphasizeElements.clear(vp);
        const subcats = await HideIsolateEmphasizeManager._getSubCategories(vp.iModel, ids);
        vp.addFeatureOverrideProvider(new SubCategoryOverrideProvider(subcats, defaultAppearance));
    }
    /**
     * Query the model Id that models a subject
     * @param subjectId Subject Id to use in query
     */
    static async _getModelIds(subjectId) {
        return HideIsolateEmphasizeManager._subjectModelIdsCache.getSubjectModelIds(subjectId);
    }
    /**
     * Hide the selected subject's model found in the presentation layer's logical selection
     * @param vp Viewport to affect
     */
    static async hideSelectedSubject(vp) {
        const ids = HideIsolateEmphasizeManager._getIdsOfClassName(HideIsolateEmphasizeManager._subjectClassName);
        if (ids.length === 0)
            return;
        const modelIds = await HideIsolateEmphasizeManager._getModelIds(ids[0]);
        vp.changeModelDisplay(modelIds, false);
    }
    /**
     * Isolate the selected subject's model found in the presentation layer's logical selection
     * @param vp Viewport to affect
     */
    static async emphasizeSelectedSubject(vp) {
        const ids = HideIsolateEmphasizeManager._getIdsOfClassName(HideIsolateEmphasizeManager._subjectClassName);
        if (ids.length === 0)
            return;
        const modelIds = await HideIsolateEmphasizeManager._getModelIds(ids[0]);
        const defaultAppearance = core_frontend_1.EmphasizeElements.getOrCreate(vp).createDefaultAppearance();
        core_frontend_1.EmphasizeElements.clear(vp);
        vp.addFeatureOverrideProvider(new ModelOverrideProvider(modelIds, defaultAppearance));
    }
    /**
     * Hide the selected model
     * @param vp Viewport to affect
     */
    static hideSelectedModel(vp) {
        const ids = HideIsolateEmphasizeManager._getIdsOfClassName(HideIsolateEmphasizeManager._modelClassName);
        if (ids.length === 0)
            return;
        vp.changeModelDisplay(ids, false);
    }
    /**
     * Isolate the selected model
     * @param vp Viewport to affect
     */
    static emphasizeSelectedModel(vp) {
        const ids = HideIsolateEmphasizeManager._getIdsOfClassName(HideIsolateEmphasizeManager._modelClassName);
        if (ids.length === 0)
            return;
        const defaultAppearance = core_frontend_1.EmphasizeElements.getOrCreate(vp).createDefaultAppearance();
        core_frontend_1.EmphasizeElements.clear(vp);
        vp.addFeatureOverrideProvider(new ModelOverrideProvider(ids, defaultAppearance));
    }
    /**
     * Isolate the selected elements
     * @param vp Viewport to affect
     */
    static isolateSelected(vp) {
        core_frontend_1.EmphasizeElements.getOrCreate(vp).isolateSelectedElements(vp, true, false); // Isolate selected elements
    }
    /**
     * Hide the selected elements
     * @param vp Viewport to affect
     */
    static hideSelected(vp) {
        core_frontend_1.EmphasizeElements.getOrCreate(vp).hideSelectedElements(vp, false, false); // Hide all selected elements
    }
    /**
     * Clear Hidden,Isolated, or Emphasized elements in specified view
     * @param vp Viewport to affect
     *
     */
    static clearEmphasize(vp) {
        if (vp) {
            core_frontend_1.EmphasizeElements.clear(vp);
        }
    }
    /**
     * Emphasize the selected elements from either presentation layer's logical selection or selected graphics
     * @param vp Viewport to affect
     * @param emphasisSilhouette defaults to true
     */
    static async emphasizeSelected(vp, emphasisSilhouette = true) {
        if (HideIsolateEmphasizeManager.categorySelected()) {
            await HideIsolateEmphasizeManager.emphasizeSelectedCategory(vp);
            return;
        }
        else if (HideIsolateEmphasizeManager.modelSelected()) {
            HideIsolateEmphasizeManager.emphasizeSelectedModel(vp);
            return;
        }
        else if (HideIsolateEmphasizeManager.subjectSelected()) {
            await HideIsolateEmphasizeManager.emphasizeSelectedSubject(vp);
            return;
        }
        const ee = core_frontend_1.EmphasizeElements.getOrCreate(vp);
        ee.wantEmphasis = emphasisSilhouette;
        ee.emphasizeSelectedElements(vp, undefined, true, false); // Emphasize elements by making all others grey/transparent
        vp.isFadeOutActive = true; // Enable flat alpha for greyed out elements…
    }
    /**
     * Isolate the selected subject's model found in the presentation layer's logical selection
     * @param vp Viewport to affect
     */
    static async isolateSelectedSubject(vp) {
        const ids = HideIsolateEmphasizeManager._getIdsOfClassName(HideIsolateEmphasizeManager._subjectClassName);
        if (ids.length === 0)
            return;
        const modelIds = await HideIsolateEmphasizeManager._getModelIds(ids[0]);
        await vp.replaceViewedModels(modelIds);
    }
    /**
     * Isolate the selected model
     * @param vp Viewport to affect
     */
    static async isolateSelectedModel(vp) {
        const ids = HideIsolateEmphasizeManager._getIdsOfClassName(HideIsolateEmphasizeManager._modelClassName);
        if (ids.length === 0)
            return;
        await vp.replaceViewedModels(ids);
    }
    /**
     * Isolate the selected category found in presentation layer's logical selection
     * @param vp Viewport to affect
     */
    static isolateSelectedCategory(vp) {
        const ids = new Set(HideIsolateEmphasizeManager._getIdsOfClassName(HideIsolateEmphasizeManager._categoryClassName));
        const categoriesToDrop = [];
        vp.view.categorySelector.categories.forEach((categoryId) => {
            if (!ids.has(categoryId))
                categoriesToDrop.push(categoryId);
        });
        vp.changeCategoryDisplay(categoriesToDrop, false);
        vp.changeCategoryDisplay(ids, true);
    }
    static async getSelectionSetElementModels(iModel) {
        const props = await iModel.elements.getProps(iModel.selectionSet.elements);
        const modelIds = new Set();
        for (const prop of props)
            if (prop.model)
                modelIds.add(prop.model);
        return modelIds;
    }
    static async getSelectionSetElementCategories(iModel) {
        const props = (await iModel.elements.getProps(iModel.selectionSet.elements));
        const categoryIds = new Set();
        for (const prop of props)
            if (prop.category)
                categoryIds.add(prop.category);
        return categoryIds;
    }
    /**
     * Isolate either based on Presentation selection, if defined, else the selected graphic elements
     * @param vp Viewport to affect
     */
    static async isolateCommand(vp) {
        if (HideIsolateEmphasizeManager.categorySelected()) {
            HideIsolateEmphasizeManager.isolateSelectedCategory(vp);
            return;
        }
        else if (HideIsolateEmphasizeManager.modelSelected()) {
            await HideIsolateEmphasizeManager.isolateSelectedModel(vp);
            return;
        }
        else if (HideIsolateEmphasizeManager.subjectSelected()) {
            await HideIsolateEmphasizeManager.isolateSelectedSubject(vp);
            return;
        }
    }
    /**
     * Isolate model from selected elements
     * @param vp Viewport to affect
     */
    static async isolateSelectedElementsModel(vp) {
        const modelsToKeep = new Set(await HideIsolateEmphasizeManager.getSelectionSetElementModels(vp.iModel));
        if (vp.view.isSpatialView()) {
            const modelsToTurnOff = [...vp.view.modelSelector.models].filter((modelId) => !modelsToKeep.has(modelId));
            this.updateModelOverride(vp, modelsToTurnOff);
        }
        await vp.replaceViewedModels(modelsToKeep);
    }
    /**
     * Isolate the selected category found in SelectionSet elements
     * @param vp Viewport to affect
     */
    static async isolateSelectedElementsCategory(vp) {
        const categoriesToKeep = new Set(await HideIsolateEmphasizeManager.getSelectionSetElementCategories(vp.iModel));
        const categoriesToTurnOff = [...vp.view.categorySelector.categories].filter((categoryId) => !categoriesToKeep.has(categoryId));
        vp.changeCategoryDisplay(categoriesToTurnOff, false);
        vp.changeCategoryDisplay(categoriesToKeep, true);
        this.updateCategoryOverride(vp, categoriesToTurnOff);
    }
    /**
     * Hide either based on Presentation selection, if defined, else the selected graphic elements
     * @param vp Viewport to affect
     */
    static async hideCommand(vp) {
        if (HideIsolateEmphasizeManager.categorySelected()) {
            HideIsolateEmphasizeManager.hideSelectedCategory(vp);
            return;
        }
        else if (HideIsolateEmphasizeManager.modelSelected()) {
            HideIsolateEmphasizeManager.hideSelectedModel(vp);
            return;
        }
        else if (HideIsolateEmphasizeManager.subjectSelected()) {
            await HideIsolateEmphasizeManager.hideSelectedSubject(vp);
            return;
        }
        core_frontend_1.EmphasizeElements.getOrCreate(vp).hideSelectedElements(vp, false, false); // Hide selected elements
    }
    /**
     * Hide the models defined by the elements in the current SelectionSet
     * @param vp Viewport to affect
     */
    static async hideSelectedElementsModel(vp) {
        const modelIds = await HideIsolateEmphasizeManager.getSelectionSetElementModels(vp.iModel);
        vp.changeModelDisplay(modelIds, false);
        this.updateModelOverride(vp, [...modelIds]);
    }
    /**
     * Clear (restore) the previously hidden/isolated models hidden by hideSelectedElementsModel
     * @param vp Viewport to affect
     */
    static clearOverrideModels(vp) {
        const ids = this._overrideModelIds.get(vp);
        if (ids) {
            vp.changeModelDisplay([...ids], true);
            this.clearModelOverride(vp);
        }
    }
    /**
     * Determine if models are hidden by hideSelectedElementsModel or isolateSelectedElementsModel
     * @param vp Viewport to affect
     */
    static isOverrideModels(vp) {
        const ids = this._overrideModelIds.get(vp);
        return (ids) ? [...ids].length > 0 : false;
    }
    /**
     * Determine if categories are hidden by hideSelectedElementsCategory or isolateSelectedElementsCategory
     * @param vp Viewport to affect
     */
    static isOverrideCategories(vp) {
        const ids = this._overrideCategoryIds.get(vp);
        return (ids) ? [...ids].length > 0 : false;
    }
    /**
     * Hide the categories defined by the elements in the current SelectionSet
     * @param vp Viewport to affect
     */
    static async hideSelectedElementsCategory(vp) {
        const categoryIds = await HideIsolateEmphasizeManager.getSelectionSetElementCategories(vp.iModel);
        vp.changeCategoryDisplay(categoryIds, false);
        this.updateCategoryOverride(vp, [...categoryIds]);
    }
    /**
     * Clear (restore) the previously hidden categories hidden by hideSelectedElementsCategory
     * @param vp Viewport to affect
     */
    static clearOverrideCategories(vp) {
        const ids = this._overrideCategoryIds.get(vp);
        if (ids) {
            vp.changeCategoryDisplay([...ids], true);
            this.clearCategoryOverride(vp);
        }
    }
    /** Checks to see if any featureOverrideProviders are active */
    areFeatureOverridesActive(vp) {
        // Check all the emphasize possibilities
        const emphasizeElementsProvider = vp.findFeatureOverrideProviderOfType(core_frontend_1.EmphasizeElements);
        if (undefined !== emphasizeElementsProvider && emphasizeElementsProvider.isActive)
            return true;
        const modelOverrideProvider = vp.findFeatureOverrideProviderOfType(ModelOverrideProvider);
        if (undefined !== modelOverrideProvider && modelOverrideProvider.modelIds.length > 0)
            return true;
        const subCategoryOverrideProvider = vp.findFeatureOverrideProviderOfType(SubCategoryOverrideProvider);
        if (undefined !== subCategoryOverrideProvider && subCategoryOverrideProvider.subCategoryIds.length > 0)
            return true;
        // Check hide/isolate possibilities
        if (HideIsolateEmphasizeManager.isOverrideCategories(vp))
            return true;
        if (HideIsolateEmphasizeManager.isOverrideModels(vp))
            return true;
        return false;
    }
    /**
       * Function that is run when `IsolateSelectedElementsModel` tool button is pressed
       */
    async processIsolateSelectedElementsModel() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        await HideIsolateEmphasizeManager.isolateSelectedElementsModel(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.IsolateSelectedModels });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.IsolateSelectedModels, featureIdMap.get(HideIsolateEmphasizeAction.IsolateSelectedModels)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
    }
    /**
     * Function that is run when `IsolateSelectedElementsCategory` tool button is pressed
     */
    async processIsolateSelectedElementsCategory() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        await HideIsolateEmphasizeManager.isolateSelectedElementsCategory(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.IsolateSelectedCategories });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.IsolateSelectedCategories, featureIdMap.get(HideIsolateEmphasizeAction.IsolateSelectedCategories)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
    }
    /**
     * Function that is run when `IsolateSelected` tool button is pressed
     */
    async processIsolateSelected() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        HideIsolateEmphasizeManager.isolateSelected(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.IsolateSelectedElements });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.IsolateSelectedElements, featureIdMap.get(HideIsolateEmphasizeAction.IsolateSelectedElements)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
        // clear out selection now that any callbacks have processed
        const selection = vp.view.iModel.selectionSet;
        if (selection.isActive)
            selection.emptyAll();
    }
    /**
     * Function that is run when `HideSelectedElementsModel` tool button is pressed
     */
    async processHideSelectedElementsModel() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        await HideIsolateEmphasizeManager.hideSelectedElementsModel(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.HideSelectedModels });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.HideSelectedModels, featureIdMap.get(HideIsolateEmphasizeAction.HideSelectedModels)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
    }
    /**
     * Function that is run when `HideSelectedElementsCategory` tool button is pressed
     */
    async processHideSelectedElementsCategory() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        await HideIsolateEmphasizeManager.hideSelectedElementsCategory(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.HideSelectedCategories });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.HideSelectedCategories, featureIdMap.get(HideIsolateEmphasizeAction.HideSelectedCategories)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
    }
    /**
     * Function that is run when `HideSelected` tool button is pressed
     */
    async processHideSelected() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        HideIsolateEmphasizeManager.hideSelected(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.HideSelectedElements });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.HideSelectedCategories, featureIdMap.get(HideIsolateEmphasizeAction.HideSelectedElements)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
        // clear out selection now that any callbacks have processed
        const selection = vp.view.iModel.selectionSet;
        if (selection.isActive)
            selection.emptyAll();
    }
    /**
     * Function that is run when `EmphasizeSelected` tool button is pressed
     */
    async processEmphasizeSelected() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        await HideIsolateEmphasizeManager.emphasizeSelected(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.EmphasizeSelectedElements });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.EmphasizeSelectedElements, featureIdMap.get(HideIsolateEmphasizeAction.EmphasizeSelectedElements)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
        // clear out selection now that any callbacks have processed
        const selection = vp.view.iModel.selectionSet;
        if (selection.isActive)
            selection.emptyAll();
    }
    /**
     * Function that is run when `ClearEmphasize` tool button is pressed
     */
    async processClearEmphasize() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        HideIsolateEmphasizeManager.clearEmphasize(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.ClearHiddenIsolatedEmphasized });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.ClearHiddenIsolatedEmphasized, featureIdMap.get(HideIsolateEmphasizeAction.ClearHiddenIsolatedEmphasized)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
    }
    /**
     * Function that is run when `ClearOverrideModels` tool button is pressed
     */
    async processClearOverrideModels() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        HideIsolateEmphasizeManager.clearOverrideModels(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.ClearOverrideModels });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.ClearOverrideModels, featureIdMap.get(HideIsolateEmphasizeAction.ClearOverrideModels)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
    }
    /**
     * Function that is run when `ClearOverrideCategories` tool button is pressed
     */
    async processClearOverrideCategories() {
        const vp = core_frontend_1.IModelApp.viewManager.selectedView;
        if (!vp)
            return;
        HideIsolateEmphasizeManager.clearOverrideCategories(vp);
        HideIsolateEmphasizeActionHandler.emphasizeElementsChanged.raiseEvent({ viewport: vp, action: HideIsolateEmphasizeAction.ClearOverrideCategories });
        UiFramework_1.UiFramework.postTelemetry(HideIsolateEmphasizeAction.ClearOverrideCategories, featureIdMap.get(HideIsolateEmphasizeAction.ClearOverrideCategories)); // eslint-disable-line @typescript-eslint/no-floating-promises
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.dispatchSyncUiEvent(HideIsolateEmphasizeActionHandler.hideIsolateEmphasizeUiSyncId);
    }
    /**
     * Add category ids to the category override cache (hidden or isolated categories)
     */
    static updateCategoryOverride(vp, ids) {
        const prevIds = this._overrideCategoryIds.get(vp);
        const newIds = [...(prevIds || []), ...ids];
        this._overrideCategoryIds.set(vp, new Set(newIds));
    }
    /**
     * Add model ids to the model override cache (hidden or isolated models)
     */
    static updateModelOverride(vp, ids) {
        const prevIds = this._overrideModelIds.get(vp);
        const newIds = [...(prevIds || []), ...ids];
        this._overrideModelIds.set(vp, new Set(newIds));
    }
    /**
     * Return the list of category overrides (hidden or isolated categories)
     */
    static getCategoryOverrides(vp) {
        return this._overrideCategoryIds.get(vp);
    }
    /**
     * Return the list of model overrides (hidden or isolated models)
     */
    static getModelOverrides(vp) {
        return this._overrideModelIds.get(vp);
    }
    /**
     * Clear the category override cache (hidden or isolated categories)
     */
    static clearCategoryOverride(vp) {
        this._overrideCategoryIds.delete(vp);
    }
    /**
     * Clear the model override cache (hidden or isolated models)
     */
    static clearModelOverride(vp) {
        this._overrideModelIds.delete(vp);
    }
}
exports.HideIsolateEmphasizeManager = HideIsolateEmphasizeManager;
// TODO: We need to check the type by going to backend, not by using classnames
HideIsolateEmphasizeManager._categoryClassName = "BisCore:SpatialCategory";
HideIsolateEmphasizeManager._subjectClassName = "BisCore:Subject";
HideIsolateEmphasizeManager._modelClassName = "BisCore:PhysicalModel";
HideIsolateEmphasizeManager._subjectModelIdsCache = undefined;
HideIsolateEmphasizeManager._overrideCategoryIds = new Map();
HideIsolateEmphasizeManager._overrideModelIds = new Map();
//# sourceMappingURL=HideIsolateEmphasizeManager.js.map