/*
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 * as tslib_1 from "tslib";
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
 */
var TransactionService = /** @class */ (function (_super) {
    tslib_1.__extends(TransactionService, _super);
    function TransactionService(a, b, c) {
        var _this = _super.call(this, a, b, c) || this;
        _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);
        }
        return _this;
    }
    TransactionService.prototype.get_l = function () {
        return true;
    };
    TransactionService.prototype.set_l = function (a) {
        _super.prototype.set_l.call(this, a);
    };
    TransactionService.prototype.get_k = function () {
        return this.an.count > 0;
    };
    TransactionService.prototype.get_j = function () {
        return this.al.count > 0;
    };
    TransactionService.prototype.af = function (a, b) {
        var 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)));
        }
    };
    TransactionService.prototype.aq = function (a, b) {
        var 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;
        }
    };
    TransactionService.prototype.z = function (a, b, c) {
        _super.prototype.z.call(this, a, b, c);
        switch (a) {
            case "IsEnabled":
                this.w(null);
                break;
        }
    };
    TransactionService.prototype.v = function (a, b) {
        if (b === void 0) { b = false; }
        var c = (this.m || b) ? this.o : this.ai;
        var 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) {
            var e = new TransactionHistory();
            e.transactions.add(a);
            this.an.insert(0, e);
            this.al.clear();
            this.aa(3, e);
        }
    };
    TransactionService.prototype.w = function (a) {
        if (a === void 0) { a = null; }
        if (a != null) {
            for (var b = 0; b < this.am.count; b++) {
                if (this.am._inner[b].id == a) {
                    this.am.removeAt(b);
                    b--;
                }
            }
            this.ai.removeItem(a);
            for (var c = 0; c < this.an.count; c++) {
                for (var 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.prototype.w.call(this, a);
    };
    TransactionService.prototype.g = function (a) {
        var b = this.m ? this.o : this.ai;
        if (b.count > 0 && b.containsKey(a)) {
            return b.item(a);
        }
        return null;
    };
    TransactionService.prototype.ah = function (a) {
        return _super.prototype.g.call(this, a);
    };
    TransactionService.prototype.ag = function (a) {
        for (var 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;
    };
    TransactionService.prototype.y = function (a) {
        this.m = false;
        if (a) {
            var b = new TransactionHistory();
            for (var c = 0; c < this.q.count; c++) {
                var d = this.q._inner[c];
                var 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.prototype.y.call(this, a);
    };
    TransactionService.prototype.ap = function (a) {
        for (var b = 0; b < this.q.count; b++) {
            var 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 (var d = 0; d < this.q.count; d++) {
                    this.af(this.o, this.q._inner[d]);
                }
                var e = new TransactionHistory();
                e.transactions.add(c);
                this.an.insert(0, e);
                this.al.clear();
                break;
            }
        }
    };
    TransactionService.prototype.s = function (a) {
        var b = -1;
        if (this.ai.count > 0) {
            var 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;
    };
    TransactionService.prototype.u = function (a) {
        var b = this.e(a);
        if (b != null) {
            this.aa(5, b);
            this.ak.removeAt(b.d);
        }
    };
    TransactionService.prototype.ac = function (a) {
        var b = this.e(a);
        if (b != null) {
            this.ak.removeAt(b.d);
        }
    };
    TransactionService.prototype.ae = function () {
        if (this.k) {
            var a = this.an._inner[0];
            this.an.removeAt(0);
            var b = this.am.count - a.transactions.count;
            this.am.v(b, a.transactions.count);
            this.al.insert(0, a);
            this.ai.clear();
            for (var c = 0; c < this.an.count; c++) {
                for (var 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);
        }
    };
    TransactionService.prototype.ab = function () {
        if (this.j) {
            var a = this.al._inner[0];
            this.al.removeAt(0);
            for (var 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);
        }
    };
    TransactionService.prototype.a = function (a) {
        var e_1, _a, e_2, _b;
        if (a === void 0) { a = -1; }
        var b = new List$1(TransactionState.$, 0);
        if (a > -1) {
            var c = this.e(a);
            if (c != null) {
                try {
                    for (var _c = tslib_1.__values(fromEnum(c.b.values)), _d = _c.next(); !_d.done; _d = _c.next()) {
                        var d = _d.value;
                        b.add(d);
                    }
                }
                catch (e_1_1) { e_1 = { error: e_1_1 }; }
                finally {
                    try {
                        if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
                    }
                    finally { if (e_1) throw e_1.error; }
                }
            }
        }
        else {
            try {
                for (var _e = tslib_1.__values(fromEnum(this.ai.values)), _f = _e.next(); !_f.done; _f = _e.next()) {
                    var e = _f.value;
                    b.add(e);
                }
            }
            catch (e_2_1) { e_2 = { error: e_2_1 }; }
            finally {
                try {
                    if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
                }
                finally { if (e_2) throw e_2.error; }
            }
        }
        return b.toArray();
    };
    TransactionService.prototype.e = function (a) {
        for (var b = 0; b < this.ak.count; b++) {
            if (this.ak._inner[b].id == a) {
                return this.ak._inner[b];
            }
        }
        return null;
    };
    TransactionService.prototype.c = function (a) {
        if (a === void 0) { a = null; }
        if (a != null) {
            var b = new List$1(Transaction.$, 0);
            for (var 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;
    return TransactionService;
}(BaseTransactionService));
export { TransactionService };
//# sourceMappingURL=TransactionService.js.map