/**
 * Copyright Aquera Inc 2023
 *
 * This source code is licensed under the BSD-3-Clause license found in the
 * LICENSE file in the root directory of this source tree.
 */
import { __decorate } from "tslib";
import { html, } from 'lit';
import { customElement, query, state, queryAssignedNodes, property } from 'lit/decorators.js';
import { styles } from './nile-table-body.css';
import NileElement from '../internal/nile-element';
import { hasResizableColumns } from '../internal/resizable-table-helper';
import { ifDefined } from 'lit/directives/if-defined.js';
/**
 * nile-table-body component.
 *
 * @tag nile-table-body
 *
 */
let NileTableBody = class NileTableBody extends NileElement {
    constructor() {
        /**
         * The styles for TableBody
         * @remarks If you are extending this class you can extend the base styles with super. Eg `return [super(), myCustomStyles]`
         */
        super(...arguments);
        this.type = 'primary';
        this.rows_data = [];
        this.header_rows_data = [];
        this.hasResizableColumns = false;
        /* #endregion */
    }
    static get styles() {
        return [styles];
    }
    firstUpdated(_changedProperties) {
        this.emit('nile-body-load', { value: this.type, comp: this }, true, false);
    }
    /* #endregion */
    /* #region Methods */
    getChildren(child, tagname) {
        return child.querySelectorAll(tagname);
    }
    getIndexValue(index__value) {
        return this.header_rows_data.indexOf(index__value);
    }
    checkForResizableColumns() {
        this.hasResizableColumns = hasResizableColumns(this);
    }
    handleSlotchange(e) {
        let row_data = [];
        this._childNodes.forEach((child) => {
            if (child.tagName && child?.tagName?.toLowerCase() === 'nile-table-row') {
                let nilerows = this.getChildren(child, 'nile-table-cell-item');
                if (nilerows.length > 0) {
                    nilerows.forEach((element) => {
                        row_data.push(element.innerHTML);
                    });
                    this.rows_data.push(row_data);
                }
                else {
                    let nileheaders = this.getChildren(child, 'nile-table-header-item');
                    if (nileheaders.length > 0) {
                        nileheaders.forEach((header, index) => {
                            this.header_rows_data.push(header.innerHTML);
                        });
                    }
                }
                row_data = [];
            }
        });
        this.checkForResizableColumns();
    }
    handleSort(e) {
        const { curr_sort_string, order } = e.detail.value;
        const header_index = this.getIndexValue(curr_sort_string);
        this.emit('nile-sort', {
            value: { header_index, curr_sort_string, order },
        });
    }
    handleSearch(e) {
        const searchText = e.detail.value;
        this.search__index = this.getIndexValue(e.target.textContent);
        this.emit('nile-table-search', {
            index: this.search__index,
            value: searchText,
        });
    }
    render() {
        return html `
      <div class="base" part="base">
        <slot name="table__header" class="table__header"></slot>
        <slot
          @slotchange=${this.handleSlotchange}
          @nile-click-sort=${this.handleSort}
          class="nile__table__body"
          style=${ifDefined(this.bodyStyle ? `display: ${this.bodyStyle}` : undefined)}
          ?resizable=${this.hasResizableColumns}
          @nile-search=${this.handleSearch}
        ></slot>
      </div>
    `;
    }
};
__decorate([
    query('slot')
], NileTableBody.prototype, "defaultSlot", void 0);
__decorate([
    queryAssignedNodes({ flatten: true })
], NileTableBody.prototype, "_childNodes", void 0);
__decorate([
    property()
], NileTableBody.prototype, "type", void 0);
__decorate([
    property()
], NileTableBody.prototype, "bodyStyle", void 0);
__decorate([
    state()
], NileTableBody.prototype, "rows_data", void 0);
__decorate([
    state()
], NileTableBody.prototype, "header_rows_data", void 0);
__decorate([
    state()
], NileTableBody.prototype, "sort__index", void 0);
__decorate([
    state()
], NileTableBody.prototype, "search__index", void 0);
__decorate([
    state()
], NileTableBody.prototype, "hasResizableColumns", void 0);
NileTableBody = __decorate([
    customElement('nile-table-body')
], NileTableBody);
export { NileTableBody };
export default NileTableBody;
//# sourceMappingURL=nile-table-body.js.map