var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
    function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
    var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
    var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
    var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
    var _, done = false;
    for (var i = decorators.length - 1; i >= 0; i--) {
        var context = {};
        for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
        for (var p in contextIn.access) context.access[p] = contextIn.access[p];
        context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
        var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
        if (kind === "accessor") {
            if (result === void 0) continue;
            if (result === null || typeof result !== "object") throw new TypeError("Object expected");
            if (_ = accept(result.get)) descriptor.get = _;
            if (_ = accept(result.set)) descriptor.set = _;
            if (_ = accept(result.init)) initializers.unshift(_);
        }
        else if (_ = accept(result)) {
            if (kind === "field") initializers.unshift(_);
            else descriptor[key] = _;
        }
    }
    if (target) Object.defineProperty(target, contextIn.name, descriptor);
    done = true;
};
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
    var useValue = arguments.length > 2;
    for (var i = 0; i < initializers.length; i++) {
        value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
    }
    return useValue ? value : void 0;
};
import { LitElement, html, css, property, state, customElement } from './plugins.js';
import { sharedStyles, tableStyles } from './sw-dash-styles.js';
/**
 * Base sortable table component for sw-dash
 */
let SwDashTable = (() => {
    let _classDecorators = [customElement('sw-dash-table')];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = LitElement;
    let _columns_decorators;
    let _columns_initializers = [];
    let _columns_extraInitializers = [];
    let _data_decorators;
    let _data_initializers = [];
    let _data_extraInitializers = [];
    let _filterPlaceholder_decorators;
    let _filterPlaceholder_initializers = [];
    let _filterPlaceholder_extraInitializers = [];
    let _infoLabel_decorators;
    let _infoLabel_initializers = [];
    let _infoLabel_extraInitializers = [];
    let _sortColumn_decorators;
    let _sortColumn_initializers = [];
    let _sortColumn_extraInitializers = [];
    let _sortDirection_decorators;
    let _sortDirection_initializers = [];
    let _sortDirection_extraInitializers = [];
    let _filterText_decorators;
    let _filterText_initializers = [];
    let _filterText_extraInitializers = [];
    var SwDashTable = class extends _classSuper {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _columns_decorators = [property({ type: Array })];
            _data_decorators = [property({ type: Array })];
            _filterPlaceholder_decorators = [property({ type: String })];
            _infoLabel_decorators = [property({ type: String })];
            _sortColumn_decorators = [state()];
            _sortDirection_decorators = [state()];
            _filterText_decorators = [state()];
            __esDecorate(this, null, _columns_decorators, { kind: "accessor", name: "columns", static: false, private: false, access: { has: obj => "columns" in obj, get: obj => obj.columns, set: (obj, value) => { obj.columns = value; } }, metadata: _metadata }, _columns_initializers, _columns_extraInitializers);
            __esDecorate(this, null, _data_decorators, { kind: "accessor", name: "data", static: false, private: false, access: { has: obj => "data" in obj, get: obj => obj.data, set: (obj, value) => { obj.data = value; } }, metadata: _metadata }, _data_initializers, _data_extraInitializers);
            __esDecorate(this, null, _filterPlaceholder_decorators, { kind: "accessor", name: "filterPlaceholder", static: false, private: false, access: { has: obj => "filterPlaceholder" in obj, get: obj => obj.filterPlaceholder, set: (obj, value) => { obj.filterPlaceholder = value; } }, metadata: _metadata }, _filterPlaceholder_initializers, _filterPlaceholder_extraInitializers);
            __esDecorate(this, null, _infoLabel_decorators, { kind: "accessor", name: "infoLabel", static: false, private: false, access: { has: obj => "infoLabel" in obj, get: obj => obj.infoLabel, set: (obj, value) => { obj.infoLabel = value; } }, metadata: _metadata }, _infoLabel_initializers, _infoLabel_extraInitializers);
            __esDecorate(this, null, _sortColumn_decorators, { kind: "accessor", name: "sortColumn", static: false, private: false, access: { has: obj => "sortColumn" in obj, get: obj => obj.sortColumn, set: (obj, value) => { obj.sortColumn = value; } }, metadata: _metadata }, _sortColumn_initializers, _sortColumn_extraInitializers);
            __esDecorate(this, null, _sortDirection_decorators, { kind: "accessor", name: "sortDirection", static: false, private: false, access: { has: obj => "sortDirection" in obj, get: obj => obj.sortDirection, set: (obj, value) => { obj.sortDirection = value; } }, metadata: _metadata }, _sortDirection_initializers, _sortDirection_extraInitializers);
            __esDecorate(this, null, _filterText_decorators, { kind: "accessor", name: "filterText", static: false, private: false, access: { has: obj => "filterText" in obj, get: obj => obj.filterText, set: (obj, value) => { obj.filterText = value; } }, metadata: _metadata }, _filterText_initializers, _filterText_extraInitializers);
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            SwDashTable = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        static styles = [
            sharedStyles,
            tableStyles,
            css `
      :host {
        display: block;
      }
    `
        ];
        #columns_accessor_storage = __runInitializers(this, _columns_initializers, []);
        get columns() { return this.#columns_accessor_storage; }
        set columns(value) { this.#columns_accessor_storage = value; }
        #data_accessor_storage = (__runInitializers(this, _columns_extraInitializers), __runInitializers(this, _data_initializers, []));
        get data() { return this.#data_accessor_storage; }
        set data(value) { this.#data_accessor_storage = value; }
        #filterPlaceholder_accessor_storage = (__runInitializers(this, _data_extraInitializers), __runInitializers(this, _filterPlaceholder_initializers, 'Filter...'));
        get filterPlaceholder() { return this.#filterPlaceholder_accessor_storage; }
        set filterPlaceholder(value) { this.#filterPlaceholder_accessor_storage = value; }
        #infoLabel_accessor_storage = (__runInitializers(this, _filterPlaceholder_extraInitializers), __runInitializers(this, _infoLabel_initializers, 'items'));
        get infoLabel() { return this.#infoLabel_accessor_storage; }
        set infoLabel(value) { this.#infoLabel_accessor_storage = value; }
        #sortColumn_accessor_storage = (__runInitializers(this, _infoLabel_extraInitializers), __runInitializers(this, _sortColumn_initializers, ''));
        get sortColumn() { return this.#sortColumn_accessor_storage; }
        set sortColumn(value) { this.#sortColumn_accessor_storage = value; }
        #sortDirection_accessor_storage = (__runInitializers(this, _sortColumn_extraInitializers), __runInitializers(this, _sortDirection_initializers, 'desc'));
        get sortDirection() { return this.#sortDirection_accessor_storage; }
        set sortDirection(value) { this.#sortDirection_accessor_storage = value; }
        #filterText_accessor_storage = (__runInitializers(this, _sortDirection_extraInitializers), __runInitializers(this, _filterText_initializers, ''));
        get filterText() { return this.#filterText_accessor_storage; }
        set filterText(value) { this.#filterText_accessor_storage = value; }
        // Utility formatters
        static formatNumber(n) {
            return n.toLocaleString();
        }
        static formatBytes(bytes) {
            if (bytes === 0)
                return '0 B';
            const k = 1024;
            const sizes = ['B', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }
        static formatTimestamp(ts) {
            if (!ts || ts === 0)
                return 'never';
            const ago = Date.now() - ts;
            if (ago < 60000)
                return Math.floor(ago / 1000) + 's ago';
            if (ago < 3600000)
                return Math.floor(ago / 60000) + 'm ago';
            if (ago < 86400000)
                return Math.floor(ago / 3600000) + 'h ago';
            return new Date(ts).toLocaleDateString();
        }
        static getGaugeClass(rate) {
            if (rate >= 80)
                return 'good';
            if (rate >= 50)
                return 'warning';
            return 'bad';
        }
        handleSort(column) {
            if (this.sortColumn === column) {
                this.sortDirection = this.sortDirection === 'asc' ? 'desc' : 'asc';
            }
            else {
                this.sortColumn = column;
                this.sortDirection = 'desc';
            }
        }
        handleFilter(e) {
            this.filterText = e.target.value;
        }
        getSortedFilteredData() {
            let result = [...this.data];
            // Filter
            if (this.filterText) {
                const search = this.filterText.toLowerCase();
                result = result.filter(row => {
                    return this.columns.some(col => {
                        const val = row[col.key];
                        if (val == null)
                            return false;
                        return String(val).toLowerCase().includes(search);
                    });
                });
            }
            // Sort
            if (this.sortColumn) {
                result.sort((a, b) => {
                    let valA = a[this.sortColumn];
                    let valB = b[this.sortColumn];
                    if (typeof valA === 'string')
                        valA = valA.toLowerCase();
                    if (typeof valB === 'string')
                        valB = valB.toLowerCase();
                    if (valA < valB)
                        return this.sortDirection === 'asc' ? -1 : 1;
                    if (valA > valB)
                        return this.sortDirection === 'asc' ? 1 : -1;
                    return 0;
                });
            }
            return result;
        }
        renderHitRateBar(rate) {
            const cls = SwDashTable.getGaugeClass(rate);
            return html `
      <span class="hit-rate-bar">
        <span class="hit-rate-fill ${cls}" style="width: ${rate}%"></span>
      </span>${rate}%
    `;
        }
        renderCellValue(value, row, column) {
            if (column.formatter) {
                return column.formatter(value, row);
            }
            // Special handling for hitRate
            if (column.key === 'hitRate') {
                return this.renderHitRateBar(value);
            }
            return value;
        }
        render() {
            const sortedData = this.getSortedFilteredData();
            return html `
      <div class="table-controls">
        <input
          type="text"
          class="search-input"
          placeholder="${this.filterPlaceholder}"
          .value="${this.filterText}"
          @input="${this.handleFilter}"
        >
        <span class="table-info">${sortedData.length} of ${this.data.length} ${this.infoLabel}</span>
      </div>
      <div class="table-container">
        <table class="data-table">
          <thead>
            <tr>
              ${this.columns.map(col => html `
                <th
                  class="${this.sortColumn === col.key ? 'sorted' : ''}"
                  @click="${() => col.sortable !== false && this.handleSort(col.key)}"
                >
                  ${col.label}
                  ${col.sortable !== false ? html `
                    <span class="sort-icon">${this.sortColumn === col.key && this.sortDirection === 'asc' ? '↑' : '↓'}</span>
                  ` : ''}
                </th>
              `)}
            </tr>
          </thead>
          <tbody>
            ${sortedData.map(row => html `
              <tr>
                ${this.columns.map(col => html `
                  <td class="${col.className || ''}">${this.renderCellValue(row[col.key], row, col)}</td>
                `)}
              </tr>
            `)}
          </tbody>
        </table>
      </div>
    `;
        }
        constructor() {
            super(...arguments);
            __runInitializers(this, _filterText_extraInitializers);
        }
        static {
            __runInitializers(_classThis, _classExtraInitializers);
        }
    };
    return SwDashTable = _classThis;
})();
export { SwDashTable };
//# sourceMappingURL=data:application/json;base64, 