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 F09100;
(function (F09100) {
    var InterfaceRules = (function (_super) {
        __extends(InterfaceRules, _super);
        function InterfaceRules() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        InterfaceRules.GetStrCode = function (row) {
            return ObasHelper.FillWithCharacter(row + 1, 4, "0");
        };
        InterfaceRules.prototype.SheetP1FormatEventHandler = function (sheetId, row, column, groupIndex) {
            var isLastRow = this.Document.CommonRules.IsFooterRow(sheetId, row);
            var lastRow = this.Document.Sheets.getValue(sheetId).RowCount - 1;
            if (!isLastRow && column > 1) {
                return SheetFormatCollection.Related;
            }
            else if ((isLastRow && column > 1) || (column === 1))
                return SheetFormatCollection.Calc;
            return SheetFormatCollection.Default;
        };
        InterfaceRules.prototype.SheetP1EditingCellEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return false;
        };
        InterfaceRules.prototype.FilterNPAStatuses = function () {
            var npaStatusesTable = ObasTableCollection.NpaStatusesSprTable;
            return npaStatusesTable.RecordKey.Value === 1 || npaStatusesTable.RecordKey.Value === 2;
        };
        return InterfaceRules;
    }(BaseInterfaceRules));
    InterfaceRules.TotalRowName = "Всего";
    F09100.InterfaceRules = InterfaceRules;
    var SheetColumnBlocks;
    (function (SheetColumnBlocks) {
        SheetColumnBlocks[SheetColumnBlocks["Total"] = 0] = "Total";
    })(SheetColumnBlocks = F09100.SheetColumnBlocks || (F09100.SheetColumnBlocks = {}));
    var P2Sp1Sheet = (function (_super) {
        __extends(P2Sp1Sheet, _super);
        function P2Sp1Sheet(id, document) {
            var _this = _super.call(this, id, document, 1) || this;
            _this._sheetSprCols = [0, 5, 9];
            return _this;
        }
        P2Sp1Sheet.prototype.GetFormat = function (sheetId, row, column, groupIndex) {
            var isLastRow = this.Document.CommonRules.IsFooterRow(sheetId, row);
            if ((this._sheetSprCols.indexOf(column) > -1) && !isLastRow)
                return SheetFormatCollection.Spr;
            else if ((isLastRow &&
                this.IsColumnInDataBlock(column, SheetColumnBlocks.Total))
                || (column === this.RowCodeColumn))
                return SheetFormatCollection.Calc;
            else if (!isLastRow)
                return SheetFormatCollection.Free;
            return SheetFormatCollection.Default;
        };
        P2Sp1Sheet.prototype.GetCalc = function (sheetId, row, column, fieldId) {
            if (column === this.RowCodeColumn) {
                return InterfaceRules.GetStrCode(row);
            }
            else if (this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                if (column === 0)
                    return InterfaceRules.TotalRowName;
                else if (this.IsColumnInDataBlock(column, SheetColumnBlocks.Total))
                    return this.Document.CommonRules.GetFooterSum(sheetId, row, column);
                else if (!this.IsColumnInDataBlock(column, SheetColumnBlocks.Total))
                    return ObasHelper.X;
            }
            return undefined;
        };
        return P2Sp1Sheet;
    }(YearsSheet));
    F09100.P2Sp1Sheet = P2Sp1Sheet;
    var P2Sp2Sheet = (function (_super) {
        __extends(P2Sp2Sheet, _super);
        function P2Sp2Sheet(id, document) {
            return _super.call(this, id, document, 1) || this;
        }
        P2Sp2Sheet.prototype.GetFormat = function (sheetId, row, column, groupIndex) {
            var isLastRow = this.Document.CommonRules.IsFooterRow(sheetId, row);
            if ((isLastRow &&
                this.IsColumnInDataBlock(column, SheetColumnBlocks.Total))
                || (column === this.RowCodeColumn))
                return SheetFormatCollection.Calc;
            else if (!isLastRow)
                return SheetFormatCollection.Free;
            return SheetFormatCollection.Default;
        };
        P2Sp2Sheet.prototype.GetCalc = function (sheetId, row, column, fieldId) {
            if (fieldId === BaseObasTableFields.StrCodeField.Id) {
                return ObasHelper.FillWithCharacter(row + 1, 2);
            }
            else if (this.Document.CommonRules.IsFooterRow(sheetId, row)) {
                if (column === 0)
                    return InterfaceRules.TotalRowName;
                else if (this.IsColumnInDataBlock(column, SheetColumnBlocks.Total))
                    return this.Document.CommonRules.GetFooterSum(sheetId, row, column);
                else if (!this.IsColumnInDataBlock(column, SheetColumnBlocks.Total))
                    return ObasHelper.X;
            }
            return undefined;
        };
        return P2Sp2Sheet;
    }(YearsSheet));
    F09100.P2Sp2Sheet = P2Sp2Sheet;
    var P2Sp3Sheet = (function (_super) {
        __extends(P2Sp3Sheet, _super);
        function P2Sp3Sheet(id, _document, readOnly) {
            if (readOnly === void 0) { readOnly = false; }
            var _this = _super.call(this, id, readOnly) || this;
            _this._document = _document;
            return _this;
        }
        P2Sp3Sheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            switch (column) {
                case 0:
                    return SheetFormatCollection.Related;
                case 1:
                    return SheetFormatCollection.Calc;
                case 2:
                case 6:
                    return SheetFormatCollection.Spr;
                default:
                    return SheetFormatCollection.Free;
            }
        };
        P2Sp3Sheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            if (fieldId === BaseObasTableFields.StrCodeField.Id) {
                var colCount = this.ColumnCount;
                var curParentKey = this.GetCellValue(row, colCount, false);
                var prevParentKey = row === 0 ? -1 : this.GetCellValue(row - 1, colCount, false);
                var code = row === 0 ? 0 : parseInt(this.GetCellValue(row - 1, column), 10);
                if (curParentKey !== prevParentKey) {
                    code = ((code / 100) | 0) * 100 + 100;
                }
                code += 1;
                return ObasHelper.FillWithCharacter(code, 4);
            }
            return undefined;
        };
        P2Sp3Sheet.prototype.CanEditEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return this.Table.GetKeyBySourceTable(this._document.TableP2Sp3) != null;
        };
        return P2Sp3Sheet;
    }(Sheet));
    F09100.P2Sp3Sheet = P2Sp3Sheet;
})(F09100 || (F09100 = {}));
