"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 IModelComponents
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCategories = exports.toggleAllCategories = exports.CategoryTree = exports.RULESET_CATEGORIES = void 0;
/* eslint-disable deprecation/deprecation */
require("./CategoriesTree.scss");
const React = require("react");
const core_frontend_1 = require("@itwin/core-frontend");
const presentation_components_1 = require("@itwin/presentation-components");
const presentation_frontend_1 = require("@itwin/presentation-frontend");
const components_react_1 = require("@itwin/components-react");
const core_react_1 = require("@itwin/core-react");
const UiFramework_1 = require("../../UiFramework");
const VisibilityTreeEventHandler_1 = require("../VisibilityTreeEventHandler");
const VisibilityTreeRenderer_1 = require("../VisibilityTreeRenderer");
const CategoryVisibilityHandler_1 = require("./CategoryVisibilityHandler");
const PAGING_SIZE = 20;
/**
 * Presentation rules used by ControlledCategoriesTree
 * @internal
 */
exports.RULESET_CATEGORIES = require("./Categories.json"); // eslint-disable-line @typescript-eslint/no-var-requires
/**
 * Tree which displays and manages categories contained in an iModel.
 * @public
 * @deprecated in 3.6. Was moved to `@itwin/tree-widget-react` package.
 */
function CategoryTree(props) {
    var _a;
    const { nodeLoader } = (0, presentation_components_1.usePresentationTreeNodeLoader)({
        imodel: props.iModel,
        ruleset: exports.RULESET_CATEGORIES,
        pagingSize: PAGING_SIZE,
    });
    const { filteredNodeLoader, isFiltering, nodeHighlightingProps } = (0, VisibilityTreeRenderer_1.useVisibilityTreeFiltering)(nodeLoader, props.filterInfo, props.onFilterApplied);
    // istanbul ignore next
    const viewManager = (_a = props.viewManager) !== null && _a !== void 0 ? _a : core_frontend_1.IModelApp.viewManager;
    const { activeView, allViewports, categoryVisibilityHandler } = props;
    const currentActiveView = activeView !== null && activeView !== void 0 ? activeView : viewManager.getFirstOpenView();
    const categories = (0, CategoryVisibilityHandler_1.useCategories)(viewManager, props.iModel, currentActiveView);
    const visibilityHandler = useCategoryVisibilityHandler(viewManager, props.iModel, categories, currentActiveView, allViewports, categoryVisibilityHandler);
    React.useEffect(() => {
        setViewType(currentActiveView); // eslint-disable-line @typescript-eslint/no-floating-promises
    }, [currentActiveView]);
    const eventHandler = (0, core_react_1.useDisposable)(React.useCallback(() => new VisibilityTreeEventHandler_1.VisibilityTreeEventHandler({
        nodeLoader: filteredNodeLoader,
        visibilityHandler,
        collapsedChildrenDisposalEnabled: true,
    }), [filteredNodeLoader, visibilityHandler]));
    const treeModel = (0, components_react_1.useTreeModel)(filteredNodeLoader.modelSource);
    const treeRenderer = (0, VisibilityTreeRenderer_1.useVisibilityTreeRenderer)(false, true);
    const overlay = isFiltering ? React.createElement("div", { className: "filteredTreeOverlay" }) : undefined;
    const filterApplied = filteredNodeLoader !== nodeLoader;
    const noFilteredDataRenderer = React.useCallback(() => {
        return React.createElement(VisibilityTreeRenderer_1.VisibilityTreeNoFilteredData, { title: UiFramework_1.UiFramework.localization.getLocalizedString("UiFramework:categoriesTree.noCategoryFound"), message: UiFramework_1.UiFramework.localization.getLocalizedString("UiFramework:categoriesTree.noMatchingCategoryNames") });
    }, []);
    return (React.createElement("div", { className: "ui-fw-categories-tree" },
        React.createElement(components_react_1.ControlledTree, { nodeLoader: filteredNodeLoader, model: treeModel, selectionMode: components_react_1.SelectionMode.None, eventsHandler: eventHandler, treeRenderer: treeRenderer, descriptionsEnabled: true, nodeHighlightingProps: nodeHighlightingProps, noDataRenderer: filterApplied ? noFilteredDataRenderer : undefined, width: props.width, height: props.height }),
        overlay));
}
exports.CategoryTree = CategoryTree;
function useCategoryVisibilityHandler(viewManager, imodel, categories, activeView, allViewports, visibilityHandler) {
    return (0, core_react_1.useDisposable)(React.useCallback(() => 
    // istanbul ignore next
    visibilityHandler !== null && visibilityHandler !== void 0 ? visibilityHandler : new CategoryVisibilityHandler_1.CategoryVisibilityHandler({ viewManager, imodel, categories, activeView, allViewports }), [viewManager, imodel, categories, activeView, allViewports, visibilityHandler]));
}
async function setViewType(activeView) {
    if (!activeView)
        return;
    const view = activeView.view;
    const viewType = view.is3d() ? "3d" : "2d";
    await presentation_frontend_1.Presentation.presentation.vars(exports.RULESET_CATEGORIES.id).setString("ViewType", viewType);
}
/**
 * Toggles visibility of categories to show or hide.
 * @alpha
 * @deprecated in 3.6. Was moved to `@itwin/tree-widget-react` package.
 */
async function toggleAllCategories(viewManager, imodel, display, viewport, forAllViewports, filteredProvider) {
    // istanbul ignore next
    const activeView = viewport !== null && viewport !== void 0 ? viewport : viewManager.getFirstOpenView();
    const ids = await getCategories(imodel, activeView, filteredProvider);
    // istanbul ignore else
    if (ids.length > 0) {
        CategoryVisibilityHandler_1.CategoryVisibilityHandler.enableCategory(viewManager, imodel, ids, display, forAllViewports !== null && forAllViewports !== void 0 ? forAllViewports : false);
    }
}
exports.toggleAllCategories = toggleAllCategories;
/**
 * Gets ids of all categories or categories from filtered data provider.
 * @alpha
 * @deprecated in 3.6. Was moved to `@itwin/tree-widget-react` package.
 */
async function getCategories(imodel, viewport, filteredProvider) {
    if (filteredProvider) {
        const nodes = await filteredProvider.getNodes();
        return nodes.map((node) => CategoryVisibilityHandler_1.CategoryVisibilityHandler.getInstanceIdFromTreeNodeKey(filteredProvider.getNodeKey(node)));
    }
    const categories = await (0, CategoryVisibilityHandler_1.loadCategoriesFromViewport)(imodel, viewport);
    return categories.map((category) => category.key);
}
exports.getCategories = getCategories;
//# sourceMappingURL=CategoriesTree.js.map