/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { BaseTransactionService } from "./BaseTransactionService";
import { List$1 } from "./List$1";
import { TransactionHistory } from "./TransactionHistory";
import { Base, fromEnum, markType } from "./type";
import { Transaction } from "./Transaction";
import { TransactionCommit } from "./TransactionCommit";
import { Dictionary$2 } from "./Dictionary$2";
import { TransactionState } from "./TransactionState";
import { TransactionType } from "./TransactionType";
import { InvalidOperationException } from "./InvalidOperationException";
/**
 * @hidden
 */
export class TransactionService extends BaseTransactionService {
    constructor(a, b, c) {
        super(a, b, c);
        this.am = new List$1(Transaction.$, 0);
        this.ak = new List$1(TransactionCommit.$, 0);
        this.ai = null;
        this.an = new List$1(TransactionHistory.$, 0);
        this.al = new List$1(TransactionHistory.$, 0);
        this.aj = null;
        this.aj = c;
        if (this.aj != null) {
            this.ai = new Dictionary$2(Base.$, TransactionState.$, 2, this.aj);
        }
        else {
            this.ai = new Dictionary$2(Base.$, TransactionState.$, 0);
        }
    }
    get_l() {
        return true;
    }
    set_l(a) {
        super.set_l(a);
    }
    get_k() {
        return this.an.count > 0;
    }
    get_j() {
        return this.al.count > 0;
    }
    af(a, b) {
        let c = null;
        if (a.containsKey(b.id)) {
            c = a.item(b.id);
        }
        if (c != null) {
            switch (b.type) {
                case TransactionType.Delete:
                    if (c.transactionType == 0) {
                        a.removeItem(b.id);
                    }
                    else if (c.transactionType == 1) {
                        c.value = null;
                        c.transactionType = TransactionType.Delete;
                    }
                    break;
                case 1:
                    if (c.transactionType == 0 || c.transactionType == 1) {
                        this.h(c.value, b.value);
                    }
                    break;
            }
        }
        else {
            a.addItem(b.id, new TransactionState(b.id, b.type, this.r(b.value)));
        }
    }
    aq(a, b) {
        let c = null;
        if (a.containsKey(b.id)) {
            c = a.item(b.id);
        }
        switch (b.type) {
            case 0:
                if (c != null) {
                    throw new InvalidOperationException(1, "Cannot add this transaction. A transaction with this ID already exists.");
                }
                break;
            case 1:
            case TransactionType.Delete:
                if (c != null && c.transactionType == TransactionType.Delete) {
                    throw new InvalidOperationException(1, "Cannot add this transaction. A transaction with this ID has already been deleted.");
                }
                break;
        }
    }
    z(a, b, c) {
        super.z(a, b, c);
        switch (a) {
            case "IsEnabled":
                this.w(null);
                break;
        }
    }
    v(a, b = false) {
        let c = (this.m || b) ? this.o : this.ai;
        let d = (this.m || b) ? this.q : this.am;
        this.aq(c, a);
        this.af(c, a);
        d.add(a);
        a.transactionID = TransactionService.ao++;
        if (!this.m && !b) {
            let e = new TransactionHistory();
            e.transactions.add(a);
            this.an.insert(0, e);
            this.al.clear();
            this.aa(3, e);
        }
    }
    w(a = null) {
        if (a != null) {
            for (let b = 0; b < this.am.count; b++) {
                if (this.am._inner[b].id == a) {
                    this.am.removeAt(b);
                    b--;
                }
            }
            this.ai.removeItem(a);
            for (let c = 0; c < this.an.count; c++) {
                for (let d = 0; d < this.an._inner[c].transactions.count; d++) {
                    if (this.an._inner[c].transactions._inner[d].id == a) {
                        this.an._inner[c].transactions.removeAt(d);
                        d--;
                    }
                }
                if (this.an._inner[c].transactions.count == 0) {
                    this.an.removeAt(c);
                    c--;
                }
            }
        }
        else {
            if (this.aj != null) {
                this.ai = new Dictionary$2(Base.$, TransactionState.$, 2, this.aj);
            }
            else {
                this.ai = new Dictionary$2(Base.$, TransactionState.$, 0);
            }
            this.am.clear();
            this.an.clear();
        }
        this.al.clear();
        super.w(a);
    }
    g(a) {
        let b = this.m ? this.o : this.ai;
        if (b.count > 0 && b.containsKey(a)) {
            return b.item(a);
        }
        return null;
    }
    ah(a) {
        return super.g(a);
    }
    ag(a) {
        for (let b = this.ak.count - 1; b >= 0; b--) {
            if (this.ak._inner[b].b.containsKey(a)) {
                return this.ak._inner[b].b.item(a);
            }
        }
        return null;
    }
    y(a) {
        this.m = false;
        if (a) {
            let b = new TransactionHistory();
            for (let c = 0; c < this.q.count; c++) {
                let d = this.q._inner[c];
                let e = this.o.item(d.id);
                this.am.add(d);
                this.af(this.ai, d);
                b.transactions.add(d);
            }
            this.an.insert(0, b);
            this.al.clear();
        }
        super.y(a);
    }
    ap(a) {
        for (let b = 0; b < this.q.count; b++) {
            let c = this.q._inner[b];
            if (c.transactionID == a) {
                this.am.add(c);
                this.af(this.ai, c);
                this.q.removeAt(b);
                this.o.clear();
                for (let d = 0; d < this.q.count; d++) {
                    this.af(this.o, this.q._inner[d]);
                }
                let e = new TransactionHistory();
                e.transactions.add(c);
                this.an.insert(0, e);
                this.al.clear();
                break;
            }
        }
    }
    s(a) {
        let b = -1;
        if (this.ai.count > 0) {
            let c = new TransactionCommit();
            c.id = TransactionService.ao++;
            c.a = this.a(-1);
            c.b = this.ai;
            c.d = -1;
            if (a) {
                c.d = this.ak.count;
                this.ak.add(c);
            }
            else {
                this.aa(5, c);
            }
            b = c.id;
        }
        this.w(null);
        return b;
    }
    u(a) {
        let b = this.e(a);
        if (b != null) {
            this.aa(5, b);
            this.ak.removeAt(b.d);
        }
    }
    ac(a) {
        let b = this.e(a);
        if (b != null) {
            this.ak.removeAt(b.d);
        }
    }
    ae() {
        if (this.k) {
            let a = this.an._inner[0];
            this.an.removeAt(0);
            let b = this.am.count - a.transactions.count;
            this.am.v(b, a.transactions.count);
            this.al.insert(0, a);
            this.ai.clear();
            for (let c = 0; c < this.an.count; c++) {
                for (let d = 0; d < this.an._inner[c].transactions.count; d++) {
                    this.af(this.ai, this.an._inner[c].transactions._inner[d]);
                }
            }
            this.aa(0, a);
        }
    }
    ab() {
        if (this.j) {
            let a = this.al._inner[0];
            this.al.removeAt(0);
            for (let b = 0; b < a.transactions.count; b++) {
                this.af(this.ai, a.transactions._inner[b]);
                this.am.add(a.transactions._inner[b]);
            }
            this.an.insert(0, a);
            this.aa(1, a);
        }
    }
    a(a = -1) {
        let b = new List$1(TransactionState.$, 0);
        if (a > -1) {
            let c = this.e(a);
            if (c != null) {
                for (let d of fromEnum(c.b.values)) {
                    b.add(d);
                }
            }
        }
        else {
            for (let e of fromEnum(this.ai.values)) {
                b.add(e);
            }
        }
        return b.toArray();
    }
    e(a) {
        for (let b = 0; b < this.ak.count; b++) {
            if (this.ak._inner[b].id == a) {
                return this.ak._inner[b];
            }
        }
        return null;
    }
    c(a = null) {
        if (a != null) {
            let b = new List$1(Transaction.$, 0);
            for (let c = 0; c < this.q.count; c++) {
                if (this.q._inner[c].id == a || this.aj.equalsC(this.q._inner[c].id, a)) {
                    b.add(this.q._inner[c]);
                }
            }
            return b.toArray();
        }
        return this.q.toArray();
    }
}
TransactionService.$t = markType(TransactionService, 'TransactionService', BaseTransactionService.$);
TransactionService.ao = 0;
//# sourceMappingURL=TransactionService.js.map