"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 ChangedElementsDb
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChangedElementsDb = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const BriefcaseManager_1 = require("./BriefcaseManager");
const ECDb_1 = require("./ECDb");
const IModelHost_1 = require("./IModelHost");
const NativePlatform_1 = require("./internal/NativePlatform");
const Symbols_1 = require("./internal/Symbols");
/** An ChangedElementsDb file
 * @internal
 */
class ChangedElementsDb {
    _nativeDb;
    constructor() {
        this._nativeDb = new NativePlatform_1.IModelNative.platform.ChangedElementsECDb();
    }
    [Symbol.dispose]() {
        if (!this._nativeDb)
            return;
        this.closeDb();
        this._nativeDb.dispose();
        this._nativeDb = undefined;
    }
    /** Create a ChangedElementsDb
     * @param pathName The path to the ECDb file to create.
     * @throws [IModelError]($common) if the operation failed.
     */
    _createDb(briefcase, pathName) {
        const status = this.nativeDb.createDb(briefcase[Symbols_1._nativeDb], pathName);
        if (status !== core_bentley_1.DbResult.BE_SQLITE_OK)
            throw new core_common_1.IModelError(status, "Failed to created ECDb");
    }
    /** Open the Changed Elements Db.
     * @param pathName The path to the ECDb file to open
     * @param openMode Open mode
     * @throws [IModelError]($common) if the operation failed.
     */
    _openDb(pathName, openMode = ECDb_1.ECDbOpenMode.Readonly) {
        const nativeOpenMode = openMode === ECDb_1.ECDbOpenMode.Readonly ? core_bentley_1.OpenMode.Readonly : core_bentley_1.OpenMode.ReadWrite;
        const tryUpgrade = openMode === ECDb_1.ECDbOpenMode.FileUpgrade;
        const status = this.nativeDb.openDb(pathName, nativeOpenMode, tryUpgrade);
        if (status !== core_bentley_1.DbResult.BE_SQLITE_OK)
            throw new core_common_1.IModelError(status, "Failed to open ECDb");
    }
    /** Open the Changed Elements Db.
     * @param pathName The path to the ECDb file to open
     * @param openMode Open mode
     * @returns ChangedElementsDb
     */
    static openDb(pathName, openMode = ECDb_1.ECDbOpenMode.Readonly) {
        const cacheDb = new ChangedElementsDb();
        cacheDb._openDb(pathName, openMode);
        return cacheDb;
    }
    /** Create the changed elements cache db
     * @param briefcase IModelDb to use
     * @param pathName The path to the ECDb file to create.
     * @returns The new cache db
     */
    static createDb(briefcase, pathName) {
        const cacheDb = new ChangedElementsDb();
        cacheDb._createDb(briefcase, pathName);
        return cacheDb;
    }
    /** Processes a range of changesets and adds it to the changed elements cache
     * @param briefcase iModel briefcase to use
     * @param options Options for processing
     */
    async processChangesets(accessToken, briefcase, options) {
        const iModelId = briefcase.iModelId;
        const first = (await IModelHost_1.IModelHost[Symbols_1._hubAccess].queryChangeset({ iModelId, changeset: { id: options.startChangesetId }, accessToken })).index;
        const end = (await IModelHost_1.IModelHost[Symbols_1._hubAccess].queryChangeset({ iModelId, changeset: { id: options.endChangesetId }, accessToken })).index;
        const changesets = await IModelHost_1.IModelHost[Symbols_1._hubAccess].downloadChangesets({ accessToken, iModelId, range: { first, end }, targetDir: BriefcaseManager_1.BriefcaseManager.getChangeSetsPath(iModelId) });
        // ChangeSets need to be processed from newest to oldest
        changesets.reverse();
        const status = this.nativeDb.processChangesets(briefcase[Symbols_1._nativeDb], changesets, options.rulesetId, options.filterSpatial, options.wantParents, options.wantPropertyChecksums, options.rulesetDir, options.tempDir, options.wantChunkTraversal);
        if (status !== core_bentley_1.DbResult.BE_SQLITE_OK)
            throw new core_common_1.IModelError(status, "Failed to process changesets");
        return status;
    }
    /** Processes a range of changesets and adds it to the changed elements cache
     * This call will close the IModelDb object as it is required for processing and applying changesets
     * @param briefcase iModel briefcase to use
     * @param options options for processing
     */
    async processChangesetsAndRoll(accessToken, briefcase, options) {
        const iModelId = briefcase.iModelId;
        const first = (await IModelHost_1.IModelHost[Symbols_1._hubAccess].queryChangeset({ iModelId, changeset: { id: options.startChangesetId }, accessToken })).index;
        const end = (await IModelHost_1.IModelHost[Symbols_1._hubAccess].queryChangeset({ iModelId, changeset: { id: options.endChangesetId }, accessToken })).index;
        const changesets = await IModelHost_1.IModelHost[Symbols_1._hubAccess].downloadChangesets({ accessToken, iModelId, range: { first, end }, targetDir: BriefcaseManager_1.BriefcaseManager.getChangeSetsPath(iModelId) });
        // ChangeSets need to be processed from newest to oldest
        changesets.reverse();
        // Close briefcase before doing processing and rolling briefcase
        const dbFilename = briefcase.pathName;
        const dbGuid = briefcase.iModelId;
        briefcase.close();
        // Process changesets
        const status = this.nativeDb.processChangesetsAndRoll(dbFilename, dbGuid, changesets, options.rulesetId, options.filterSpatial, options.wantParents, options.wantPropertyChecksums, options.rulesetDir, options.tempDir, options.wantRelationshipCaching, options.relationshipCacheSize, options.wantChunkTraversal, options.wantBoundingBoxes);
        if (status !== core_bentley_1.DbResult.BE_SQLITE_OK)
            throw new core_common_1.IModelError(status, "Failed to process changesets");
        return status;
    }
    /** Get changed elements between two changesets
     * @param startChangesetId Start Changeset Id
     * @param endChangesetId End Changeset Id
     * @returns Returns the changed elements between the changesets provided
     * @throws [IModelError]($common) if the operation failed.
     */
    getChangedElements(startChangesetId, endChangesetId) {
        const result = this.nativeDb.getChangedElements(startChangesetId, endChangesetId);
        if (result.error || !result.result)
            throw new core_common_1.IModelError(result.error ? result.error.status : -1, result.error ? result.error.message : "Problem getting changed elements");
        return (result.result.changedElements);
    }
    /** Get changed models between two changesets
     * @param startChangesetId Start Changeset Id
     * @param endChangesetId End Changeset Id
     * @returns Returns the changed models between the changesets provided
     * @throws [IModelError]($common) if the operation failed.
     */
    getChangedModels(startChangesetId, endChangesetId) {
        const result = this.nativeDb.getChangedElements(startChangesetId, endChangesetId);
        if (result.error || !result.result)
            throw new core_common_1.IModelError(result.error ? result.error.status : -1, result.error ? result.error.message : "Problem getting changed models");
        return (result.result.changedModels);
    }
    /** Get changed models between two changesets
     * @param startChangesetId Start Changeset Id
     * @param endChangesetId End Changeset Id
     * @returns Returns the changed models between the changesets provided
     * @throws [IModelError]($common) if the operation failed.
     */
    getChangeData(startChangesetId, endChangesetId) {
        const result = this.nativeDb.getChangedElements(startChangesetId, endChangesetId);
        if (result.error)
            throw new core_common_1.IModelError(result.error.status, result.error.message);
        return result.result;
    }
    /** Returns true if the Changed Elements Db is open */
    get isOpen() { return this.nativeDb.isOpen(); }
    /** Returns true if the cache already contains this changeset Id */
    isProcessed(changesetId) { return this.nativeDb.isProcessed(changesetId); }
    /** Close the Db after saving any uncommitted changes.
     * @throws [IModelError]($common) if the database is not open.
     */
    closeDb() {
        this.nativeDb.closeDb();
    }
    cleanCaches() {
        this.nativeDb.cleanCaches();
    }
    /** @internal */
    get nativeDb() {
        if (!this._nativeDb)
            throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "ChangedElementsDb object has already been disposed.");
        return this._nativeDb;
    }
}
exports.ChangedElementsDb = ChangedElementsDb;
//# sourceMappingURL=ChangedElementsDb.js.map