"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 Elements
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createChannelControl = createChannelControl;
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const ChannelControl_1 = require("../ChannelControl");
const Element_1 = require("../Element");
const IModelHost_1 = require("../IModelHost");
const Symbols_1 = require("./Symbols");
class ChannelAdmin {
    _iModel;
    static channelClassName = "bis:ChannelRootAspect";
    [Symbols_1._implementationProhibited] = undefined;
    _allowedChannels = new Set();
    _allowedModels = new Set();
    _deniedModels = new Map();
    constructor(_iModel) {
        this._iModel = _iModel;
        // for backwards compatibility, allow the shared channel unless explicitly turned off in IModelHostOptions.
        if (IModelHost_1.IModelHost.configuration?.allowSharedChannel !== false)
            this._allowedChannels.add(ChannelControl_1.ChannelControl.sharedChannelName);
    }
    addAllowedChannel(channelKey) {
        this._allowedChannels.add(channelKey);
        this._deniedModels.clear();
    }
    removeAllowedChannel(channelKey) {
        this._allowedChannels.delete(channelKey);
        this._allowedModels.clear();
    }
    getChannelKey(elementId) {
        if (elementId === core_common_1.IModel.rootSubjectId)
            return ChannelControl_1.ChannelControl.sharedChannelName;
        try {
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            const channel = this._iModel.withPreparedStatement(`SELECT Owner FROM ${ChannelAdmin.channelClassName} WHERE Element.Id=?`, (stmt) => {
                stmt.bindId(1, elementId);
                return core_bentley_1.DbResult.BE_SQLITE_ROW === stmt.step() ? stmt.getValue(0).getString() : undefined;
            });
            if (channel !== undefined)
                return channel;
        }
        catch {
            // Exception happens if the iModel is too old: ChannelRootAspect class not present in the BisCore schema (older than v1.0.10).
            // In that case all data in such iModel is assumed to be in the shared channel.
            return ChannelControl_1.ChannelControl.sharedChannelName;
        }
        const parentId = this._iModel.withPreparedSqliteStatement("SELECT ParentId,ModelId FROM bis_Element WHERE id=?", (stmt) => {
            stmt.bindId(1, elementId);
            if (core_bentley_1.DbResult.BE_SQLITE_ROW !== stmt.step())
                throw new core_common_1.IModelError(core_bentley_1.IModelStatus.NotFound, "Element does not exist");
            return stmt.getValueId(0) ?? stmt.getValueId(1); // if parent is undefined, use modelId
        });
        return this.getChannelKey(parentId);
    }
    [Symbols_1._verifyChannel](modelId) {
        // Note: indirect changes are permitted to change any channel
        if (this._allowedModels.has(modelId) || this._iModel[Symbols_1._nativeDb].getTxnMode() === "indirect")
            return;
        const deniedChannel = this._deniedModels.get(modelId);
        if (undefined !== deniedChannel)
            core_common_1.ChannelControlError.throwError("not-allowed", `Channel ${deniedChannel} is not allowed`, deniedChannel);
        const channel = this.getChannelKey(modelId);
        if (this._allowedChannels.has(channel)) {
            this._allowedModels.add(modelId);
            return;
        }
        this._deniedModels.set(modelId, channel);
        return this[Symbols_1._verifyChannel](modelId);
    }
    makeChannelRoot(args) {
        const channelKey = this.getChannelKey(args.elementId);
        if (ChannelControl_1.ChannelControl.sharedChannelName !== channelKey)
            core_common_1.ChannelControlError.throwError("may-not-nest", `Channel ${channelKey} may not nest`, channelKey);
        if (this.queryChannelRoot(args.channelKey) !== undefined)
            core_common_1.ChannelControlError.throwError("root-exists", `Channel ${args.channelKey} root already exist`, channelKey);
        const props = { classFullName: ChannelAdmin.channelClassName, element: { id: args.elementId }, owner: args.channelKey };
        this._iModel.elements.insertAspect(props);
    }
    insertChannelSubject(args) {
        // Check if channelKey already exists before inserting Subject.
        // makeChannelRoot will check that again, but at that point the new Subject is already inserted.
        // Prefer to check twice instead of deleting the Subject in the latter option.
        if (this.queryChannelRoot(args.channelKey) !== undefined)
            core_common_1.ChannelControlError.throwError("root-exists", `Channel ${args.channelKey} root already exist`, args.channelKey);
        const elementId = Element_1.Subject.insert(this._iModel, args.parentSubjectId ?? core_common_1.IModel.rootSubjectId, args.subjectName, args.description);
        this.makeChannelRoot({ elementId, channelKey: args.channelKey });
        return elementId;
    }
    queryChannelRoot(channelKey) {
        if (channelKey === ChannelControl_1.ChannelControl.sharedChannelName)
            // RootSubject acts as the ChannelRoot element of the shared channel
            return core_common_1.IModel.rootSubjectId;
        try {
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            const channelRoot = this._iModel.withPreparedStatement(`SELECT Element.Id FROM ${ChannelAdmin.channelClassName} WHERE Owner=?`, (stmt) => {
                stmt.bindString(1, channelKey);
                return core_bentley_1.DbResult.BE_SQLITE_ROW === stmt.step() ? stmt.getValue(0).getId() : undefined;
            });
            return channelRoot;
        }
        catch {
            // Exception happens if the iModel is too old: ChannelRootAspect class not present in the BisCore schema (older than v1.0.10).
            // In that case all data in such iModel is assumed to be in the shared channel.
            return undefined;
        }
    }
}
function createChannelControl(iModel) {
    return new ChannelAdmin(iModel);
}
//# sourceMappingURL=ChannelAdmin.js.map