/* global Bdf,$$,Intl,TableSheet */
TableSheet.Standalone = {};

TableSheet.Standalone.ARGS = {
    clientId: "",
    subsetKey: "",
    subsetCategory: "",
    subsetName: "",
    tableExportName: "",
    headerType: ""
};

TableSheet.Standalone.init = function (args) {
    var tableId = Bdf.generateId();
    var subsetKey = args.subsetKey;
    var subsetCategory = args.subsetCategory;
    var subsetName = args.subsetName;
    var tableSheet;
    Bdf.Ajax.loadTable(args.tableExportName, subsetKey, function (table) {
        Bdf.Api.init({
            propertiesMapping: table.meta.mapping
        });
        let cellFactory = new TableSheet.CellFactory({
            lang: table.meta.lang
        });
        let cssResolver = new TableSheet.CssResolver();
        let columnArray = TableSheet.Column.toColumnArray(table.meta.columns, table.meta.locale, cssResolver);
        let withSum = TableSheet.Column.checkSum(columnArray, table.sum, cellFactory);
        tableSheet = new TableSheet(Bdf.api, tableId, subsetCategory, subsetName, columnArray, cellFactory, cssResolver);
        tableSheet.withSum = withSum;
        for (let entry of table.data) {
            tableSheet.convertToRow(entry);
        }
        $$(args.clientId).html(Bdf.render("tablesheet:table", {
            tableSheet: tableSheet,
            withKeyHeader: (args.headerType === "columnkey")
        }));       
        tableSheet.initTableSorter();
        tableSheet.initActions();
    });
};


/******************************************************************************
 * Initialisation
 ******************************************************************************/

$(function () {
    Bdf.initTemplates();
    TableSheet.Standalone.init(TableSheet.Standalone.ARGS);
});
