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 F02300;
(function (F02300) {
    var ExportRules = (function (_super) {
        __extends(ExportRules, _super);
        function ExportRules(_document) {
            var _this = _super.call(this, _document) || this;
            _this._document = _document;
            return _this;
        }
        ExportRules.prototype.GetTables = function () {
            var result = [];
            result.push(this.GetNpaData("НПА", this._document.NpaTable));
            result.push(this.GetSpr1Data("Справочник_1"));
            result.push(this.GetSpr2Data("Справочник_2"));
            result.push(this.GetObas1Data("Расчет_ОБАС_1"));
            result.push(this.GetExpenseData("Объем_расходов_1", this._document.P2ExpenseDataTable));
            result.push(this.GetObasR2R3Data("Расчет_ОБАС_2", this._document.P3Sp1DataTable));
            result.push(this.GetExpenseData("Объем_расходов_2", this._document.P3ExpenseDataTable));
            result.push(this.GetObasR2R3Data("Расчет_ОБАС_3", this._document.P4Sp1DataTable, ["06", "07", "08", "09", "10", "11", "12"]));
            result.push(this.GetExpenseData("Объем_расходов_3", this._document.P4ExpenseDataTable));
            return result;
        };
        ExportRules.prototype.GetNpaData = function (barsTableId, srcTable) {
            var template = new BxmlExportTemplate();
            template.InsertDynField("1", "NpaTypeCode");
            template.InsertField("2", srcTable.NpaDate, BxmlExportHelper.RegisteredHelpers.DateToStr);
            template.InsertField("3", srcTable.NpaNumber);
            template.InsertField("npacolumn", srcTable.NpaName);
            template.InsertDynField("5", "NpaStatusCode");
            template.InsertField("6", srcTable.NpaStatusChangeDate, BxmlExportHelper.RegisteredHelpers.DateToStr);
            var modifier = function (record) {
                record["NpaTypeCode"] = srcTable.NpaType.Code;
                record["NpaStatusCode"] = srcTable.NpaStatus.Code;
                return record;
            };
            return this.CreateTableInfo(srcTable, template.FieldIds, template.Template, barsTableId, modifier);
        };
        ExportRules.prototype.GetSpr1Data = function (barsTableId) {
            var template = new BxmlExportTemplate("BarsCode", BxmlExportValueTypes.ModField);
            var srcTable = this._document.P2Sp1DataTable;
            var recCountField = srcTable.Count;
            var recPayP1Field = srcTable.PaymentP1;
            var recPayP2Field = srcTable.PaymentP2;
            var recPayP3Field = srcTable.PaymentP3;
            template.InsertGenField("3", -2, recCountField, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            template.InsertGenField("7", -1, recCountField, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            template.InsertGenField("10", 0, recCountField, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            srcTable.IterateThreePastFields(function (index) {
                template.InsertGenField((6 + index).toString(), index, recPayP1Field, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            });
            srcTable.IterateTwoPastFields(function (index) {
                template.InsertGenField((9 + index).toString(), index, recPayP2Field, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            });
            template.InsertGenField("11", 0, recPayP3Field, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            var strKeyFieldId = BaseObasTableFields.StrKeyField.Id;
            var modifier = function (record) {
                record["BarsCode"] = ObasHelper.FillWithCharacter(srcTable.GetFieldValue(strKeyFieldId) + 1, 2);
                return record;
            };
            return BxmlExportHelper.InsertRequiredData(this.CreateTableInfo(srcTable, template.FieldIds, template.Template, barsTableId, modifier), ["02", "03", "04", "05", "06", "07",
                "08", "09", "10", "11", "12", "13", "14"], template.Template);
        };
        ExportRules.prototype.GetSpr2Data = function (barsTableId) {
            var template = new BxmlExportTemplate("BarsCode", BxmlExportValueTypes.ModField);
            var srcTable = this._document.IndexTransposedTable;
            srcTable.IterateByYears(function (index) {
                template.InsertGenField((1 + index).toString(), index, srcTable.Value, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            }, false);
            var recordKeyFieldId = BaseObasTableFields.RecordKeyField.Id;
            var modifier = function (record) {
                record["BarsCode"] = ObasHelper.FillWithCharacter(srcTable.GetFieldValue(recordKeyFieldId) + 1, 2);
                return record;
            };
            return BxmlExportHelper.InsertRequiredData(this.CreateTableInfo(srcTable, template.FieldIds, template.Template, barsTableId, modifier), ["02"], template.Template);
        };
        ExportRules.prototype.GetObas1Data = function (barsTableId) {
            var template = new BxmlExportTemplate("BarsCode", BxmlExportValueTypes.ModField);
            var srcTable = this._document.P2Sp1DataTable;
            var recCountField = srcTable.PlannedCount;
            var recDeviationField = srcTable.PlannedDeviation;
            srcTable.IteratePlannedFields(function (index) {
                template.InsertGenField((1 + index).toString(), index, recCountField, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
                template.InsertGenField((7 + index).toString(), index, recDeviationField, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            });
            var strKeyFieldId = BaseObasTableFields.StrKeyField.Id;
            var modifier = function (record) {
                var strKey = srcTable.GetFieldValue(strKeyFieldId) + 1;
                if (strKey === 7) {
                    strKey = 3;
                }
                record["BarsCode"] = ObasHelper.FillWithCharacter(strKey, 2);
                return record;
            };
            var filter = function () {
                return srcTable.StrKey === F02300.P2Sp1RowsKeys.C0110 || srcTable.StrKey === F02300.P2Sp1RowsKeys.C0120;
            };
            return BxmlExportHelper.InsertRequiredData(this.CreateTableInfo(srcTable, template.FieldIds, template.Template, barsTableId, modifier, filter), ["02", "03"], template.Template);
        };
        ExportRules.prototype.GetExpenseData = function (barsTableId, srcTable) {
            var template = new BxmlExportTemplate("BarsCode", BxmlExportValueTypes.ModField);
            var yearField = srcTable.YearData;
            srcTable.IterateByYears(function (index) {
                template.InsertGenField((5 + index).toString(), index, yearField, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            }, true, -3);
            var strKeyFieldId = BaseObasTableFields.StrKeyField.Id;
            var modifier = function (record) {
                var strKey = srcTable.GetFieldValue(strKeyFieldId) + 1;
                record["BarsCode"] = ObasHelper.FillWithCharacter(strKey, 2);
                return record;
            };
            return BxmlExportHelper.InsertRequiredData(this.CreateTableInfo(srcTable, template.FieldIds, template.Template, barsTableId, modifier), ["02", "03"], template.Template);
        };
        ExportRules.prototype.GetObasR2R3Data = function (barsTableId, srcTable, addDefBarsIds) {
            if (addDefBarsIds === void 0) { addDefBarsIds = []; }
            var template = new BxmlExportTemplate("BarsCode", BxmlExportValueTypes.ModField);
            var p2Sp1Table = this._document.P2Sp1DataTable;
            var yearData = srcTable.YearData;
            var insuranceField = srcTable.InsuranceCount;
            var salary = srcTable.Salary;
            var count = srcTable.Count;
            p2Sp1Table.IterateThreePastFields(function (index) {
                template.InsertGenField((5 + index).toString(), index, count, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
                template.InsertGenField((11 + index).toString(), index, insuranceField, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
                template.InsertGenField((17 + index).toString(), index, yearData, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
                template.InsertGenField((23 + index).toString(), index, salary, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            });
            p2Sp1Table.IteratePlannedFields(function (index) {
                template.InsertGenField((4 + index).toString(), index, count, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
                template.InsertGenField((10 + index).toString(), index, insuranceField, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
                template.InsertGenField((16 + index).toString(), index, yearData, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
                template.InsertGenField((22 + index).toString(), index, salary, BxmlExportHelper.RegisteredHelpers.NumberToStr, "2");
            });
            var strKeyFieldId = BaseObasTableFields.StrKeyField.Id;
            var modifier = function (record) {
                var strKey = srcTable.GetFieldValue(strKeyFieldId) + 1;
                record["BarsCode"] = ObasHelper.FillWithCharacter(strKey, 2);
                return record;
            };
            return BxmlExportHelper.InsertRequiredData(this.CreateTableInfo(srcTable, template.FieldIds, template.Template, barsTableId, modifier), ["02", "03", "04", "05"].concat(addDefBarsIds), template.Template);
        };
        return ExportRules;
    }(SimpleFormExportRules));
    F02300.ExportRules = ExportRules;
})(F02300 || (F02300 = {}));
