import * as i0 from '@angular/core';
import { isSignal, inject, input, booleanAttribute, ElementRef, HostListener, HostBinding, Directive, NgModule } from '@angular/core';
import { DatatableComponent } from '@siemens/ngx-datatable';

/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
const unwrapSignalOrValue = (valueOrSignal) => {
    if (isSignal(valueOrSignal)) {
        return valueOrSignal();
    }
    return valueOrSignal;
};
class SiDatatableInteractionDirective {
    table = inject(DatatableComponent, { self: true });
    /**
     * Automatically select every row or cell that is navigated trough.
     * Is ignored unless `selectionType` is `single` or `cell`.
     *
     * @defaultValue false
     */
    datatableInteractionAutoSelect = input(false, { ...(ngDevMode ? { debugName: "datatableInteractionAutoSelect" } : {}), transform: booleanAttribute });
    tabIndex = '0';
    element = inject(ElementRef).nativeElement;
    tableBody;
    autoSelectTimeout;
    isMousedown = false;
    onKeydown(event) {
        if (event.key === 'ArrowDown') {
            const first = unwrapSignalOrValue(this.table.selectionType) === 'cell'
                ? this.element.querySelector('.datatable-row-wrapper > .datatable-body-row .datatable-body-cell')
                : this.element.querySelector('.datatable-row-wrapper > .datatable-body-row');
            if (first) {
                first.focus();
                event.preventDefault();
            }
        }
        else if (event.key === 'ArrowUp') {
            const last = unwrapSignalOrValue(this.table.selectionType) === 'cell'
                ? this.element.querySelector('.datatable-row-wrapper:last-child > .datatable-body-row .datatable-body-cell')
                : this.element.querySelector('.datatable-row-wrapper:last-child > .datatable-body-row');
            if (last) {
                last.focus();
                event.preventDefault();
            }
        }
    }
    onMousedown(event) {
        this.isMousedown = true;
    }
    onMouseup(event) {
        this.isMousedown = false;
    }
    onFocusin(event) {
        const target = event.target;
        if (!target) {
            return;
        }
        clearTimeout(this.autoSelectTimeout);
        // Re-select on every element
        const selectionType = unwrapSignalOrValue(this.table.selectionType);
        if (!this.isMousedown &&
            this.datatableInteractionAutoSelect() &&
            (selectionType === 'single' || selectionType === 'cell')) {
            const rowOrCell = target.closest(selectionType === 'cell' ? 'datatable-body-cell' : 'datatable-body-row');
            if (!rowOrCell) {
                return;
            }
            this.autoSelectTimeout = setTimeout(() => {
                rowOrCell.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', keyCode: 13 }));
            }, 100);
        }
        if (unwrapSignalOrValue(this.table.virtualization)) {
            if (this.tableBody) {
                const lastList = selectionType === 'cell'
                    ? this.tableBody.querySelectorAll('.datatable-row-wrapper:last-child > .datatable-body-row .datatable-body-cell')
                    : this.tableBody.querySelectorAll('.datatable-row-wrapper:last-child > .datatable-body-row');
                if (Array.from(lastList).includes(target)) {
                    this.tableBody.scrollTop = this.tableBody.scrollTop + lastList[0].clientHeight;
                }
                else {
                    const firstList = selectionType === 'cell'
                        ? this.tableBody.querySelectorAll('.datatable-row-wrapper:first-child > .datatable-body-row .datatable-body-cell')
                        : this.tableBody.querySelectorAll('.datatable-row-wrapper:first-child > .datatable-body-row');
                    if (Array.from(firstList).includes(target)) {
                        this.tableBody.scrollTop = this.tableBody.scrollTop - firstList[0].clientHeight;
                    }
                }
            }
        }
    }
    ngOnInit() {
        this.tableBody = this.element.querySelector('datatable-body');
        if (this.tableBody) {
            this.tableBody.tabIndex = -1;
        }
    }
    ngOnDestroy() {
        clearTimeout(this.autoSelectTimeout);
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.8", ngImport: i0, type: SiDatatableInteractionDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
    static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.0.8", type: SiDatatableInteractionDirective, isStandalone: true, selector: "ngx-datatable[siDatatableInteraction]", inputs: { datatableInteractionAutoSelect: { classPropertyName: "datatableInteractionAutoSelect", publicName: "datatableInteractionAutoSelect", isSignal: true, isRequired: false, transformFunction: null } }, host: { listeners: { "keydown": "onKeydown($event)", "mousedown": "onMousedown($event)", "mouseup": "onMouseup($event)", "focusin": "onFocusin($event)" }, properties: { "attr.tabindex": "this.tabIndex" } }, exportAs: ["si-datatable-interaction"], ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.8", ngImport: i0, type: SiDatatableInteractionDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: 'ngx-datatable[siDatatableInteraction]',
                    exportAs: 'si-datatable-interaction'
                }]
        }], propDecorators: { datatableInteractionAutoSelect: [{ type: i0.Input, args: [{ isSignal: true, alias: "datatableInteractionAutoSelect", required: false }] }], tabIndex: [{
                type: HostBinding,
                args: ['attr.tabindex']
            }], onKeydown: [{
                type: HostListener,
                args: ['keydown', ['$event']]
            }], onMousedown: [{
                type: HostListener,
                args: ['mousedown', ['$event']]
            }], onMouseup: [{
                type: HostListener,
                args: ['mouseup', ['$event']]
            }], onFocusin: [{
                type: HostListener,
                args: ['focusin', ['$event']]
            }] } });

/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
class SiDatatableModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.8", ngImport: i0, type: SiDatatableModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "21.0.8", ngImport: i0, type: SiDatatableModule, imports: [SiDatatableInteractionDirective], exports: [SiDatatableInteractionDirective] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "21.0.8", ngImport: i0, type: SiDatatableModule });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.8", ngImport: i0, type: SiDatatableModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [SiDatatableInteractionDirective],
                    exports: [SiDatatableInteractionDirective]
                }]
        }] });

const SI_DATATABLE_CONFIG = {
    cssClasses: {
        sortAscending: 'icon element-sort-up text-primary',
        sortDescending: 'icon element-sort-down text-primary',
        pagerLeftArrow: 'icon element-left-2 flip-rtl',
        pagerRightArrow: 'icon element-right-2 flip-rtl',
        pagerPrevious: 'icon element-double-left flip-rtl',
        pagerNext: 'icon element-double-right flip-rtl',
        sortUnset: '',
        treeStatusLoading: '',
        treeStatusExpanded: 'icon element-down-2 flip-rtl',
        treeStatusCollapsed: 'icon element-right-2 flip-rtl'
    },
    headerHeight: 40,
    footerHeight: 40,
    rowHeight: 64,
    rowHeightSmall: 48,
    rowHeightExtraSmall: 32,
    rowHeightTiny: 24,
    summaryHeight: 32
};
/**
 * Provides element configuration for the \@siemens/ngx-datatable.
 *
 *  @param configOverrides - overrides that will be merged with the element configuration.
 */
const provideSiDatatableConfig = (configOverrides) => ({
    provide: 'configuration',
    useValue: { ...SI_DATATABLE_CONFIG, ...configOverrides }
});

/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */

/**
 * Generated bundle index. Do not edit.
 */

export { SI_DATATABLE_CONFIG, SiDatatableInteractionDirective, SiDatatableModule, provideSiDatatableConfig };
//# sourceMappingURL=siemens-element-ng-datatable.mjs.map
