"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 chunk_cf921ba9_js_1 = require("./chunk-cf921ba9.js");
var _nextId = 0, Table = function () { function t() { this.columns = [], this.disableDefaultSort = !1, this.features = [], this.sortOrder = "asc"; } return t.prototype.componentWillLoad = function () { void 0 === this.id && (this.id = "table-" + _nextId.toString(), _nextId += 1); }, t.prototype.componentDidLoad = function () { this.updateColumns(); }, t.prototype.updateColumns = function () { this.columns = Array.from(this.el.querySelectorAll("gl-table-column")); }, t.prototype.setSort = function (t) { t.attribute === this.sortAttribute ? this.sortOrder = "asc" === this.sortOrder ? "desc" : "asc" : (this.sortAttribute = t.attribute, this.sortOrder = "asc"), this.glTableSort.emit({ tableId: this.id, attribute: this.sortAttribute, order: this.sortOrder }); }, t.prototype.getSortedFeatures = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (t) { return [2, this.sortedFeatures()]; }); }); }, t.prototype.sortedFeatures = function () { var t = this; if (this.disableDefaultSort)
    return this.features; var e = this.features.slice(); return e.sort(function (e, r) { var o = e.properties[t.sortAttribute], n = r.properties[t.sortAttribute]; "string" == typeof o && (o = o.toUpperCase()), "string" == typeof n && (n = n.toUpperCase()); var i = 0; return o > n ? i = 1 : o < n && (i = -1), "desc" === t.sortOrder ? -1 * i : i; }), e; }, t.prototype.renderHeader = function (t) { var e = this, r = {}; return t.sortable && (r = { class: "sortable sorted-none", tabindex: 0, "aria-controls": this.id, "aria-label": t.label + ": " + chunk_cf921ba9_js_1.a("webmapgl.table.header.sort-action.desc"), onClick: function () { return e.setSort(t); } }, t.attribute === this.sortAttribute && (r.class = "sortable sorted-" + this.sortOrder, r["aria-sort"] = "asc" === this.sortOrder ? "ascending" : "descending", r["aria-label"] = t.label + ": " + chunk_cf921ba9_js_1.a("webmapgl.table.header.sort-action." + this.sortOrder))), webmapgl_core_js_1.h("th", Object.assign({}, r), t.label); }, t.prototype.getCellContents = function (t, e, r) { return tslib_1.__awaiter(this, void 0, void 0, function () { var o; return tslib_1.__generator(this, function (n) { switch (n.label) {
    case 0: return o = t.properties[e.attribute], e.component ? [4, this.renderComponentAsText(t, e, o, r)] : [2, this.renderValue(e, o)];
    case 1: return [2, n.sent()];
} }); }); }, t.prototype.renderValue = function (t, e) { return null == e ? null : (t.format && (e = e.toLocaleString({}, t.format)), e.toString()); }, t.prototype.renderComponentJSX = function (t, e, r) { return webmapgl_core_js_1.h(e.component, Object.assign({ feature: t, column: e, value: r }, e.componentProps || {})); }, t.prototype.renderComponentAsText = function (t, e, r, o) { return tslib_1.__awaiter(this, void 0, void 0, function () { var n, i, s, a; return tslib_1.__generator(this, function (l) { switch (l.label) {
    case 0:
        for (s in n = document.createElement(e.component), i = e.componentProps || {})
            n[s] = i[s];
        return n.feature = t, n.column = e, n.value = r, n.style.display = "none", this.el.appendChild(n), [4, n.componentOnReady()];
    case 1: return l.sent(), a = "function" == typeof n.formatValue ? n.formatValue(o) : n.textContent, this.el.removeChild(n), [2, a];
} }); }); }, t.prototype.renderCell = function (t, e) { var r = t.properties[e.attribute]; return webmapgl_core_js_1.h("td", null, e.component ? this.renderComponentJSX(t, e, r) : this.renderValue(e, r)); }, t.prototype.renderRow = function (t, e) { var r = this; return webmapgl_core_js_1.h("tr", { class: this.rowClassAttribute ? t.properties[this.rowClassAttribute] : "row" }, e.map(function (e) { return r.renderCell(t, e); })); }, t.prototype.render = function () { var t = this; if (0 === this.columns.length)
    return null; var e = this.sortedFeatures(), r = this.columns.filter(function (t) { return t.visible; }), o = r.map(function (e) { return t.renderHeader(e); }), n = e.map(function (e) { return t.renderRow(e, r); }); return webmapgl_core_js_1.h("table", { id: this.id }, webmapgl_core_js_1.h("thead", null, webmapgl_core_js_1.h("tr", null, o)), webmapgl_core_js_1.h("tbody", null, n)); }, Object.defineProperty(t, "is", { get: function () { return "gl-table"; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "properties", { get: function () { return { columns: { state: !0 }, disableDefaultSort: { type: Boolean, attr: "disable-default-sort" }, el: { elementRef: !0 }, features: { type: "Any", attr: "features" }, getCellContents: { method: !0 }, getSortedFeatures: { method: !0 }, id: { type: String, attr: "id", reflectToAttr: !0, mutable: !0 }, rowClassAttribute: { type: String, attr: "row-class-attribute" }, sortAttribute: { type: String, attr: "sort-attribute", mutable: !0 }, sortOrder: { type: String, attr: "sort-order", mutable: !0 } }; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "events", { get: function () { return [{ name: "glTableSort", method: "glTableSort", bubbles: !0, cancelable: !0, composed: !0 }]; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "listeners", { get: function () { return [{ name: "glTableColumnChanged", method: "updateColumns" }]; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "style", { get: function () { return "gl-table table{border-collapse:collapse;border-spacing:0;margin:0 0 10px 0;width:100%}gl-table thead{background-color:#eee}gl-table td,gl-table th{border:1px solid #ddd;line-height:1.3;padding:8px 10px}gl-table td:first-child,gl-table th:first-child{border-left:0 none transparent}gl-table td:last-child,gl-table th:last-child{border-right:0 none transparent}gl-table thead th{font-weight:700}gl-table thead th.sortable{cursor:pointer;padding-right:27px;position:relative}gl-table thead th.sortable:after,gl-table thead th.sortable:before{color:#ccc;content:\"\\25B2\";font-size:.7em;margin-top:-1.1em;position:absolute;right:10px;top:50%}gl-table thead th.sortable:after{bottom:50%;content:\"\\25BC\";margin-bottom:-1.1em;margin-top:0;top:auto}gl-table thead th.sorted-asc:before{color:var(--ion-color-primary,#3880ff)}gl-table thead th.sorted-asc:after,gl-table thead th.sorted-desc:before{content:\"\"}gl-table thead th.sorted-desc:after{color:var(--ion-color-primary,#3880ff)}"; }, enumerable: !0, configurable: !0 }), t; }();
exports.GlTable = Table;
