/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module IModelConnection
 */
import { IModelError } from "@itwin/core-common";
import { BriefcaseConnection } from "./BriefcaseConnection";
import { IModelStatus, OpenMode } from "@itwin/core-bentley";
import { NativeApp } from "./NativeApp";
/** @beta */
export var CatalogConnection;
(function (CatalogConnection) {
    /** Create a new [BlobContainer]($backend) to hold versions of a [CatalogIModel]($common).
     * @returns The properties of the newly created container.
     * @note creating new containers requires "admin" authorization.
    */
    async function createNewContainer(args) {
        return NativeApp.catalogIpc.createNewContainer(args);
    }
    CatalogConnection.createNewContainer = createNewContainer;
    /** Acquire the write lock for a CatalogIModel container. Only one person may obtain the write lock at a time.
     * @note this requires "write" authorization to the container
    */
    async function acquireWriteLock(args) {
        return NativeApp.catalogIpc.acquireWriteLock(args);
    }
    CatalogConnection.acquireWriteLock = acquireWriteLock;
    /** Release the write lock on a CatalogIModel container. This uploads all changes made while the lock is held, so they become visible to other users. */
    async function releaseWriteLock(args) {
        return NativeApp.catalogIpc.releaseWriteLock(args);
    }
    CatalogConnection.releaseWriteLock = releaseWriteLock;
    /**
     * Create a new version of a CatalogIModel as a copy of an existing version. Immediately after this operation, the new version will be an exact copy
     * of the source CatalogIModel. Then, use [[openEditable]] to modify the new version with new content.
     * @note the write lock must be held for this operation to succeed
     * @see [[acquireWriteLock]]
     */
    async function createNewVersion(args) {
        return NativeApp.catalogIpc.createNewVersion(args);
    }
    CatalogConnection.createNewVersion = createNewVersion;
    /** Open a CatalogIModel for read access.
     * @returns the [[CatalogConnection]] to access the contents of the Catalog.
     * @note CatalogConnection extends BriefcaseConnection. When finished reading, call `close` on the connection.
     */
    async function openReadonly(args) {
        const openResponse = await NativeApp.catalogIpc.openReadonly(args);
        const connection = new CatalogConnectionImpl(openResponse, OpenMode.Readonly);
        BriefcaseConnection.onOpen.raiseEvent(connection);
        return connection;
    }
    CatalogConnection.openReadonly = openReadonly;
    /** Open a CatalogIModel for write access.
     * @note Once a version of a CatalogIModel has been published (i.e. the write lock has been released), it is no longer editable, *unless* it is a prerelease version.
     * @note the write lock must be held for this operation to succeed
     */
    async function openEditable(args) {
        const openResponse = await NativeApp.catalogIpc.openEditable(args);
        const connection = new EditableCatalogConnectionImpl(openResponse, OpenMode.ReadWrite);
        BriefcaseConnection.onOpen.raiseEvent(connection);
        return connection;
    }
    CatalogConnection.openEditable = openEditable;
})(CatalogConnection || (CatalogConnection = {}));
class CatalogConnectionImpl extends BriefcaseConnection {
    constructor(props, openMode) {
        super(props, openMode);
    }
    requireTimeline() {
        throw new IModelError(IModelStatus.WrongIModel, "Catalogs have no timeline");
    }
    /** Get the manifest and version information for an open CatalogConnection. */
    async getCatalogInfo() {
        return NativeApp.catalogIpc.getInfo(this.key);
    }
    isEditable() {
        return false;
    }
}
class EditableCatalogConnectionImpl extends CatalogConnectionImpl {
    /** Update the contents of the manifest in a CatalogIModel that is open with [[openEditable]]. */
    async updateManifest(manifest) {
        return NativeApp.catalogIpc.updateCatalogManifest(this.key, manifest);
    }
    isEditable() {
        return true;
    }
}
//# sourceMappingURL=CatalogConnection.js.map