import { UIInternal } from './ui-internal';
import { Container } from 'aurelia-framework';
import { I18N } from 'aurelia-i18n';
import { FieldType } from '../model/common-model';
import moment from 'moment';
var CsvExporterOptions = (function () {
    function CsvExporterOptions(options) {
        this.fieldSeparator = ';';
        this.filename = 'Export';
        this.quoteStrings = '"';
        this.showLabels = true;
        this.headerConfig = [];
        Object.assign(this, options);
    }
    Object.defineProperty(CsvExporterOptions.prototype, "headers", {
        get: function () {
            return this.headerConfig.map(function (header) {
                return isString(header) ? header : header.headerName;
            }).filter(function (h) { return !!h; });
        },
        enumerable: false,
        configurable: true
    });
    return CsvExporterOptions;
}());
export { CsvExporterOptions };
var CsvExporter = (function () {
    function CsvExporter(colDefsOrOptions, options) {
        var finalOptions = null;
        if (colDefsOrOptions instanceof CsvExporterOptions) {
            finalOptions = colDefsOrOptions;
        }
        else {
            finalOptions = options || new CsvExporterOptions();
            finalOptions.headerConfig = colDefsOrOptions.filter(function (ColDef) { return ColDef.field != 'isSelected'; });
        }
        this.options = finalOptions;
    }
    Object.defineProperty(CsvExporter.prototype, "i18n", {
        get: function () {
            if (this._i18n == null) {
                this._i18n = Container.instance.get(I18N);
            }
            return this._i18n;
        },
        enumerable: false,
        configurable: true
    });
    CsvExporter.prototype.export = function (data) {
        var _this = this;
        var EOL = "\r\n";
        var csv = "\ufeff"
            + this.options.headers.join(this.options.fieldSeparator) + EOL
            + data.map(function (entry) { return _this.exportRow(entry); }).join(EOL);
        if (csv == '') {
            return;
        }
        var fileExtension = '.csv';
        var blob = new Blob([csv], { "type": "text/csv;charset=utf8;" });
        var filename = this.options.filename.replace(/ /g, "_") + fileExtension;
        if (navigator.msSaveBlob) {
            navigator.msSaveBlob(blob, filename);
        }
        else {
            var link = document.createElement("a");
            link.href = URL.createObjectURL(blob);
            link.setAttribute('visibility', 'hidden');
            link.download = filename;
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    };
    CsvExporter.prototype.exportRow = function (entry) {
        var _this = this;
        var row = [];
        if (false == isString(this.options.headerConfig[0])) {
            row = this.options.headerConfig.map(function (colDef) {
                var _a, _b;
                if (colDef.valueGetter) {
                    return isString(colDef.valueGetter)
                        ? UIInternal.getValue(entry, colDef.valueGetter)
                        : colDef.valueGetter({
                            colDef: colDef,
                            data: entry,
                            getValue: function (field) { return UIInternal.getValue(entry, field); },
                            node: null,
                            column: null,
                            api: null,
                            columnApi: null,
                            context: undefined
                        });
                }
                var value = UIInternal.getValue(entry, colDef.field);
                if (value == null) {
                    return value;
                }
                if (colDef.type == FieldType.Date) {
                    return moment(value).format(((_a = colDef.floatingFilterComponentParams) === null || _a === void 0 ? void 0 : _a.dateFormat) || 'YYYY-MM-DD');
                }
                else if (colDef.type == FieldType.DateTime) {
                    return moment(value).format(((_b = colDef.floatingFilterComponentParams) === null || _b === void 0 ? void 0 : _b.dateFormat) || 'YYYY-MM-DD HH:mm:ss');
                }
                else if (colDef.type == FieldType.Boolean) {
                    if (value) {
                        return _this.i18n.tr("general.yes", { ns: "common" });
                    }
                    else {
                        return _this.i18n.tr("general.no", { ns: "common" });
                    }
                }
                else if (colDef.type == FieldType.Number) {
                    return value.toLocaleString('fr-BE', { useGrouping: false });
                }
                return value;
            });
        }
        else {
            row = this.options.headers.reduce(function (row, header) {
                row.push(entry[header] || "");
                return row;
            }, []);
        }
        return row.map(function (value) {
            value = (value || "").replace(/"/g, '""');
            if (_this.options.quoteStrings || value.indexOf(',') > -1 || value.indexOf('\n') > -1 || value.indexOf('\r') > -1) {
                value = _this.options.quoteStrings + value + _this.options.quoteStrings;
            }
            return value;
        }).join(this.options.fieldSeparator);
    };
    return CsvExporter;
}());
export { CsvExporter };

//# sourceMappingURL=csv-exporter.js.map
