"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const chai_1 = require("chai");
const bentleyjs_core_1 = require("@bentley/bentleyjs-core");
const imodeljs_common_1 = require("@bentley/imodeljs-common");
const imodeljs_frontend_1 = require("@bentley/imodeljs-frontend");
const frontend_1 = require("@bentley/oidc-signin-tool/lib/frontend");
const presentation_common_1 = require("@bentley/presentation-common");
const presentation_frontend_1 = require("@bentley/presentation-frontend");
const defaultRuleset = require("./rulesets/default.json");
const getRelatedDistinctValues = require("./rulesets/DistinctValues/getRelatedDistinctValues.json");
const getFilteredNodePaths = require("./rulesets/NodePaths/getFilteredNodePaths.json");
const getNodePaths = require("./rulesets/NodePaths/getNodePaths.json");
const TestContext_1 = require("./setup/TestContext");
/* eslint-disable deprecation/deprecation */
describe("PresentationRpcInterface tests", () => {
    let iModel;
    let ruleset;
    let client; // eslint-disable-line @typescript-eslint/no-unused-vars
    before(async function () {
        const testContext = await TestContext_1.TestContext.instance();
        if (!testContext.settings.runPresentationRpcTests)
            this.skip();
        client = imodeljs_common_1.RpcManager.getClientForInterface(presentation_common_1.PresentationRpcInterface);
        await presentation_frontend_1.Presentation.initialize();
        const iModelId = testContext.iModelWithChangesets.iModelId;
        const contextId = testContext.iModelWithChangesets.contextId;
        const accessToken = testContext.adminUserAccessToken;
        imodeljs_frontend_1.IModelApp.authorizationClient = new frontend_1.TestFrontendAuthorizationClient(accessToken);
        iModel = await imodeljs_frontend_1.CheckpointConnection.openRemote(contextId, iModelId);
    });
    it("getNodes works as expected", async () => {
        ruleset = defaultRuleset;
        const props = { imodel: iModel, rulesetOrId: ruleset.id };
        await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async () => {
            const rootNodes1 = await presentation_frontend_1.Presentation.presentation.getNodes(props);
            chai_1.expect(rootNodes1.length).to.be.equal(1);
        });
    });
    it("[deprecated] getNodesAndCount works as expected", async () => {
        ruleset = defaultRuleset;
        await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async () => {
            var _a;
            const options = {
                rulesetOrId: ruleset.id,
            };
            const nodesAndCount = await client.getNodesAndCount(iModel.getRpcProps(), options); // eslint-disable-line deprecation/deprecation
            chai_1.expect((_a = nodesAndCount.result) === null || _a === void 0 ? void 0 : _a.count).to.not.be.undefined;
        });
    });
    it("getNodesAndCount works as expected", async () => {
        ruleset = defaultRuleset;
        const props = { imodel: iModel, rulesetOrId: ruleset.id };
        await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async () => {
            const nodesAndCount = await presentation_frontend_1.Presentation.presentation.getNodesAndCount(props);
            chai_1.expect(nodesAndCount.count).to.not.be.undefined;
        });
    });
    it("getNodesCount works as expected", async () => {
        ruleset = defaultRuleset;
        const props = { imodel: iModel, rulesetOrId: ruleset.id };
        await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async () => {
            const count = await presentation_frontend_1.Presentation.presentation.getNodesCount(props);
            chai_1.expect(count).to.not.be.undefined;
        });
    });
    it("getNodePath works as expected", async () => {
        ruleset = getNodePaths;
        const props = { imodel: iModel, rulesetOrId: ruleset.id };
        await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async () => {
            const key1 = { id: bentleyjs_core_1.Id64.fromString("0x1"), className: "BisCore:RepositoryModel" };
            const key2 = { id: bentleyjs_core_1.Id64.fromString("0x1"), className: "BisCore:Subject" };
            const key3 = { id: bentleyjs_core_1.Id64.fromString("0x10"), className: "BisCore:DefinitionPartition" };
            const key4 = { id: bentleyjs_core_1.Id64.fromString("0xe"), className: "BisCore:LinkPartition" };
            const keys = [[key1, key2, key3], [key1, key2, key4]];
            const result = await presentation_frontend_1.Presentation.presentation.getNodePaths(props, keys, 1);
            chai_1.expect(result).to.not.be.undefined;
        });
    });
    it("getFilteredNodePaths works as expected", async () => {
        ruleset = getFilteredNodePaths;
        const props = { imodel: iModel, rulesetOrId: ruleset.id };
        await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
            const result = await presentation_frontend_1.Presentation.presentation.getFilteredNodePaths(props, "filter");
            chai_1.expect(result).to.not.be.undefined;
        });
    });
    it("loadHierarchy works as expected", async () => {
        ruleset = defaultRuleset;
        const props = { imodel: iModel, rulesetOrId: ruleset.id };
        await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async () => {
            let success = true;
            try {
                await presentation_frontend_1.Presentation.presentation.loadHierarchy(props);
            }
            catch (ex) {
                success = false;
            }
            chai_1.expect(success).to.be.true;
        });
    });
    describe("tests that require a descriptor", async () => {
        let descriptor;
        let props;
        const key1 = { id: bentleyjs_core_1.Id64.fromString("0x1"), className: "BisCore:Subject" };
        const key2 = { id: bentleyjs_core_1.Id64.fromString("0x17"), className: "BisCore:SpatialCategory" };
        const keys = new presentation_common_1.KeySet([key1, key2]);
        before(async function () {
            // Needed in all nested describes.  See https://github.com/mochajs/mocha/issues/2683.
            const testContext = await TestContext_1.TestContext.instance();
            if (!testContext.settings.runPresentationRpcTests)
                this.skip();
            ruleset = getRelatedDistinctValues;
            props = { imodel: iModel, rulesetOrId: ruleset.id };
        });
        it("getContentDescriptor works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                descriptor = await presentation_frontend_1.Presentation.presentation.getContentDescriptor(props, "Grid", keys, undefined);
                chai_1.expect(descriptor).to.not.be.undefined;
            });
        });
        it("getDistinctValues works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const distinctValues = await presentation_frontend_1.Presentation.presentation.getDistinctValues(props, descriptor, keys, "SubCategory_DefinitionPartition_LinkPartition_PhysicalPartition_Model");
                chai_1.expect(distinctValues).to.not.be.undefined;
            });
        });
        it("[deprecated] getDistinctValues works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const options = {
                    rulesetOrId: ruleset.id,
                    descriptor: descriptor,
                    fieldDescriptor: {
                        type: presentation_common_1.FieldDescriptorType.Name,
                        fieldName: "SubCategory_DefinitionPartition_LinkPartition_PhysicalPartition_Model",
                    },
                    keys: keys.toJSON(),
                };
                const distinctValues = await client.getPagedDistinctValues(iModel.getRpcProps(), options);
                chai_1.expect(distinctValues).to.not.be.undefined;
            });
        });
        it("getContentAndSize works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const options = {
                    rulesetOrId: ruleset.id,
                };
                const contentAndSize = await client.getContentAndSize(iModel.getRpcProps(), options, descriptor, keys.toJSON()); // eslint-disable-line deprecation/deprecation
                chai_1.expect(contentAndSize).to.not.be.undefined;
            });
        });
        it("getContentAndSize works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const contentAndSize = await presentation_frontend_1.Presentation.presentation.getContentAndSize(props, descriptor, keys);
                chai_1.expect(contentAndSize).to.not.be.undefined;
            });
        });
        it("getContent works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const content = await presentation_frontend_1.Presentation.presentation.getContent(props, descriptor, keys);
                chai_1.expect(content).to.not.be.undefined;
            });
        });
        it("getContentSetSize works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const contentSetSize = await presentation_frontend_1.Presentation.presentation.getContentSetSize(props, descriptor, keys);
                chai_1.expect(contentSetSize).to.not.be.undefined;
            });
        });
        it("getElementProperties works as expected", async () => {
            const result = await client.getElementProperties(iModel.getRpcProps(), {
                elementId: "0x1",
            });
            chai_1.expect(result).to.not.be.undefined;
        });
        it("getDisplayLabelDefinition works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const displayLabel = await presentation_frontend_1.Presentation.presentation.getDisplayLabelDefinition(props, key1);
                chai_1.expect(displayLabel).to.not.be.undefined;
            });
        });
        it("getDisplayLabelDefinitions works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const displayLabels = await presentation_frontend_1.Presentation.presentation.getDisplayLabelDefinitions(props, [key1, key2]);
                chai_1.expect(displayLabels).to.not.be.undefined;
            });
        });
        it("[deprecated] getDisplayLabelDefinitions works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const displayLabels = await client.getDisplayLabelDefinitions(iModel.getRpcProps(), {}, [key1, key2]); // eslint-disable-line deprecation/deprecation
                chai_1.expect(displayLabels).to.not.be.undefined;
            });
        });
        it("getSelectionScopes works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const scopeIds = await presentation_frontend_1.Presentation.selection.scopes.getSelectionScopes(iModel);
                chai_1.expect(scopeIds).to.not.be.undefined;
            });
        });
        it("computeSelection works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const computedSelections = await presentation_frontend_1.Presentation.selection.scopes.computeSelection(iModel, [key1.id, key2.id], "element");
                chai_1.expect(computedSelections).to.not.be.undefined;
            });
        });
        it("[deprecated] comparHierarchies works as expected", async () => {
            await bentleyjs_core_1.using(await presentation_frontend_1.Presentation.presentation.rulesets().add(ruleset), async (_r) => {
                const options = {
                    prev: {
                        rulesetOrId: ruleset.id,
                    },
                    rulesetOrId: ruleset.id,
                    expandedNodeKeys: [],
                };
                const comparison = await client.compareHierarchies(iModel.getRpcProps(), options);
                chai_1.expect(comparison).to.not.be.undefined;
                chai_1.expect(comparison.statusCode).eq(presentation_common_1.PresentationStatus.Success);
            });
        });
    });
});
//# sourceMappingURL=PresentationRpc.test.js.map