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 F01110;
(function (F01110) {
    var InterfaceRules = (function (_super) {
        __extends(InterfaceRules, _super);
        function InterfaceRules() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        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.P4RefObjDescNpaTable) != null;
            }
            else {
                return table.GetKeyBySourceTable(this._document.P4RefObjDescNpaTable.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.P4RefObjDescNpaTable) != 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;
    }(BaseInterfaceRules));
    InterfaceRules.CodeInfo = {
        Column: 2,
        Length: 5,
        LevelIncs: [1, 1],
        CalcTotalCode: function () {
            return 90010;
        }
    };
    InterfaceRules.TotalRowName = "Всего";
    F01110.InterfaceRules = InterfaceRules;
    var P1SheetRows;
    (function (P1SheetRows) {
        P1SheetRows[P1SheetRows["Fot"] = 0] = "Fot";
        P1SheetRows[P1SheetRows["OnlyIns"] = 1] = "OnlyIns";
        P1SheetRows[P1SheetRows["Total"] = 2] = "Total";
    })(P1SheetRows || (P1SheetRows = {}));
    var TotalSheetColumnBlocks;
    (function (TotalSheetColumnBlocks) {
        TotalSheetColumnBlocks[TotalSheetColumnBlocks["Total"] = 0] = "Total";
        TotalSheetColumnBlocks[TotalSheetColumnBlocks["Fot"] = 1] = "Fot";
        TotalSheetColumnBlocks[TotalSheetColumnBlocks["Insurance"] = 2] = "Insurance";
    })(TotalSheetColumnBlocks || (TotalSheetColumnBlocks = {}));
    var P1Sheet = (function (_super) {
        __extends(P1Sheet, _super);
        function P1Sheet(id, document) {
            return _super.call(this, id, document, 1) || this;
        }
        P1Sheet.prototype.IsXCell = function (row, column) {
            return row === P1SheetRows.OnlyIns && this.IsColumnInDataBlock(column, TotalSheetColumnBlocks.Fot);
        };
        P1Sheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            switch (column) {
                case this.RowNameColumn:
                case this.RowCodeColumn:
                    return SheetFormatCollection.Default;
                default:
                    if (this.Document.CommonRules.IsFooterRow(this, row) || this.IsColumnInDataBlock(column, TotalSheetColumnBlocks.Total)) {
                        return SheetFormatCollection.Calc;
                    }
                    else if (!this.IsColumnInDataBlock(column, TotalSheetColumnBlocks.Total) && row === (this.RowCount - 2)) {
                        return SheetFormatCollection.Free;
                    }
                    else if (this.IsXCell(row, column)) {
                        return SheetFormatCollection.Default;
                    }
                    else {
                        return SheetFormatCollection.Related;
                    }
            }
        };
        P1Sheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            if (this.IsXCell(row, column)) {
                return ObasHelper.X;
            }
            return undefined;
        };
        P1Sheet.prototype.GetYearsCaptionEventHandler = function (tableId, index, defaultCaption, fieldCaption) {
            return this.Document.CommonRules.CalcColumnCaption(index, this.CalcStartBlockColumn(TotalSheetColumnBlocks.Fot) + 1, 2);
        };

        P1Sheet.prototype.EditingCellEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return (!this.IsColumnInDataBlock(column, TotalSheetColumnBlocks.Total) && row === (this.RowCount - 2));
        };

        return P1Sheet;
    }(YearsSheet));
    F01110.P1Sheet = P1Sheet;
    var P2Sp1SheetRows;
    (function (P2Sp1SheetRows) {
        P2Sp1SheetRows[P2Sp1SheetRows["Fot"] = 0] = "Fot";
        P2Sp1SheetRows[P2Sp1SheetRows["Change"] = 1] = "Change";
        P2Sp1SheetRows[P2Sp1SheetRows["Correction"] = 2] = "Correction";
        P2Sp1SheetRows[P2Sp1SheetRows["Total"] = 3] = "Total";
    })(P2Sp1SheetRows || (P2Sp1SheetRows = {}));
    var P2Sp1Sheet = (function (_super) {
        __extends(P2Sp1Sheet, _super);
        function P2Sp1Sheet(id, document) {
            return _super.call(this, id, document, 1) || this;
        }
        P2Sp1Sheet.prototype.IsUserDataColumn = function (column) {
            return column > this.CalcEndBlockColumn(TotalSheetColumnBlocks.Total);
        };
        P2Sp1Sheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            var isLastRow = this.IsFooterRow(row);
            switch (column) {
                case this.RowNameColumn:
                case this.RowCodeColumn:
                    return SheetFormatCollection.Default;
                default:
                    if (isLastRow || this.IsColumnInDataBlock(column, TotalSheetColumnBlocks.Total)) {
                        return SheetFormatCollection.Calc;
                    }
                    else {
                        switch (row) {
                            case P2Sp1SheetRows.Fot:
                                return SheetFormatCollection.Related;
                            case P2Sp1SheetRows.Change:
                                return SheetFormatCollection.Free;
                            default:
                                return SheetFormatCollection.Calc;
                        }
                    }
            }
        };
        P2Sp1Sheet.prototype.EditingCellEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return !this.IsFooterRow(row) && this.IsUserDataColumn(column) && row === P2Sp1SheetRows.Change;
        };
        P2Sp1Sheet.prototype.GetYearsCaptionEventHandler = function (tableId, index, defaultCaption, fieldCaption) {
            return this.Document.CommonRules.CalcColumnCaption(index, this.CalcStartBlockColumn(TotalSheetColumnBlocks.Fot) + 1, 2);
        };
        return P2Sp1Sheet;
    }(YearsSheet));
    F01110.P2Sp1Sheet = P2Sp1Sheet;
    var P2Sp2SheetRow;
    (function (P2Sp2SheetRow) {
        P2Sp2SheetRow[P2Sp2SheetRow["Fot"] = 0] = "Fot";
        P2Sp2SheetRow[P2Sp2SheetRow["Indexation"] = 1] = "Indexation";
        P2Sp2SheetRow[P2Sp2SheetRow["Total"] = 2] = "Total";
    })(P2Sp2SheetRow || (P2Sp2SheetRow = {}));
    var P2Sp2Sheet = (function (_super) {
        __extends(P2Sp2Sheet, _super);
        function P2Sp2Sheet(id, document) {
            return _super.call(this, id, document, 1) || this;
        }
        P2Sp2Sheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            switch (column) {
                case this.RowNameColumn:
                case this.RowCodeColumn:
                    return SheetFormatCollection.Default;
                default:
                    if (row === P2Sp2SheetRow.Fot) {
                        return SheetFormatCollection.Related;
                    }
                    else if (row === P2Sp2SheetRow.Indexation) {
                        return SheetFormatCollection.Free;
                    }
                    else {
                        return SheetFormatCollection.Calc;
                    }
            }
        };
        P2Sp2Sheet.prototype.EditingCellEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return (column > this.RowCodeColumn) && (row === P2Sp2SheetRow.Indexation);
        };
        return P2Sp2Sheet;
    }(YearsSheet));
    F01110.P2Sp2Sheet = P2Sp2Sheet;
    var P2Sp3GroupSheet = (function () {
        function P2Sp3GroupSheet(_document) {
            this._document = _document;
        }
        P2Sp3GroupSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            return this._document.CommonRules.StandardCalc(sheetId, row, column, P2Sp3GroupSheet._sheetInfo);
        };
        P2Sp3GroupSheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            return this._document.CommonRules.StandardFormat(sheetId, row, column, P2Sp3GroupSheet._sheetInfo);
        };
        P2Sp3GroupSheet.prototype.EditingCellEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            if (column > 1) {
                return false;
            }
            else {
                return this._document.CommonRules
                    .CantEditPrevCurYearsEditingCellEventHandler(sheetId, row, column, fieldId, rowLevel);
            }
        };
        return P2Sp3GroupSheet;
    }());
    P2Sp3GroupSheet._sheetInfo = {
        MaxLevel: 1,
        CodeInfo: InterfaceRules.CodeInfo,
        ColumnsInfo: [
            SheetColumnInfoCollection.SprColAllDefault,
            {
                Cell: { Type: SheetCellTypes.Spr },
                TotalRow: {
                    IsCalculated: false,
                    Name: InterfaceRules.TotalRowName,
                    Format: SheetFormatCollection.Default
                },
                SubTotalRow: SheetRowInfoCollection.SprOnlyFormatRow
            },
            SheetColumnInfoCollection.CalcColFormatOnly,
            SheetColumnInfoCollection.FreeColAllCalc,
            SheetColumnInfoCollection.CalcColAllX,
            SheetColumnInfoCollection.FreeColAllX,
            SheetColumnInfoCollection.FreeColAllX,
            SheetColumnInfoCollection.FreeColAllX,
            SheetColumnInfoCollection.CalcColAllCalc
        ]
    };
    F01110.P2Sp3GroupSheet = P2Sp3GroupSheet;

    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 < 14; 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));
    F01110.AnalyticSheet = AnalyticSheet;

})(F01110 || (F01110 = {}));
