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 F01211;
(function (F01211) {
    var InterfaceRules = (function (_super) {
        __extends(InterfaceRules, _super);
        function InterfaceRules() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        InterfaceRules.prototype.NpaSheetFormatEventHandler = function (sheetId, row, column, groupIndex) {
            switch (column) {
                case 1:
                    return SheetFormatCollection.Default;
                case 2:
                case 6:
                    return SheetFormatCollection.Spr;
                default:
                    return SheetFormatCollection.Free;
            }
        };
        InterfaceRules.prototype.NpaSheetCalcEventHandler = function (sheetId, row, column, fieldId) {
            if (column === 1) {
                return ObasHelper.FillWithCharacter((row + 1).toString(), 3);
            }
        };
        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.P7RefObjDescNpaTable) != null;
            }
            else {
                return table.GetKeyBySourceTable(this._document.P7RefObjDescNpaTable.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.P7RefObjDescNpaTable) != null;
            }
            else {
                return false;
            }
        };
        InterfaceRules.prototype.FilterOKPD = function () {
            var okpdCode = ObasTableCollection.OkpdSprTable.Code.Value || "";
            return okpdCode.length >= 5;
        };
        InterfaceRules.prototype.FilterNPAStatuses = function () {
            var npaStatusesTable = ObasTableCollection.NpaStatusesSprTable;
            return npaStatusesTable.RecordKey.Value === 1 || npaStatusesTable.RecordKey.Value === 2;
        };
        InterfaceRules.prototype.CountMonthForCalcSheetFormatEventHandler = function (sheetId, row, column, groupIndex) {
            if (column > 1) {
                return SheetFormatCollection.Free;
            }
            else {
                return SheetFormatCollection.Default;
            }
        };
        InterfaceRules.prototype.CountMonthForCalcSheetCanEditEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return column > 1;
        };
        return InterfaceRules;
    }(F012XX.InterfaceRules));
    F01211.InterfaceRules = InterfaceRules;
    var PXSp1Sheet = (function (_super) {
        __extends(PXSp1Sheet, _super);
        function PXSp1Sheet(id, document, isCalcCorrection) {
            return _super.call(this, id, document, 1, isCalcCorrection, 1) || this;
        }
        PXSp1Sheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            if (this.IsDataColumn(column) && this.IsFooterRow(row)) {
                return this.Document.CommonRules.GetFooterSum(this, row, column);
            }
            else {
                return _super.prototype.CalcEventHandler.call(this, sheetId, row, column, fieldId);
            }
        };
        return PXSp1Sheet;
    }(F012XX.PXSp1Sheet));
    F01211.PXSp1Sheet = PXSp1Sheet;
    var PartWithTotalGroupSheet = (function (_super) {
        __extends(PartWithTotalGroupSheet, _super);
        function PartWithTotalGroupSheet(document, _rowCodeColumn) {
            var _this = _super.call(this, document) || this;
            _this._rowCodeColumn = _rowCodeColumn;
            _this._rowNameColumn = _rowCodeColumn - 1;
            return _this;
        }
        Object.defineProperty(PartWithTotalGroupSheet.prototype, "RowCodeColumn", {
            get: function () {
                return this._rowCodeColumn;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PartWithTotalGroupSheet.prototype, "RowNameColumn", {
            get: function () {
                return this._rowNameColumn;
            },
            enumerable: true,
            configurable: true
        });
        PartWithTotalGroupSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            if (this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                if (column < this._rowNameColumn) {
                    return undefined;
                }
                else if (column === this._rowNameColumn) {
                    return InterfaceRules.TotalRowName;
                }
                else if (column === this._rowCodeColumn) {
                    return InterfaceRules.TotalRowCode;
                }
                else if (this.IsXCell(sheetId, row, column)) {
                    return ObasHelper.X;
                }
                else {
                    return this.Document.CommonRules.GetFooterSum(sheetId, row, column);
                }
            }
            else if (column === this._rowCodeColumn) {
                return InterfaceRules.CalcRowCode(row);
            }
            return undefined;
        };
        PartWithTotalGroupSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            if (this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                if ((column <= this._rowCodeColumn) || (this.IsXCell(sheetId, row, column))) {
                    return SheetFormatCollection.Default;
                }
                else {
                    return SheetFormatCollection.Calc;
                }
            }
            return SheetFormatCollection.Default;
        };
        PartWithTotalGroupSheet.prototype.EditingCellEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            if (column > this._rowCodeColumn) {
                return false;
            }
            else {
                return this.Document.CommonRules
                    .CantEditPrevCurYearsEditingCellEventHandler(sheetId, row, column, fieldId, rowLevel);
            }
        };
        return PartWithTotalGroupSheet;
    }(YearGroupSheet));
    F01211.PartWithTotalGroupSheet = PartWithTotalGroupSheet;
    var P2Sp3GroupSheet = (function (_super) {
        __extends(P2Sp3GroupSheet, _super);
        function P2Sp3GroupSheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P2Sp3GroupSheet.prototype.IsXCell = function (sheetId, row, column) {
            return column > 2;
        };
        P2Sp3GroupSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            if (!this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                switch (column) {
                    case this.RowNameColumn:
                        return SheetFormatCollection.Spr;
                    case this.RowCodeColumn:
                        return SheetFormatCollection.Default;
                    case 4:
                        return SheetFormatCollection.Calc;
                    case 2:
                        return SheetFormatCollection.Free;
                    default:
                        return SheetFormatCollection.Related;
                }
            }
            return _super.prototype.FormatEventHandler.call(this, sheetId, row, column, groupIndex);
        };
        return P2Sp3GroupSheet;
    }(PartWithTotalGroupSheet));
    F01211.P2Sp3GroupSheet = P2Sp3GroupSheet;
    var P2Sp4GroupSheet = (function (_super) {
        __extends(P2Sp4GroupSheet, _super);
        function P2Sp4GroupSheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P2Sp4GroupSheet.prototype.IsXCell = function (sheetId, row, column) {
            return false;
        };
        P2Sp4GroupSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            if (!this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                switch (column) {
                    case this.RowNameColumn:
                        return SheetFormatCollection.Related;
                    case this.RowCodeColumn:
                        return SheetFormatCollection.Default;
                    default:
                        return SheetFormatCollection.Calc;
                }
            }
            return _super.prototype.FormatEventHandler.call(this, sheetId, row, column, groupIndex);
        };
        return P2Sp4GroupSheet;
    }(PartWithTotalGroupSheet));
    F01211.P2Sp4GroupSheet = P2Sp4GroupSheet;
    var P5Sp3GroupSheet = (function (_super) {
        __extends(P5Sp3GroupSheet, _super);
        function P5Sp3GroupSheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P5Sp3GroupSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            if (column === 4 && !this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                var result = new SheetCalcResult(BaseFormulas.SUM);
                for (var i = 1; i < 7; i++) {
                    result.AddCoordinates(new CellCoordinate(row, column + i));
                }
                return result.ToArray();
            }
            return _super.prototype.CalcEventHandler.call(this, sheetId, row, column, fieldId);
        };
        return P5Sp3GroupSheet;
    }(P2Sp3GroupSheet));
    F01211.P5Sp3GroupSheet = P5Sp3GroupSheet;
    var P5Sp4GroupSheet = (function (_super) {
        __extends(P5Sp4GroupSheet, _super);
        function P5Sp4GroupSheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P5Sp4GroupSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            var sheet = this.Document.Sheets.getValue(sheetId);
            if (!this.Document.CommonRules.IsFooterRow(sheet, row)) {
                switch (column) {
                    case this.RowCodeColumn:
                        return SheetFormatCollection.Default;
                    case 2:
                    case 3:
                    case sheet.ColumnCount - 1:
                        return SheetFormatCollection.Calc;
                    default:
                        return SheetFormatCollection.Related;
                }
            }
            return _super.prototype.FormatEventHandler.call(this, sheetId, row, column, groupIndex);
        };
        return P5Sp4GroupSheet;
    }(P2Sp4GroupSheet));
    F01211.P5Sp4GroupSheet = P5Sp4GroupSheet;
    var P3Sp3GroupSheet = (function (_super) {
        __extends(P3Sp3GroupSheet, _super);
        function P3Sp3GroupSheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P3Sp3GroupSheet.prototype.IsXCell = function (sheetId, row, column) {
            return column > 5;
        };
        P3Sp3GroupSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            if ((column === 7 || column === 13) && !this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                var result = new SheetCalcResult(BaseFormulas.SUM);
                var count = column === 7 ? 5 : 2;
                for (var i = 1; i <= count; i++) {
                    result.AddCoordinates(new CellCoordinate(row, column + i));
                }
                return result.ToArray();
            }
            return _super.prototype.CalcEventHandler.call(this, sheetId, row, column, fieldId);
        };
        P3Sp3GroupSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            if (!this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                switch (column) {
                    case 0:
                        return SheetFormatCollection.Spr;
                    case this.RowCodeColumn:
                        return SheetFormatCollection.Default;
                    case 7:
                    case 13:
                        return SheetFormatCollection.Calc;
                    case 4:
                    case 5:
                        return SheetFormatCollection.Free;
                    default:
                        return SheetFormatCollection.Related;
                }
            }
            return _super.prototype.FormatEventHandler.call(this, sheetId, row, column, groupIndex);
        };
        return P3Sp3GroupSheet;
    }(PartWithTotalGroupSheet));
    F01211.P3Sp3GroupSheet = P3Sp3GroupSheet;
    var P3Sp4GroupSheet = (function (_super) {
        __extends(P3Sp4GroupSheet, _super);
        function P3Sp4GroupSheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P3Sp4GroupSheet.prototype.IsXCell = function (sheetId, row, column) {
            return false;
        };
        P3Sp4GroupSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            if (!this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                switch (column) {
                    case this.RowCodeColumn:
                        return SheetFormatCollection.Default;
                    case 4:
                    case 5:
                    case 11:
                    case 14:
                        return SheetFormatCollection.Calc;
                    default:
                        return SheetFormatCollection.Related;
                }
            }
            return _super.prototype.FormatEventHandler.call(this, sheetId, row, column, groupIndex);
        };
        return P3Sp4GroupSheet;
    }(PartWithTotalGroupSheet));
    F01211.P3Sp4GroupSheet = P3Sp4GroupSheet;
    var P4Sp3GroupSheet = (function (_super) {
        __extends(P4Sp3GroupSheet, _super);
        function P4Sp3GroupSheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P4Sp3GroupSheet.prototype.IsXCell = function (sheetId, row, column) {
            return column > 3 && column < 8;
        };
        P4Sp3GroupSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            var sheet = this.Document.Sheets.getValue(sheetId);
            if (!this.Document.CommonRules.IsFooterRow(sheet, row)) {
                switch (column) {
                    case this.RowNameColumn:
                        return SheetFormatCollection.Spr;
                    case this.RowCodeColumn:
                        return SheetFormatCollection.Default;
                    case 4:
                    case sheet.ColumnCount - 1:
                        return SheetFormatCollection.Calc;
                    default:
                        return SheetFormatCollection.Free;
                }
            }
            return _super.prototype.FormatEventHandler.call(this, sheetId, row, column, groupIndex);
        };
        return P4Sp3GroupSheet;
    }(PartWithTotalGroupSheet));
    F01211.P4Sp3GroupSheet = P4Sp3GroupSheet;
    var AnalyticSheet = (function (_super) {
        __extends(AnalyticSheet, _super);
        function AnalyticSheet(id, _document) {
            var _this = _super.call(this, id, false) || this;
            _this._document = _document;
            return _this;
        }
        AnalyticSheet.prototype.IsTotal = function (row) {
            return (row === 0 || row === 1 || row === 4 || row === 7 || row === 9 || row === 11);
        };
        AnalyticSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            if (column > AnalyticSheet._strCodeColumn) {
                if (this.IsTotal(row)) {
                    return SheetFormatCollection.Calc;
                }
                else {
                    return SheetFormatCollection.Free;
                }
            }
            else {
                return SheetFormatCollection.Default;
            }
        };
        AnalyticSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            if (column > AnalyticSheet._strCodeColumn) {
                if (this.IsTotal(row)) {
                    var result = new SheetCalcResult(BaseFormulas.SUM);
                    switch (row) {
                        case 0:
                            for (var i = 1; i < this.RowCount; i++) {
                                if (!this.IsTotal(i)) {
                                    result.AddCoordinates(new CellCoordinate(i, column));
                                }
                            }
                            break;
                        case 1:
                            for (var i = 2; i < 4; i++) {
                                result.AddCoordinates(new CellCoordinate(i, column));
                            }
                            break;
                        case 4:
                            for (var i = 5; i < 7; i++) {
                                result.AddCoordinates(new CellCoordinate(i, column));
                            }
                            break;
                        case 7:
                            result.AddCoordinates(new CellCoordinate(8, column));
                            break;
                        case 9:
                            result.AddCoordinates(new CellCoordinate(10, column));
                            break;
                        case 11:
                            for (var i = 12; i < this.RowCount; i++) {
                                result.AddCoordinates(new CellCoordinate(i, column));
                            }
                            break;
                    }
                    return result.ToArray();
                }
            }
            return undefined;
        };
        AnalyticSheet.prototype.CanEditCellEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return ((column > AnalyticSheet._strCodeColumn) && !this.IsTotal(row));
        };
        AnalyticSheet._strCodeColumn = 1;
        return AnalyticSheet;
    }(Sheet));
    F01211.AnalyticSheet = AnalyticSheet;
})(F01211 || (F01211 = {}));
