var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var AnalyticsKosgu;
(function (AnalyticsKosgu) {
    var AnalyticsDataTable = (function (_super) {
        __extends(AnalyticsDataTable, _super);
        function AnalyticsDataTable(id, _document) {
            var _this = _super.call(this, id) || this;
            _this._document = _document;
            _this._kosgu = null;
            _this._analyticCode = null;
            _this._analyticName = null;
            return _this;
        }
        Object.defineProperty(AnalyticsDataTable.prototype, "Kosgu", {
            get: function () {
                if (this._kosgu == null) {
                    this._kosgu = new ObasSprTableField(ObasTableCollection.KosguSprTable, this);
                }
                return this._kosgu;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(AnalyticsDataTable.prototype, "AnalyticCode", {
            get: function () {
                if (this._analyticCode == null) {
                    this._analyticCode = new ObasTableField("AnalyticCode", this);
                }
                return this._analyticCode;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(AnalyticsDataTable.prototype, "AnalyticName", {
            get: function () {
                if (this._analyticName == null) {
                    this._analyticName = new ObasTableField("AnalyticName", this);
                }
                return this._analyticName;
            },
            enumerable: true,
            configurable: true
        });
        AnalyticsDataTable.prototype.DeleteEventHandler = function (tableId) {
            for (var i = 0; i <= ObasStageSettings.YearsCount; i++) {
                var fieldId = "Y" + (i + 1);
                var oldValue = this.GetFieldValue(fieldId) || 0;
                var newValue = 0;
                this.SumChangeEventHandler(tableId, oldValue, newValue, fieldId);
            }
            if (this.IsSecondRow() && (this.RowCount > 2)) {
                this.Next();
                this.FillAnalyticsData(true);
                this.Prev();
            }
        };
        AnalyticsDataTable.prototype.KosguChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            this.FillAnalyticsData();
        };
        AnalyticsDataTable.prototype.SumChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            var curKey = this.RecordKey.Value;
            if (this.RecordKey.Value != 1) {
                if (this.Locate(this.RecordKey.Id, 1)) {
                    var oldTotalValue = this.GetFieldValue(fieldId);
                    this.SetFieldValue(fieldId, oldTotalValue + (newValue - oldValue));
                }
            }
            this.Locate(this.RecordKey.Id, curKey);
        };
        AnalyticsDataTable.prototype.IsSecondRow = function () {
            var result = false;
            if (this.RowCount > 1) {
                this.Prev();
                result = (this.RecordKey.Value === 1);
                this.Next();
            }
            return result;
        };
        AnalyticsDataTable.prototype.FillAnalyticsData = function (needPrefix) {
            var kosguName = this.Kosgu.Name;
            this.AnalyticName.Value = kosguName.charAt(0).toLowerCase() + kosguName.slice(1);
            if (needPrefix == null) {
                needPrefix = this.IsSecondRow();
            }
            if (needPrefix) {
                this.AnalyticName.Value = "\u0432 \u0442\u043E\u043C \u0447\u0438\u0441\u043B\u0435:\n" + this.AnalyticName.Value;
            }
            this.AnalyticCode.Value = this.Kosgu.Code;
        };
        return AnalyticsDataTable;
    }(SprTable));
    AnalyticsKosgu.AnalyticsDataTable = AnalyticsDataTable;
})(AnalyticsKosgu || (AnalyticsKosgu = {}));
