/*---------------------------------------------------------------------------------------------
* 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
 */
/* eslint-disable deprecation/deprecation */
import "./CategoriesTree.scss";
import * as React from "react";
import { IModelApp } from "@itwin/core-frontend";
import { usePresentationTreeNodeLoader } from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
import { ControlledTree, SelectionMode, useTreeModel } from "@itwin/components-react";
import { useDisposable } from "@itwin/core-react";
import { UiFramework } from "../../UiFramework";
import { VisibilityTreeEventHandler } from "../VisibilityTreeEventHandler";
import { useVisibilityTreeFiltering, useVisibilityTreeRenderer, VisibilityTreeNoFilteredData } from "../VisibilityTreeRenderer";
import { CategoryVisibilityHandler, loadCategoriesFromViewport, useCategories } from "./CategoryVisibilityHandler";
const PAGING_SIZE = 20;
/**
 * Presentation rules used by ControlledCategoriesTree
 * @internal
 */
export const 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.
 */
export function CategoryTree(props) {
    var _a;
    const { nodeLoader } = usePresentationTreeNodeLoader({
        imodel: props.iModel,
        ruleset: RULESET_CATEGORIES,
        pagingSize: PAGING_SIZE,
    });
    const { filteredNodeLoader, isFiltering, nodeHighlightingProps } = useVisibilityTreeFiltering(nodeLoader, props.filterInfo, props.onFilterApplied);
    // istanbul ignore next
    const viewManager = (_a = props.viewManager) !== null && _a !== void 0 ? _a : IModelApp.viewManager;
    const { activeView, allViewports, categoryVisibilityHandler } = props;
    const currentActiveView = activeView !== null && activeView !== void 0 ? activeView : viewManager.getFirstOpenView();
    const categories = 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 = useDisposable(React.useCallback(() => new VisibilityTreeEventHandler({
        nodeLoader: filteredNodeLoader,
        visibilityHandler,
        collapsedChildrenDisposalEnabled: true,
    }), [filteredNodeLoader, visibilityHandler]));
    const treeModel = useTreeModel(filteredNodeLoader.modelSource);
    const treeRenderer = useVisibilityTreeRenderer(false, true);
    const overlay = isFiltering ? React.createElement("div", { className: "filteredTreeOverlay" }) : undefined;
    const filterApplied = filteredNodeLoader !== nodeLoader;
    const noFilteredDataRenderer = React.useCallback(() => {
        return React.createElement(VisibilityTreeNoFilteredData, { title: UiFramework.localization.getLocalizedString("UiFramework:categoriesTree.noCategoryFound"), message: UiFramework.localization.getLocalizedString("UiFramework:categoriesTree.noMatchingCategoryNames") });
    }, []);
    return (React.createElement("div", { className: "ui-fw-categories-tree" },
        React.createElement(ControlledTree, { nodeLoader: filteredNodeLoader, model: treeModel, selectionMode: SelectionMode.None, eventsHandler: eventHandler, treeRenderer: treeRenderer, descriptionsEnabled: true, nodeHighlightingProps: nodeHighlightingProps, noDataRenderer: filterApplied ? noFilteredDataRenderer : undefined, width: props.width, height: props.height }),
        overlay));
}
function useCategoryVisibilityHandler(viewManager, imodel, categories, activeView, allViewports, visibilityHandler) {
    return useDisposable(React.useCallback(() => 
    // istanbul ignore next
    visibilityHandler !== null && visibilityHandler !== void 0 ? visibilityHandler : new 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.presentation.vars(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.
 */
export 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.enableCategory(viewManager, imodel, ids, display, forAllViewports !== null && forAllViewports !== void 0 ? forAllViewports : false);
    }
}
/**
 * Gets ids of all categories or categories from filtered data provider.
 * @alpha
 * @deprecated in 3.6. Was moved to `@itwin/tree-widget-react` package.
 */
export async function getCategories(imodel, viewport, filteredProvider) {
    if (filteredProvider) {
        const nodes = await filteredProvider.getNodes();
        return nodes.map((node) => CategoryVisibilityHandler.getInstanceIdFromTreeNodeKey(filteredProvider.getNodeKey(node)));
    }
    const categories = await loadCategoriesFromViewport(imodel, viewport);
    return categories.map((category) => category.key);
}
//# sourceMappingURL=CategoriesTree.js.map