import { Component, Input, ViewChild, ElementRef } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { EuiTableSelectableRowService } from '../services/eui-table-selectable-row.service';
import * as i0 from "@angular/core";
import * as i1 from "../services/eui-table-selectable-row.service";
import * as i2 from "@angular/common";
import * as i3 from "../../atoms/eui-icon/eui-icon.component";
const _c0 = ["checkbox"];
const _c1 = ["isSelectableHeader", ""];
function EuiTableSelectableHeaderComponent_th_0_eui_icon_1_Template(rf, ctx) { if (rf & 1) {
    const _r5 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "eui-icon", 5);
    i0.ɵɵlistener("click", function EuiTableSelectableHeaderComponent_th_0_eui_icon_1_Template_eui_icon_click_0_listener() { i0.ɵɵrestoreView(_r5); const ctx_r4 = i0.ɵɵnextContext(2); return ctx_r4.toggleCheckedState(true); });
    i0.ɵɵelementEnd();
} }
function EuiTableSelectableHeaderComponent_th_0_eui_icon_2_Template(rf, ctx) { if (rf & 1) {
    const _r7 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "eui-icon", 6);
    i0.ɵɵlistener("click", function EuiTableSelectableHeaderComponent_th_0_eui_icon_2_Template_eui_icon_click_0_listener() { i0.ɵɵrestoreView(_r7); const ctx_r6 = i0.ɵɵnextContext(2); return ctx_r6.toggleCheckedState(false); });
    i0.ɵɵelementEnd();
} }
function EuiTableSelectableHeaderComponent_th_0_eui_icon_3_Template(rf, ctx) { if (rf & 1) {
    const _r9 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "eui-icon", 7);
    i0.ɵɵlistener("click", function EuiTableSelectableHeaderComponent_th_0_eui_icon_3_Template_eui_icon_click_0_listener() { i0.ɵɵrestoreView(_r9); const ctx_r8 = i0.ɵɵnextContext(2); return ctx_r8.toggleCheckedState(false); });
    i0.ɵɵelementEnd();
} }
function EuiTableSelectableHeaderComponent_th_0_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "th", 1);
    i0.ɵɵtemplate(1, EuiTableSelectableHeaderComponent_th_0_eui_icon_1_Template, 1, 0, "eui-icon", 2);
    i0.ɵɵtemplate(2, EuiTableSelectableHeaderComponent_th_0_eui_icon_2_Template, 1, 0, "eui-icon", 3);
    i0.ɵɵtemplate(3, EuiTableSelectableHeaderComponent_th_0_eui_icon_3_Template, 1, 0, "eui-icon", 4);
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const ctx_r0 = i0.ɵɵnextContext();
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", !ctx_r0.isChecked && !ctx_r0.isCheckedIndeterminate);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", ctx_r0.isCheckedIndeterminate);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", ctx_r0.isChecked && !ctx_r0.isCheckedIndeterminate);
} }
const _c2 = ["*"];
export class EuiTableSelectableHeaderComponent {
    constructor(euiTableSelectableRowService) {
        this.euiTableSelectableRowService = euiTableSelectableRowService;
        this.isSelectableHeader = true;
        this.isChecked = false;
        this.isCheckedIndeterminate = false;
        this.destroy$ = new Subject();
    }
    ngOnInit() {
        this.euiTableSelectableRowService.isAllRowsSelected$.pipe(takeUntil(this.destroy$)).subscribe((selected) => {
            setTimeout(() => {
                this.isChecked = selected;
                if (this.checkbox) {
                    this.checkbox.nativeElement.checked = selected;
                }
                if (!selected && this.euiTableSelectableRowService.selectedRows$.value.length > 0) {
                    this.isCheckedIndeterminate = true;
                }
                else {
                    this.isCheckedIndeterminate = false;
                }
            });
        });
    }
    ngOnDestroy() {
        this.destroy$.next(true);
        this.destroy$.unsubscribe();
    }
    toggleCheckedState(value) {
        this.isChecked = value;
        if (this.isChecked) {
            this.euiTableSelectableRowService.selectAllRows();
        }
        else {
            this.euiTableSelectableRowService.unselectAllRows();
        }
    }
}
/** @nocollapse */ EuiTableSelectableHeaderComponent.ɵfac = function EuiTableSelectableHeaderComponent_Factory(t) { return new (t || EuiTableSelectableHeaderComponent)(i0.ɵɵdirectiveInject(i1.EuiTableSelectableRowService)); };
/** @nocollapse */ EuiTableSelectableHeaderComponent.ɵcmp = i0.ɵɵdefineComponent({ type: EuiTableSelectableHeaderComponent, selectors: [["tr", "isSelectableHeader", ""]], viewQuery: function EuiTableSelectableHeaderComponent_Query(rf, ctx) { if (rf & 1) {
        i0.ɵɵviewQuery(_c0, true);
    } if (rf & 2) {
        var _t;
        i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.checkbox = _t.first);
    } }, inputs: { isSelectableHeader: "isSelectableHeader", isChecked: "isChecked" }, attrs: _c1, ngContentSelectors: _c2, decls: 2, vars: 1, consts: [["class", "eui-u-color-grey-light eui-u-cursor-pointer", 4, "ngIf"], [1, "eui-u-color-grey-light", "eui-u-cursor-pointer"], ["iconClass", "eui-icon-checkbox-outline eui-u-font-size-2xl", "aria-label", "Check", 3, "click", 4, "ngIf"], ["euiPrimary", "", "iconClass", "eui-icon-checkbox-intermediate eui-u-font-size-2xl", "aria-label", "Uncheck", 3, "click", 4, "ngIf"], ["euiPrimary", "", "iconClass", "eui-icon-checkbox eui-u-font-size-2xl", "aria-label", "Uncheck", 3, "click", 4, "ngIf"], ["iconClass", "eui-icon-checkbox-outline eui-u-font-size-2xl", "aria-label", "Check", 3, "click"], ["euiPrimary", "", "iconClass", "eui-icon-checkbox-intermediate eui-u-font-size-2xl", "aria-label", "Uncheck", 3, "click"], ["euiPrimary", "", "iconClass", "eui-icon-checkbox eui-u-font-size-2xl", "aria-label", "Uncheck", 3, "click"]], template: function EuiTableSelectableHeaderComponent_Template(rf, ctx) { if (rf & 1) {
        i0.ɵɵprojectionDef();
        i0.ɵɵtemplate(0, EuiTableSelectableHeaderComponent_th_0_Template, 4, 3, "th", 0);
        i0.ɵɵprojection(1);
    } if (rf & 2) {
        i0.ɵɵproperty("ngIf", ctx.isSelectableHeader);
    } }, directives: [i2.NgIf, i3.EuiIconComponent], encapsulation: 2 });
/*@__PURE__*/ (function () { i0.ɵsetClassMetadata(EuiTableSelectableHeaderComponent, [{
        type: Component,
        args: [{
                // tslint:disable-next-line
                selector: 'tr[isSelectableHeader]',
                templateUrl: './eui-table-selectable-header.component.html',
            }]
    }], function () { return [{ type: i1.EuiTableSelectableRowService }]; }, { isSelectableHeader: [{
            type: Input
        }], isChecked: [{
            type: Input
        }], checkbox: [{
            type: ViewChild,
            args: ['checkbox']
        }] }); })();
//# sourceMappingURL=data:application/json;base64, 