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 FOutlay;
(function (FOutlay) {
    var InterfaceRules = (function () {
        function InterfaceRules(_document) {
            this._document = _document;
        }
        InterfaceRules.prototype.GetExcelTemplateFileName = function () {
            return ObasStageSettings.ExcelTemplateDirectory + "\\" + this._document.MainParametersTable.GetFieldValue("ExcelTemplate");
        };
        InterfaceRules.prototype.GetExcelFileName = function () {
            var outlayType = this._document.MainParametersTable.GetFieldValue("OutlayType");
            if (this._document.OutlayType === 1) {
                return "\u0411\u044E\u0434\u0436\u0435\u0442\u043D\u0430\u044F \u0441\u043C\u0435\u0442\u0430 (\u0444. 0501019).xls";
            }
            else {
                return "\u0418\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u0435 \u043F\u043E\u043A\u0430\u0437\u0430\u0442\u0435\u043B\u0435\u0439 \u0431\u044E\u0434\u0436\u0435\u0442\u043D\u043E\u0439 \u0441\u043C\u0435\u0442\u044B (\u0444. 0501020).xls";
            }
        };
        InterfaceRules.prototype.SilentExport = function (exportDir) {
            this._document.ExportToExcel(this.GetExcelTemplateFileName(), this.GetExcelFileName(), true, false, exportDir);
        };
        InterfaceRules.prototype.HideLables = function () {
            Client.SetComponentText(ClientComponents.lblKBK, this.GetExcelFileName());
            Client.SetComponentText(ClientComponents.lblKbkName, "");
            Client.SetComponentText(ClientComponents.lblName, "");
            Client.SetComponentText(ClientComponents.lblNameLbl, "");
        };
        InterfaceRules.prototype.ExportToExcel = function () {
            this.SilentExport(ObasHelper.GetExportDirectory());
        };
        return InterfaceRules;
    }());
    FOutlay.InterfaceRules = InterfaceRules;
    var DataColumns;
    (function (DataColumns) {
        DataColumns[DataColumns["Rub"] = 0] = "Rub";
        DataColumns[DataColumns["Usd"] = 1] = "Usd";
        DataColumns[DataColumns["Code"] = 2] = "Code";
    })(DataColumns || (DataColumns = {}));
    var BaseGroupSheet = (function (_super) {
        __extends(BaseGroupSheet, _super);
        function BaseGroupSheet(document, _startDataColumn, _blockColumnCount) {
            var _this = _super.call(this, document) || this;
            _this._startDataColumn = _startDataColumn;
            _this._blockColumnCount = _blockColumnCount;
            return _this;
        }
        BaseGroupSheet.prototype.CalcStartBlockColumn = function (block) {
            return this._startDataColumn + this._blockColumnCount * block;
        };
        BaseGroupSheet.prototype.CalcEndBlockColumn = function (block) {
            return this._startDataColumn + this._blockColumnCount * (block + 1) - 1;
        };
        BaseGroupSheet.prototype.IsDataColumn = function (column) {
            return this.CalcOffsetCol(column) >= 0;
        };
        BaseGroupSheet.prototype.CalcDataColumnBlock = function (column) {
            return (this.CalcOffsetCol(column) / this._blockColumnCount) | 0;
        };
        BaseGroupSheet.prototype.IsColumnInDataBlock = function (column, block) {
            return ((this.CalcDataColumnBlock(column) === block) && this.IsDataColumn(column));
        };
        BaseGroupSheet.prototype.CalcRelColumn = function (column) {
            return this.CalcOffsetCol(column) % this._blockColumnCount;
        };
        BaseGroupSheet.prototype.CalcOffsetCol = function (column) {
            return column - this._startDataColumn;
        };
        return BaseGroupSheet;
    }(YearGroupSheet));
    var P1GroupSheet = (function (_super) {
        __extends(P1GroupSheet, _super);
        function P1GroupSheet(document, tableId) {
            var _this = _super.call(this, document, 5, 3) || this;
            _this.tableId = tableId;
            return _this;
        }
        P1GroupSheet.prototype.IsDualRow = function (sheet, row) {
            return sheet.GetCellValue(row, 0, true) === "";
        };
        P1GroupSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            var sheet = this.Document.Sheets.getValue(sheetId);
            var rowLvl = sheet.GetRowLevel(row);
            if (column === P1GroupSheet._rowNameColumn) {
                if (row === sheet.RowCount - 1) {
                    return P1GroupSheet._totalName;
                }
                else if (this.IsDualRow(sheet, row)) {
                    return P1GroupSheet._subtotalName;
                }
            }
            if (this.IsDataColumn(column)) {
                var relColumn = this.CalcRelColumn(column);
                switch (relColumn) {
                    case DataColumns.Rub:
                        if (row === sheet.RowCount - 1) {
                            var result = 0;
                            var table = this.Document.Tables.getValue(this.tableId);
                            var reader = table.CreateReader();
                            while (reader.Read()) {
                                if (table.GetFieldValue("Level") === 1) {
                                    result += table.GetFieldValue(fieldId.replace("Fake_", ""));
                                }
                            }
                            return result;
                        }
                        else if (this.IsDualRow(sheet, row)) {
                            var result = 0;
                            var fcrSectionCode = sheet.GetCellValue(row - 1, 0, true);
                            var table = this.Document.Tables.getValue(this.tableId);
                            var reader = table.CreateReader();
                            while (reader.Read()) {
                                if ((table.GetFieldValue("Level") === 1) && (table.GetFieldValue("FCRCodeSection") === fcrSectionCode)) {
                                    result += table.GetFieldValue(fieldId.replace("Fake_", ""));
                                }
                            }
                            return result;
                        }
                        break;
                    case DataColumns.Usd:
                    case DataColumns.Code:
                        if (this.IsDualRow(sheet, row)) {
                            return ObasHelper.X;
                        }
                }
            }
        };
        P1GroupSheet._totalName = "\u0412\u0441\u0435\u0433\u043E";
        P1GroupSheet._subtotalName = "\u0418\u0442\u043E\u0433\u043E \u043F\u043E \u043A\u043E\u0434\u0443 \u0411\u041A";
        P1GroupSheet._rowNameColumn = 4;
        return P1GroupSheet;
    }(BaseGroupSheet));
    FOutlay.P1GroupSheet = P1GroupSheet;
    var PXGroupSheet = (function (_super) {
        __extends(PXGroupSheet, _super);
        function PXGroupSheet(document, tableId) {
            var _this = _super.call(this, document, 7, 3) || this;
            _this.tableId = tableId;
            return _this;
        }
        PXGroupSheet.prototype.IsDualRow = function (sheet, row) {
            return sheet.GetCellValue(row, 5, true) === "";
        };
        PXGroupSheet.prototype.GetPrevRowCode = function (sheet, row, column) {
            row--;
            while (row > -1) {
                var prevCode = sheet.GetCellValue(row, column, true) || "";
                if (prevCode.length > 0) {
                    return parseInt(prevCode, 10);
                }
                row--;
            }
            return 0;
        };
        PXGroupSheet.prototype.CalcRowCode = function (sheet, row, column) {
            var result = this.GetPrevRowCode(sheet, row, column) + 10;
            return ObasHelper.FillWithCharacter(result, 4);
        };
        PXGroupSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            var sheet = this.Document.Sheets.getValue(sheetId);
            var rowLvl = sheet.GetRowLevel(row);
            if (column === PXGroupSheet._rowNameColumn) {
                if (row === sheet.RowCount - 1) {
                    return PXGroupSheet._totalName;
                }
                else if (this.IsDualRow(sheet, row)) {
                    return PXGroupSheet._subtotalName;
                }
            }
            else if (column === 1) {
                if (this.IsDualRow(sheet, row)) {
                    return "";
                }
                else if ((this.IsDualRow(sheet, row - 1) || (row === 0))) {
                    return "0100";
                }
                else {
                    return this.CalcRowCode(sheet, row, column);
                }
            }
            else if (column === 2) {
                if (this.IsDualRow(sheet, row) && (row != sheet.RowCount - 1)) {
                    return sheet.GetCellValue(row - 1, column, true);
                }
            }
            else if (column === 3) {
                if (this.IsDualRow(sheet, row) && (row != sheet.RowCount - 1)) {
                    return "00";
                }
            }
            if (this.IsDataColumn(column)) {
                var relColumn = this.CalcRelColumn(column);
                switch (relColumn) {
                    case DataColumns.Rub:
                        if (row === sheet.RowCount - 1) {
                            var result = 0;
                            var table = this.Document.Tables.getValue(this.tableId);
                            var reader = table.CreateReader();
                            while (reader.Read()) {
                                if (table.GetFieldValue("Level") === 1) {
                                    result += table.GetFieldValue(fieldId.replace("Fake_", ""));
                                }
                            }
                            return result;
                        }
                        else if (this.IsDualRow(sheet, row)) {
                            var result = 0;
                            var fcrSectionCode = sheet.GetCellValue(row - 1, 2, true);
                            var table = this.Document.Tables.getValue(this.tableId);
                            var reader = table.CreateReader();
                            while (reader.Read()) {
                                if ((table.GetFieldValue("Level") === 1) && (table.GetFieldValue("FCRCodeSection") === fcrSectionCode)) {
                                    result += table.GetFieldValue(fieldId.replace("Fake_", ""));
                                }
                            }
                            return result;
                        }
                        break;
                    case DataColumns.Usd:
                    case DataColumns.Code:
                        if (this.IsDualRow(sheet, row)) {
                            return ObasHelper.X;
                        }
                }
            }
        };
        PXGroupSheet._totalName = "\u0412\u0441\u0435\u0433\u043E";
        PXGroupSheet._subtotalName = "\u0418\u0442\u043E\u0433\u043E \u043F\u043E \u043A\u043E\u0434\u0443 \u0411\u041A";
        PXGroupSheet._rowNameColumn = 0;
        return PXGroupSheet;
    }(BaseGroupSheet));
    FOutlay.PXGroupSheet = PXGroupSheet;
    var KbkColumns;
    (function (KbkColumns) {
        KbkColumns[KbkColumns["FcrSectionCode"] = 2] = "FcrSectionCode";
        KbkColumns[KbkColumns["FcrSubSectionCode"] = 3] = "FcrSubSectionCode";
        KbkColumns[KbkColumns["CsrCode"] = 4] = "CsrCode";
        KbkColumns[KbkColumns["VrCode"] = 5] = "VrCode";
    })(KbkColumns || (KbkColumns = {}));
    var P2GroupSheet = (function (_super) {
        __extends(P2GroupSheet, _super);
        function P2GroupSheet(document, tableId, tablePart4Id) {
            var _this = _super.call(this, document, 7, 3) || this;
            _this.tableId = tableId;
            _this.tablePart4Id = tablePart4Id;
            return _this;
        }
        P2GroupSheet.prototype.IsDualRow = function (sheet, row) {
            return sheet.GetCellValue(row, 5, true) === "";
        };
        P2GroupSheet.prototype.GetPrevRowCode = function (sheet, row, column) {
            row--;
            while (row > -1) {
                var prevCode = sheet.GetCellValue(row, column, true) || "";
                if (prevCode.length > 0) {
                    return parseInt(prevCode, 10);
                }
                row--;
            }
            return 0;
        };
        P2GroupSheet.prototype.CalcRowCode = function (sheet, row, column) {
            var result = this.GetPrevRowCode(sheet, row, column) + 10;
            return ObasHelper.FillWithCharacter(result, 4);
        };
        P2GroupSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            var sheet = this.Document.Sheets.getValue(sheetId);
            var rowLvl = sheet.GetRowLevel(row);
            if (column === P2GroupSheet._rowNameColumn) {
                if (row === sheet.RowCount - 1) {
                    return P2GroupSheet._totalName;
                }
                else if (this.IsDualRow(sheet, row)) {
                    return P2GroupSheet._subtotalName;
                }
            }
            else if (column === 1) {
                if (this.IsDualRow(sheet, row)) {
                    return "";
                }
                else if ((this.IsDualRow(sheet, row - 1) || (row === 0))) {
                    return "0100";
                }
                else {
                    return this.CalcRowCode(sheet, row, column);
                }
            }
            else if (column === 2) {
                if (this.IsDualRow(sheet, row) && (row != sheet.RowCount - 1)) {
                    return sheet.GetCellValue(row - 1, column, true);
                }
            }
            else if (column === 3) {
                if (this.IsDualRow(sheet, row) && (row != sheet.RowCount - 1)) {
                    return "00";
                }
            }
            if (this.IsDataColumn(column)) {
                var relColumn = this.CalcRelColumn(column);
                switch (relColumn) {
                    case DataColumns.Rub:
                        if (row === sheet.RowCount - 1) {
                            var result = 0;
                            var table = this.Document.Tables.getValue(this.tableId);
                            var reader = table.CreateReader();
                            while (reader.Read()) {
                                if (table.GetFieldValue("Level") === 1) {
                                    result += table.GetFieldValue(fieldId.replace("Fake_", ""));
                                }
                            }
                            var tablePart4 = this.Document.Tables.getValue(this.tablePart4Id);
                            var reader = tablePart4.CreateReader();
                            while (reader.Read()) {
                                if (tablePart4.GetFieldValue("Level") === 1) {
                                    result -= tablePart4.GetFieldValue(fieldId.replace("Fake_", ""));
                                }
                            }
                            return result;
                        }
                        else if (this.IsDualRow(sheet, row)) {
                            var result = 0;
                            var fcrSectionCode = sheet.GetCellValue(row - 1, 2, true);
                            var table = this.Document.Tables.getValue(this.tableId);
                            var reader = table.CreateReader();
                            while (reader.Read()) {
                                if ((table.GetFieldValue("Level") === 1) && (table.GetFieldValue("FCRCodeSection") === fcrSectionCode)) {
                                    result += table.GetFieldValue(fieldId.replace("Fake_", ""));
                                }
                            }
                            var tablePart4 = this.Document.Tables.getValue(this.tablePart4Id);
                            var reader = tablePart4.CreateReader();
                            while (reader.Read()) {
                                if ((tablePart4.GetFieldValue("Level") === 1) && (tablePart4.GetFieldValue("FCRCodeSection") === fcrSectionCode)) {
                                    result -= tablePart4.GetFieldValue(fieldId.replace("Fake_", ""));
                                }
                            }
                            return result;
                        }
                        else {
                            var fcrSectionCode = sheet.GetCellValue(row, KbkColumns.FcrSectionCode, true);
                            var fcrSubSectionCode = sheet.GetCellValue(row, KbkColumns.FcrSubSectionCode, true);
                            var csrCode = sheet.GetCellValue(row, KbkColumns.CsrCode, true);
                            var vrCode = sheet.GetCellValue(row, KbkColumns.VrCode, true);
                            var kosguCode = sheet.GetCellValue(row, KbkColumns.VrCode + 1, true);
                            var result = sheet.GetCellValue(row, column, true);
                            var tablePart4 = this.Document.Tables.getValue(this.tablePart4Id);
                            if (tablePart4.Locate(["FCR_CodeSection", "FCR_CodeSubSection", "CSR_Code", "VR_Code", "AnalyticCode"], [fcrSectionCode, fcrSubSectionCode, csrCode, vrCode, kosguCode])) {
                                result -= tablePart4.GetFieldValue(fieldId.replace("Fake_", ""));
                            }
                            return result;
                        }
                        break;
                    case DataColumns.Usd:
                    case DataColumns.Code:
                        if (this.IsDualRow(sheet, row)) {
                            return ObasHelper.X;
                        }
                }
            }
        };
        P2GroupSheet._totalName = "\u0412\u0441\u0435\u0433\u043E";
        P2GroupSheet._subtotalName = "\u0418\u0442\u043E\u0433\u043E \u043F\u043E \u043A\u043E\u0434\u0443 \u0411\u041A";
        P2GroupSheet._rowNameColumn = 0;
        return P2GroupSheet;
    }(BaseGroupSheet));
    FOutlay.P2GroupSheet = P2GroupSheet;
    var P5sp3xGroupSheet = (function (_super) {
        __extends(P5sp3xGroupSheet, _super);
        function P5sp3xGroupSheet(document, tableId) {
            var _this = _super.call(this, document, 4, 1) || this;
            _this.tableId = tableId;
            return _this;
        }
        P5sp3xGroupSheet.prototype.IsDualRow = function (sheet, row) {
            return sheet.GetCellValue(row, 0, true) === "";
        };
        P5sp3xGroupSheet.prototype.GetPrevRowCode = function (sheet, row, column) {
            row--;
            while (row > -1) {
                var prevCode = sheet.GetCellValue(row, column, true) || "";
                if (prevCode.length > 0) {
                    return parseInt(prevCode, 10);
                }
                row--;
            }
            return 0;
        };
        P5sp3xGroupSheet.prototype.CalcRowCode = function (sheet, row, column) {
            var result = this.GetPrevRowCode(sheet, row, column) + 100;
            return ObasHelper.FillWithCharacter(result, 4);
        };
        P5sp3xGroupSheet.prototype.CalcEventHandler = function (sheetId, row, column, fieldId) {
            var sheet = this.Document.Sheets.getValue(sheetId);
            if (column === P5sp3xGroupSheet._rowNameColumn) {
                if (row === sheet.RowCount - 1) {
                    return P5sp3xGroupSheet._totalName;
                }
                else if (this.IsDualRow(sheet, row)) {
                    return P5sp3xGroupSheet._subtotalName;
                }
            }
            else if (column === 7) {
                if (this.IsDualRow(sheet, row)) {
                    return "";
                }
                else if ((this.IsDualRow(sheet, row - 1) || (row === 0))) {
                    return "0100";
                }
                else {
                    return this.CalcRowCode(sheet, row, column);
                }
            }
            if ((this.CalcOffsetCol(column) >= 0) && (this.CalcOffsetCol(column) <= 2)) {
                if (row === sheet.RowCount - 1) {
                    var result = 0;
                    var table = this.Document.Tables.getValue(this.tableId);
                    var reader = table.CreateReader();
                    while (reader.Read()) {
                        result += table.GetFieldValue(fieldId);
                    }
                    return result;
                }
                else if (this.IsDualRow(sheet, row)) {
                    var result = 0;
                    var fcrSectionCode = sheet.GetCellValue(row - 1, 0, true);
                    var table = this.Document.Tables.getValue(this.tableId);
                    var reader = table.CreateReader();
                    while (reader.Read()) {
                        if (table.GetFieldValue("FCRCodeSection") === fcrSectionCode) {
                            result += table.GetFieldValue(fieldId);
                        }
                    }
                    return result;
                }
            }
            else if ((column > 7) && this.IsDualRow(sheet, row)) {
                return ObasHelper.X;
            }
        };
        P5sp3xGroupSheet.prototype.CanEditEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            var sheet = this.Document.Sheets.getValue(sheetId);
            return column > 3 && column != 7 && !this.IsDualRow(sheet, row);
        };
        P5sp3xGroupSheet.prototype.CanAddEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            var sheet = this.Document.Sheets.getValue(sheetId);
            return column > 7 && !this.IsDualRow(sheet, row);
        };
        P5sp3xGroupSheet.prototype.CanDeleteEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            var sheet = this.Document.Sheets.getValue(sheetId);
            return column > 7 && !this.IsDualRow(sheet, row);
        };
        P5sp3xGroupSheet._totalName = "\u0412\u0441\u0435\u0433\u043E";
        P5sp3xGroupSheet._subtotalName = "\u0418\u0442\u043E\u0433\u043E \u043F\u043E \u043A\u043E\u0434\u0443 \u0411\u041A";
        P5sp3xGroupSheet._rowNameColumn = 3;
        return P5sp3xGroupSheet;
    }(BaseGroupSheet));
    FOutlay.P5sp3xGroupSheet = P5sp3xGroupSheet;
})(FOutlay || (FOutlay = {}));
