import * as i0 from '@angular/core';
import { Component, Input, NgModule } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import * as i1 from 'primeng/table';
import * as i2 from '@seniorsistemas/angular-components/checkbox-list';
import { CheckboxListModule } from '@seniorsistemas/angular-components/checkbox-list';
import * as i3 from '@angular/forms';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';

class TableHeaderCheckboxComponent {
    table;
    tableService;
    static nextId = 0;
    id = `s-table-header-checkbox-${TableHeaderCheckboxComponent.nextId++}`;
    disabled = false;
    rowProps;
    useAllObject;
    checkboxItems = {
        label: '',
    };
    checked = false;
    hasFocus = false;
    checkboxState = { checked: false };
    _ngUnsubscribe = new Subject();
    constructor(table, tableService) {
        this.table = table;
        this.tableService = tableService;
        this.table.tableService.valueSource$.pipe(takeUntil(this._ngUnsubscribe)).subscribe(() => {
            this.checked = this.updateCheckedState();
        });
        this.table.tableService.selectionSource$.pipe(takeUntil(this._ngUnsubscribe)).subscribe(() => {
            this.checked = this.updateCheckedState();
        });
    }
    ngOnInit() {
        this.checked = this.updateCheckedState();
    }
    ngOnDestroy() {
        this._ngUnsubscribe.next();
    }
    onFocus() {
        this.hasFocus = true;
    }
    onBlur() {
        this.hasFocus = false;
    }
    isDisabled() {
        return this.disabled || !this.table.value || !this.table.value.length;
    }
    onClick() {
        if (!this.disabled) {
            if (this.table.value && this.table.value.length > 0)
                this.onToggleCheckbox(!this.checked);
        }
    }
    updateCheckedState() {
        if (this.table.filteredValue) {
            const val = this.table.filteredValue;
            return val && val.length > 0 && this.table.selection && this.table.selection.length > 0 && this.isAllFilteredValuesChecked();
        }
        else {
            const val = this.table.value;
            return val && val.length > 0 && this.table.selection && this.table.selection.length > 0 && this.isAllValuesChecked();
        }
    }
    onToggleCheckbox(checked) {
        const { filteredValue, value, dataKey } = this.table;
        const gridData = checked ? (filteredValue ? filteredValue.slice() : value.slice()) : [];
        if (!dataKey)
            return;
        if (checked) {
            const unselecteds = gridData
                .filter((record) => !this.table._selection.find((selected) => record[dataKey] === selected[dataKey]))
                .map((record) => {
                if (this.useAllObject)
                    return record;
                const newRecord = {};
                if (this.rowProps)
                    this.rowProps.forEach((prop) => (newRecord[prop] = record[prop]));
                else
                    newRecord[dataKey] = record[dataKey];
                return newRecord;
            });
            this.table._selection = [...this.table._selection, ...unselecteds];
        }
        else {
            value.forEach((data) => {
                this.table._selection = this.table._selection.filter((record) => record[dataKey] !== data[dataKey]);
            });
        }
        this.table.updateSelectionKeys();
        this.table.selectionChange.emit(this.table._selection);
        this.tableService.onSelectionChange();
        if (this.table.isStateful())
            this.table.saveState();
    }
    isAllFilteredValuesChecked() {
        if (!this.table.filteredValue) {
            return false;
        }
        else {
            for (const rowData of this.table.filteredValue) {
                if (!this.table.isSelected(rowData)) {
                    return false;
                }
            }
            return true;
        }
    }
    isAllValuesChecked() {
        if (!this.table.value) {
            return false;
        }
        else {
            for (const rowData of this.table.value) {
                if (!this.table.isSelected(rowData)) {
                    return false;
                }
            }
            return true;
        }
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: TableHeaderCheckboxComponent, deps: [{ token: i1.Table }, { token: i1.TableService }], target: i0.ɵɵFactoryTarget.Component });
    static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.14", type: TableHeaderCheckboxComponent, selector: "s-table-header-checkbox", inputs: { id: "id", disabled: "disabled", rowProps: "rowProps", useAllObject: "useAllObject" }, ngImport: i0, template: `
        <s-checkbox-list
            [(ngModel)]="checkboxState"
            (ngModelChange)="onClick()"
            [data]="checkboxItems"
        />
    `, isInline: true, dependencies: [{ kind: "component", type: i2.CheckboxListComponent, selector: "s-checkbox-list", inputs: ["data"] }, { kind: "directive", type: i3.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i3.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: TableHeaderCheckboxComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 's-table-header-checkbox',
                    template: `
        <s-checkbox-list
            [(ngModel)]="checkboxState"
            (ngModelChange)="onClick()"
            [data]="checkboxItems"
        />
    `,
                }]
        }], ctorParameters: () => [{ type: i1.Table }, { type: i1.TableService }], propDecorators: { id: [{
                type: Input
            }], disabled: [{
                type: Input
            }], rowProps: [{
                type: Input
            }], useAllObject: [{
                type: Input
            }] } });

class TableHeaderCheckboxModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: TableHeaderCheckboxModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: TableHeaderCheckboxModule, declarations: [TableHeaderCheckboxComponent], imports: [CommonModule, CheckboxListModule, FormsModule], exports: [TableHeaderCheckboxComponent] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: TableHeaderCheckboxModule, imports: [CommonModule, CheckboxListModule, FormsModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: TableHeaderCheckboxModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule, CheckboxListModule, FormsModule],
                    exports: [TableHeaderCheckboxComponent],
                    declarations: [TableHeaderCheckboxComponent],
                }]
        }] });

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

export { TableHeaderCheckboxComponent, TableHeaderCheckboxModule };
//# sourceMappingURL=seniorsistemas-angular-components-table-header.mjs.map
