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 F10120;
(function (F10120) {
    var InterfaceRules = (function (_super) {
        __extends(InterfaceRules, _super);
        function InterfaceRules() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        InterfaceRules.prototype.SheetP1FormatEventHandler = function (sheetId, row, column, groupIndex) {
            var lastRow = this.Document.Sheets.getValue(sheetId).RowCount - 1;
            if (column < 2) {
                return SheetFormatCollection.Default;
            }
            else if (this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                return SheetFormatCollection.Calc;
            }
            else {
                return SheetFormatCollection.Related;
            }
        };
        InterfaceRules.prototype.SheetNpaFormatEventHandler = function (sheetId, row, column, groupIndex) {
            switch (column) {
                case 0:
                    return SheetFormatCollection.Related;
                case 1:
                case 5:
                    return SheetFormatCollection.Spr;
                default:
                    return SheetFormatCollection.Free;
            }
        };
        InterfaceRules.prototype.FilterKosgu = function () {
            var kosguCode = ObasTableCollection.KosguSprTable.Code.Value || "";
            return (kosguCode !== "212") && this.Document.CommonRules.FilterKosgu();
        };
        InterfaceRules.prototype.FilterNPAStatuses = function () {
            var npaStatusesTable = ObasTableCollection.NpaStatusesSprTable;
            return npaStatusesTable.RecordKey.Value === 1 || npaStatusesTable.RecordKey.Value === 2;
        };
        InterfaceRules.prototype.AddKosgu212 = function () {
            var formTotalTable = this._document.TableP1;
            var formAnalyticsTable = this._document.AnalyticsDataTable;
            var keyKosgu212 = ObasTableCollection.KosguSprTable.LookupKeyByCode("212");
            if (!formAnalyticsTable.Locate([formAnalyticsTable.Kosgu.ForeignKey.Id], [keyKosgu212])) {
                formAnalyticsTable.AddRow();
                formAnalyticsTable.PostRow();
                formAnalyticsTable.Kosgu.ForeignKey.Value = keyKosgu212;
                if (formTotalTable.IsTotalKeyField.Locate(1)) {
                    for (var i = 1; i <= ObasStageSettings.YearsCount; i++) {
                        var yearFieldId = BaseObasTableFields.YearDataField.GenerateId(i);
                        var otherFieldId = F10120.ObasTableFields.OtherRubField.GenerateId(i);
                        formAnalyticsTable.SetFieldValue(yearFieldId, formTotalTable.GetFieldValue(otherFieldId));
                    }
                }
                else {
                    this.DeleteKosgu212();
                }
            }
        };
        InterfaceRules.prototype.DeleteKosgu212 = function () {
            var formAnalyticsTable = this._document.AnalyticsDataTable;
            var keyKosgu212 = ObasTableCollection.KosguSprTable.LookupKeyByCode("212");
            if (formAnalyticsTable.Locate([formAnalyticsTable.Kosgu.ForeignKey.Id], [keyKosgu212])) {
                formAnalyticsTable.DeleteRow();
            }
        };
        InterfaceRules.prototype.BeforeDocumentSave = function () {
            this.AddKosgu212();
            this.Document.AnalyticSheet.Refresh();
            var result = this.Document.CommonRules.CheckChangeType();
            this.DeleteKosgu212();
            this.Document.AnalyticSheet.Refresh();
            return result;
        };
        InterfaceRules.prototype.SaveKosguTable = function (tableId) {
            this.AddKosgu212();
            this.Document.CommonRules.UpdateMainKosguValues();
            this.DeleteKosgu212();
        };
        InterfaceRules.prototype.DocumentSaveEventHandler = function () {
            var tableP1 = this.Document.TableP1;
            var yearsCount = this.Document.Settings.YearsCount;
            if (tableP1.IsTotalKeyField.Locate(1)) {
                var kosguSprTable = ObasTableCollection.KosguSprTable;
                var data = [];
                var record_1 = {
                    Key: kosguSprTable.LookupKeyByCode("212"),
                    Data: new Float32Array(yearsCount)
                };
                var otherField_1 = F10120.ObasTableFields.OtherRubField;
                var yearField_1 = BaseObasTableFields.YearDataField;
                this.Document.IterateByYears(function (i) {
                    record_1.Data[i] = tableP1.GetFieldValue(otherField_1.GenerateId(i + 1));
                }, false);
                data.push(record_1);
                record_1 = {
                    Key: kosguSprTable.LookupKeyByCode("999"),
                    Data: new Float32Array(yearsCount)
                };
                this.Document.IterateByYears(function (i) {
                    record_1.Data[i] = tableP1.GetFieldValue(yearField_1.GenerateId(i + 1)) -
                        tableP1.GetFieldValue(otherField_1.GenerateId(i + 1));
                }, false);
                data.push(record_1);
                this.Document.CommonRules.UpdateKosguTable(data);
            }
            this.Document.CommonRules.DocumentSaveEventHandler();
        };
        InterfaceRules.prototype.ObjectDescSheetFormatEventHandler = function (sheetId, row, column, groupIndex) {
            switch (column) {
                case 0:
                case 1:
                case 2:
                    return SheetFormatCollection.Related;
                case 8:
                    return SheetFormatCollection.Default;
                case 9:
                case 13:
                    return SheetFormatCollection.Spr;
                default:
                    return SheetFormatCollection.Free;
            }
        };
        InterfaceRules.prototype.ObjectDescSheetCalcEventHandler = function (sheetId, row, column, fieldId) {
            if (BaseObasTableFields.StrCodeField.Equal(fieldId)) {
                return ObasHelper.FillWithCharacter(row + 1, 6);
            }
            return undefined;
        };
        InterfaceRules.prototype.ObjectDescSheetCanAddEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return column > 7;
        };
        InterfaceRules.prototype.ObjectDescSheetCanEditEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            var table = this._document.Sheets.getValue(sheetId).Table;
            if (column > 7) {
                return table.GetKeyBySourceTable(this._document.P5RefObjDescNpaTable) != null;
            }
            else {
                return table.GetKeyBySourceTable(this._document.P5RefObjDescNpaTable.ParentTable) != null;
            }
        };
        InterfaceRules.prototype.ObjectDescSheetCanDeleteEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            var table = this._document.Sheets.getValue(sheetId).Table;
            if (column > 7) {
                return table.GetKeyBySourceTable(this._document.P5RefObjDescNpaTable) != null;
            }
            else {
                return false;
            }
        };
        InterfaceRules.prototype.FilterOKPD = function () {
            var okpdCode = ObasTableCollection.OkpdSprTable.Code.Value || "";
            return okpdCode.length >= 5;
        };
        return InterfaceRules;
    }(BaseInterfaceRules));
    F10120.InterfaceRules = InterfaceRules;
    var BaseSheet = (function (_super) {
        __extends(BaseSheet, _super);
        function BaseSheet(id, _document) {
            var _this = _super.call(this, id, false) || this;
            _this._document = _document;
            return _this;
        }
        Object.defineProperty(BaseSheet.prototype, "Document", {
            get: function () {
                return this._document;
            },
            enumerable: true,
            configurable: true
        });
        BaseSheet.prototype.GetStrCode = function (row) {
            return ObasHelper.FillWithCharacter(row + 1, BaseSheet._rowCodeLength);
        };
        BaseSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            if (column === BaseSheet._rowCodeColumn) {
                return this.GetStrCode(row);
            }
            return undefined;
        };
        return BaseSheet;
    }(Sheet));
    BaseSheet._rowCodeLength = 3;
    BaseSheet._rowCodeColumn = 1;
    F10120.BaseSheet = BaseSheet;
    var BaseCalcSheet = (function (_super) {
        __extends(BaseCalcSheet, _super);
        function BaseCalcSheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        BaseCalcSheet.prototype.IsFooterRow = function (row) {
            return this.Document.CommonRules.IsFooterRow(this, row);
        };
        BaseCalcSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            if (this.IsFooterRow(row)) {
                if (column > BaseCalcSheet._rowCodeColumn) {
                    return SheetFormatCollection.Calc;
                }
                else {
                    return SheetFormatCollection.Default;
                }
            }
            else {
                switch (column) {
                    case 0:
                        return SheetFormatCollection.Free;
                    case 1:
                        return SheetFormatCollection.Default;
                    default:
                        return this.FormatDataColumns(row, column);
                }
            }
        };
        BaseCalcSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            var result = _super.prototype.CalcEventHandler.call(this, sheetId, row, column, fieldId);
            if (result == null) {
                if (this.IsFooterRow(row)) {
                    if (column === 0) {
                        return BaseCalcSheet._totalRowName;
                    }
                    else if (column > BaseCalcSheet._rowCodeColumn) {
                        return this.Document.CommonRules.GetFooterSum(this, row, column);
                    }
                }
            }
            return result;
        };
        return BaseCalcSheet;
    }(BaseSheet));
    BaseCalcSheet._totalRowName = "Всего";
    F10120.BaseCalcSheet = BaseCalcSheet;
    var P2Sheet = (function (_super) {
        __extends(P2Sheet, _super);
        function P2Sheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P2Sheet.prototype.FormatDataColumns = function (row, column) {
            return SheetFormatCollection.Free;
        };
        return P2Sheet;
    }(BaseCalcSheet));
    F10120.P2Sheet = P2Sheet;
    var P3Sheet = (function (_super) {
        __extends(P3Sheet, _super);
        function P3Sheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P3Sheet.prototype.GetColumnBlock = function (column) {
            return ((column - (P3Sheet._rowCodeColumn + 1)) / this.Document.Settings.YearsCount) | 0;
        };
        P3Sheet.prototype.FormatDataColumns = function (row, column) {
            if ((this.GetColumnBlock(column) % 2) === 0) {
                return SheetFormatCollection.Free;
            }
            else {
                return SheetFormatCollection.Calc;
            }
        };
        return P3Sheet;
    }(BaseCalcSheet));
    F10120.P3Sheet = P3Sheet;
})(F10120 || (F10120 = {}));
