/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module iModels
 */
import { DbChangeStage, DbConflictCause, DbOpcode, DbValueType } from "@itwin/core-bentley";
import { SqliteChangesetReader } from "../SqliteChangesetReader";
export class RebaseChangesetConflictArgs {
    _dbConflictArg;
    _iModel;
    constructor(_dbConflictArg, _iModel) {
        this._dbConflictArg = _dbConflictArg;
        this._iModel = _iModel;
    }
    get cause() {
        switch (this._dbConflictArg.cause) {
            case DbConflictCause.Conflict: return "Conflict";
            case DbConflictCause.Data: return "Data";
            case DbConflictCause.Constraint: return "Constraint";
            case DbConflictCause.ForeignKey: return "ForeignKey";
            case DbConflictCause.NotFound: return "NotFound";
        }
        throw new Error("Invalid value for cause");
    }
    get opcode() {
        switch (this._dbConflictArg.opcode) {
            case DbOpcode.Insert: return "Inserted";
            case DbOpcode.Update: return "Updated";
            case DbOpcode.Delete: return "Deleted";
        }
    }
    openTxn() {
        return SqliteChangesetReader.openTxn({ txnId: this._dbConflictArg.txn.id, db: this._iModel });
    }
    get indirect() {
        return this._dbConflictArg.indirect;
    }
    get tableName() {
        return this._dbConflictArg.tableName;
    }
    get columnCount() {
        return this._dbConflictArg.columnCount;
    }
    getForeignKeyConflicts() {
        return this._dbConflictArg.getForeignKeyConflicts();
    }
    setLastError(message) {
        this._dbConflictArg.setLastError(message);
    }
    getPrimaryKeyColumns() {
        return this._dbConflictArg.getPrimaryKeyColumns();
    }
    getValueType(columnIndex, stage) {
        return this._dbConflictArg.getValueType(columnIndex, stage === "New" ? DbChangeStage.New : DbChangeStage.Old);
    }
    getValueBinary(columnIndex, stage) {
        return this._dbConflictArg.getValueBinary(columnIndex, stage === "New" ? DbChangeStage.New : DbChangeStage.Old);
    }
    getValueId(columnIndex, stage) {
        return this._dbConflictArg.getValueId(columnIndex, stage === "New" ? DbChangeStage.New : DbChangeStage.Old);
    }
    getValueText(columnIndex, stage) {
        return this._dbConflictArg.getValueText(columnIndex, stage === "New" ? DbChangeStage.New : DbChangeStage.Old);
    }
    getValueInteger(columnIndex, stage) {
        return this._dbConflictArg.getValueInteger(columnIndex, stage === "New" ? DbChangeStage.New : DbChangeStage.Old);
    }
    getValueDouble(columnIndex, stage) {
        return this._dbConflictArg.getValueDouble(columnIndex, stage === "New" ? DbChangeStage.New : DbChangeStage.Old);
    }
    isValueNull(columnIndex, stage) {
        return this._dbConflictArg.isValueNull(columnIndex, stage === "New" ? DbChangeStage.New : DbChangeStage.Old);
    }
    getColumnNames() {
        return this._dbConflictArg.getColumnNames();
    }
    getPrimaryKeyValues() {
        const pkv = [];
        if (this.opcode === undefined)
            return pkv;
        const stage = this._dbConflictArg.opcode === DbOpcode.Insert ? DbChangeStage.New : DbChangeStage.Old;
        for (const pk of this._dbConflictArg.getPrimaryKeyColumns()) {
            const type = this._dbConflictArg.getValueType(pk, stage);
            if (type === DbValueType.IntegerVal)
                pkv.push(this._dbConflictArg.getValueId(pk, stage));
            else if (type === DbValueType.TextVal)
                pkv.push(this._dbConflictArg.getValueText(pk, stage));
            else if (type === DbValueType.FloatVal)
                pkv.push(this._dbConflictArg.getValueDouble(pk, stage));
            else if (type === DbValueType.BlobVal)
                pkv.push(this._dbConflictArg.getValueBinary(pk, stage));
            else
                pkv.push(null);
        }
        return pkv;
    }
    ;
    get txn() {
        return this._dbConflictArg.txn;
    }
}
//# sourceMappingURL=ChangesetConflictArgs.js.map