"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("../polyfills/tslib.js");
var webmapgl_core_js_1 = require("../webmapgl.core.js");
var TableExportButton = function () { function t() { this.format = "csv"; } return t.prototype.clickHandler = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (t) { return this.exportCtrl.create(this), [2]; }); }); }, t.prototype.render = function () { var t = this; return webmapgl_core_js_1.h("ion-button", { "ion-button": !0, onClick: function () { return t.clickHandler(); } }, webmapgl_core_js_1.h("slot", null, this.format.toUpperCase())); }, Object.defineProperty(t, "is", { get: function () { return "gl-table-export-button"; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "properties", { get: function () { return { author: { type: String, attr: "author" }, exportCtrl: { connect: "gl-table-export-controller" }, filename: { type: String, attr: "filename" }, format: { type: String, attr: "format" }, pageOrientation: { type: String, attr: "page-orientation" }, pageSubtitle: { type: String, attr: "page-subtitle" }, pageTitle: { type: String, attr: "page-title" }, tableId: { type: String, attr: "table-id" } }; }, enumerable: !0, configurable: !0 }), t; }(), fileDownload = function (t, e, r, n) { var o = new Blob(void 0 !== n ? [n, t] : [t], { type: r || "application/octet-stream" }); if (void 0 !== window.navigator.msSaveBlob)
    window.navigator.msSaveBlob(o, e);
else {
    var i = window.URL.createObjectURL(o), a = document.createElement("a");
    a.style.display = "none", a.href = i, a.setAttribute("download", e), void 0 === a.download && a.setAttribute("target", "_blank"), document.body.appendChild(a), a.click(), document.body.removeChild(a), window.URL.revokeObjectURL(i);
} };
exports.GlTableExportButton = TableExportButton;
function csvQuote(t) { return t ? -1 !== t.indexOf(",") ? '"' + t.replace(/"/g, '""') + '"' : t : ""; }
var TableExportController = function () { function t() { } return t.prototype.create = function (t) { return tslib_1.__awaiter(this, void 0, void 0, function () { var e, r, n, o, i; return tslib_1.__generator(this, function (a) { switch (a.label) {
    case 0: return e = "gl-table" + (t.tableId ? "#" + t.tableId : ""), r = t.table ? t.table : document.querySelector(e), t.features ? (o = t.features, [3, 3]) : [3, 1];
    case 1: return [4, r.getSortedFeatures()];
    case 2: o = a.sent(), a.label = 3;
    case 3: return n = o, i = t.columns ? t.columns : Array.from(r.querySelectorAll("gl-table-column")), "csv" === t.format && this.exportCsv(r, n, i, t), "pdf" === t.format && this.exportPdf(r, n, i, t), [2];
} }); }); }, t.prototype.exportCsv = function (t, e, r, n) { return tslib_1.__awaiter(this, void 0, void 0, function () { var o; return tslib_1.__generator(this, function (i) { switch (i.label) {
    case 0: return [4, this.createCsvDoc(t, e, r)];
    case 1: return o = i.sent(), fileDownload(o, this.getFilename(n), "text/csv"), [2];
} }); }); }, t.prototype.exportPdf = function (t, e, r, n) { return tslib_1.__awaiter(this, void 0, void 0, function () { var o, i; return tslib_1.__generator(this, function (a) { switch (a.label) {
    case 0: return i = (o = pdfMake).createPdf, [4, this.createDocDef(t, e, r, n)];
    case 1: return i.apply(o, [a.sent()]).download(this.getFilename(n)), [2];
} }); }); }, t.prototype.getFilename = function (t) { return t.filename ? t.filename : t.pageTitle ? t.pageTitle.toLowerCase().replace(/[^a-z0-9]+/g, "_") + "." + t.format : "table." + t.format; }, t.prototype.zebraFill = function (t) { return 0 === t ? "black" : t % 2 == 0 ? "#ECEBEB" : null; }, t.prototype.createCsvDoc = function (t, e, r) { return tslib_1.__awaiter(this, void 0, void 0, function () { var n, o, i, a = this; return tslib_1.__generator(this, function (l) { switch (l.label) {
    case 0: return n = r.filter(function (t) { return t.visible; }), o = n.map(function (t) { return csvQuote(t.label); }), [4, Promise.all(e.map(function (e) { return a.getTableRow(t, e, n, "text"); }))];
    case 1: return i = l.sent().map(function (t) { return t.map(function (t) { return csvQuote(t); }); }), [2, [o].concat(i).map(function (t) { return t.join(","); }).join("\n")];
} }); }); }, t.prototype.getTableRow = function (t, e, r, n) { return void 0 === n && (n = "pdf"), Promise.all(r.map(function (r) { return t.getCellContents(e, r, n); })); }, t.prototype.createDocDef = function (t, e, r, n) { return tslib_1.__awaiter(this, void 0, void 0, function () { var o, i, a, l, u = this; return tslib_1.__generator(this, function (c) { switch (c.label) {
    case 0: return o = r.filter(function (t) { return t.visible; }), i = o.map(function (t) { return { text: t.label, style: "tableHeader" }; }), [4, Promise.all(e.map(function (e) { return u.getTableRow(t, e, o); }))];
    case 1: return a = c.sent(), l = { info: {}, pageOrientation: n.pageOrientation, content: [{ table: { headerRows: 1, body: [i].concat(a) }, style: "table", layout: { fillColor: this.zebraFill } }], styles: { tableHeader: { color: "white" }, header: { fontSize: 15, bold: !0, margin: [0, 0, 0, 5] }, subHeader: { margin: [0, 5, 0, 15] } } }, n.pageSubtitle && l.content.unshift({ text: n.pageSubtitle, style: "subHeader", alignment: "center" }), n.pageTitle && (l.info.title = n.pageTitle, l.content.unshift({ text: n.pageTitle, style: "header", alignment: "center" })), n.author && (l.info.author = n.author), [2, l];
} }); }); }, Object.defineProperty(t, "is", { get: function () { return "gl-table-export-controller"; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "properties", { get: function () { return { create: { method: !0 } }; }, enumerable: !0, configurable: !0 }), t; }();
exports.GlTableExportController = TableExportController;
