/**
 * @fileoverview added by tsickle
 * Generated from: lib/table/table.component.ts
 * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
import { ChangeDetectionStrategy, Component, ContentChildren, Directive, Input, QueryList, TemplateRef, ViewChild, ViewEncapsulation } from '@angular/core';
import { catchError, debounceTime, distinctUntilChanged, map, startWith, switchMap, tap } from 'rxjs/operators';
import { ApiDataSource, DataSourceRequest } from '@bravobit/bb-foundation/api';
import { BbTableHeaderCell } from '../table-header-cell/table-header-cell.component';
import { combineLatest, of } from 'rxjs';
import { FormControl } from '@angular/forms';
export class BbTableColumnDef {
}
BbTableColumnDef.decorators = [
    { type: Directive, args: [{
                selector: '[bbTableColumnDef]'
            },] }
];
export class BbTable {
    constructor() {
        // Inputs.
        this.identifier = null;
        this.queryFields = [];
        // Sizes.
        this._sizes = [25, 50, 75, 100];
        // Templates.
        this._header = true;
        this._footer = true;
        // Optimizations.
        this.trackByFn = (/**
         * @param {?} index
         * @param {?} item
         * @return {?}
         */
        (index, item) => this.identifier ? item[this.identifier] : index);
        this.trackByValue = (/**
         * @param {?} index
         * @param {?} item
         * @return {?}
         */
        (index, item) => item);
        // State.
        this.queryControl = new FormControl('');
        this.sizeControl = new FormControl(this._sizes[0]);
        this.pageControl = new FormControl(1);
    }
    /**
     * @return {?}
     */
    get sizes() {
        return this._sizes;
    }
    /**
     * @param {?} newValue
     * @return {?}
     */
    set sizes(newValue) {
        this.updateSizes(newValue);
    }
    /**
     * @return {?}
     */
    get header() {
        // If the header equals false just return null.
        if (!this._header) {
            return null;
        }
        // If the header is true use the default header.
        if (this._header === true) {
            return this.headerTemplate;
        }
        // It must be a custom header, use this.
        return this._header;
    }
    /**
     * @param {?} newValue
     * @return {?}
     */
    set header(newValue) {
        this._header = newValue;
    }
    /**
     * @return {?}
     */
    get footer() {
        // If the footer equals false just return null.
        if (!this._footer) {
            return null;
        }
        // If the footer is true use the default header.
        if (this._footer === true) {
            return this.footerTemplate;
        }
        // It must be a custom footer, use this.
        return this._footer;
    }
    /**
     * @param {?} newValue
     * @return {?}
     */
    set footer(newValue) {
        this._footer = newValue;
    }
    /**
     * @return {?}
     */
    ngOnInit() {
        // Validate the data source exists.
        if (!this.dataSource) {
            throw new Error('The data source was not passed properly.');
        }
        // Create a new request.
        this.request = new DataSourceRequest();
        this.isLoading$ = this.request.loading;
        // Pass the request to the data source.
        this.data$ = this.dataSource.handle(this.request).pipe(tap((/**
         * @return {?}
         */
        () => this.request.stopLoading())), map((/**
         * @param {?} result
         * @return {?}
         */
        result => {
            /** @type {?} */
            const count = (result && result.data && result.data.length) || 0;
            if (count <= 0) {
                return { state: 'success-empty', result };
            }
            return { state: 'success-all', result };
        })), catchError((/**
         * @param {?} error
         * @return {?}
         */
        (error) => {
            this.request.stopLoading();
            if (error.status === 404) {
                return of({ state: 'error-not-found' });
            }
            if (error.status === 500) {
                return of({ state: 'error-server' });
            }
            return of({ state: 'error-unknown' });
        })));
    }
    /**
     * @return {?}
     */
    ngAfterViewInit() {
        /** @type {?} */
        const queryChanges$ = this.queryControl.valueChanges.pipe(debounceTime(400), distinctUntilChanged(), startWith([this.queryControl.value]));
        /** @type {?} */
        const sortChanges$ = this.headerCells.changes.pipe(startWith(this.headerCells.toArray()), switchMap((/**
         * @param {?} headerCells
         * @return {?}
         */
        headerCells => combineLatest(headerCells.map((/**
         * @param {?} cell
         * @return {?}
         */
        cell => cell.state))))), map((/**
         * @param {?} cells
         * @return {?}
         */
        cells => cells.filter((/**
         * @param {?} cell
         * @return {?}
         */
        cell => !!cell['name'] && cell['direction'] !== 'none')))));
        /** @type {?} */
        const sizeChanges$ = this.sizeControl.valueChanges.pipe(startWith([this.sizeControl.value]));
        /** @type {?} */
        const pageChanges$ = this.pageControl.valueChanges.pipe(startWith([this.pageControl.value]));
        this._subscription = combineLatest([
            queryChanges$,
            sortChanges$,
            sizeChanges$,
            pageChanges$
        ]).subscribe((/**
         * @param {?} __0
         * @return {?}
         */
        ([query, sort, pageSize, pageNumber]) => {
            this.request.startLoading();
            return this.request.update({
                query,
                pageSize,
                pageNumber,
                queryFields: this.queryFields,
                sort: (/** @type {?} */ (sort))
            });
        }));
    }
    /**
     * @return {?}
     */
    ngOnDestroy() {
        this._subscription && this._subscription.unsubscribe();
    }
    /**
     * @return {?}
     */
    refresh() {
        return this.request.update();
    }
    /**
     * @private
     * @param {?} newValue
     * @return {?}
     */
    updateSizes(newValue) {
        // Validate the sizes has at least one value.
        if (newValue.length <= 0) {
            throw new Error('Cannot set an empty array as the sizes.');
        }
        // Set the new size.
        this._sizes = newValue;
        // Check if the current selected value is
        // in the new sizes array.
        if (this._sizes.includes(this.sizeControl.value)) {
            return;
        }
        // Set the first value in the array as
        // the new value.
        /** @type {?} */
        const firstValue = this._sizes[0];
        this.sizeControl.setValue(firstValue);
    }
}
BbTable.decorators = [
    { type: Component, args: [{
                selector: 'bb-table',
                template: "<!--\r\n    This is the header of the table. It can be a user\r\n    defined custom header or just the default header.\r\n-->\r\n\r\n<header *ngIf=\"header as headerTemplate\"\r\n        class=\"bb-table-top-actions\">\r\n    <ng-container *ngTemplateOutlet=\"headerTemplate\"></ng-container>\r\n</header>\r\n\r\n<!--\r\n    This is the table that contains the\r\n    rows and columns.\r\n-->\r\n\r\n<div class=\"bb-table-wrapper\">\r\n    <div *ngIf=\"isLoading$ | async\"\r\n         class=\"bb-table-progress\">\r\n        <div class=\"bb-table-indeterminate\"></div>\r\n    </div>\r\n    <table class=\"bb-table-container\">\r\n        <thead>\r\n            <tr>\r\n                <ng-content select=\"bb-table-header-cell\"></ng-content>\r\n            </tr>\r\n        </thead>\r\n        <tbody *ngIf=\"data$ | async as data; else isLoadingTemplate\"\r\n               [ngSwitch]=\"data?.state\">\r\n            <ng-container *ngSwitchCase=\"'success-all'\">\r\n                <tr *ngFor=\"let item of data?.result?.data; trackBy: trackByFn\">\r\n                    <ng-container *ngFor=\"let template of tableColumnTemplates\">\r\n                        <ng-container *ngTemplateOutlet=\"template; context: {item: item}\"></ng-container>\r\n                    </ng-container>\r\n                </tr>\r\n            </ng-container>\r\n\r\n            <ng-container *ngSwitchCase=\"'success-empty'\">\r\n                <ng-container\r\n                    *ngTemplateOutlet=\"errorTemplate; context: {content: 'tables.empty'}\"></ng-container>\r\n            </ng-container>\r\n\r\n            <ng-container *ngSwitchCase=\"'error-not-found'\">\r\n                <ng-container\r\n                    *ngTemplateOutlet=\"errorTemplate; context: {content: 'tables.error_not_found'}\"></ng-container>\r\n            </ng-container>\r\n\r\n            <ng-container *ngSwitchCase=\"'error-server'\">\r\n                <ng-container\r\n                    *ngTemplateOutlet=\"errorTemplate; context: {content: 'tables.error_server'}\"></ng-container>\r\n            </ng-container>\r\n\r\n            <ng-container *ngSwitchDefault>\r\n                <ng-container\r\n                    *ngTemplateOutlet=\"errorTemplate; context: {content: 'tables.error_unknown'}\"></ng-container>\r\n            </ng-container>\r\n        </tbody>\r\n    </table>\r\n</div>\r\n\r\n<footer *ngIf=\"footer as footerTemplate\"\r\n        class=\"bb-table-bottom-actions\">\r\n    <ng-container *ngTemplateOutlet=\"footerTemplate\"></ng-container>\r\n</footer>\r\n\r\n<ng-template #isLoadingTemplate>\r\n    <tr>\r\n        <td [attr.colspan]=\"tableColumnTemplates?.length\"\r\n            class=\"bb-table-template\">\r\n            <div class=\"bb-table-template-content\">\r\n                {{ 'tables.loading' | bbLocalize }}\r\n            </div>\r\n        </td>\r\n    </tr>\r\n</ng-template>\r\n\r\n<!--\r\n    This is the template that will be used when the user has not\r\n    supplied a custom header template.\r\n-->\r\n\r\n<ng-template #headerTemplate>\r\n    <ng-content></ng-content>\r\n    <bb-form-control>\r\n        <input [formControl]=\"queryControl\"\r\n               [placeholder]=\"'tables.search' | bbLocalize\"\r\n               bbInput\r\n               autocomplete=\"off\">\r\n        <bb-icon *ngIf=\"queryControl?.value?.length <= 0; else closeButtonTemplate\"\r\n                 bbSuffix\r\n                 src=\"material:search\">\r\n        </bb-icon>\r\n    </bb-form-control>\r\n</ng-template>\r\n\r\n<!--\r\n    This is the template that will be used when the user has not\r\n    supplied a custom footer template.\r\n-->\r\n\r\n<ng-template #footerTemplate>\r\n    <bb-form-control *ngIf=\"sizes?.length > 1\">\r\n        <select [formControl]=\"sizeControl\"\r\n                bbInput\r\n                title=\"Sizes\">\r\n            <option *ngFor=\"let item of sizes; trackBy: trackByValue\"\r\n                    [ngValue]=\"item\">\r\n                {{ item }} / {{ 'tables.page' | bbLocalize }}\r\n            </option>\r\n        </select>\r\n    </bb-form-control>\r\n\r\n    <bb-table-pager [totalPages]=\"(data$ | async)?.result?.totalPages\"\r\n                    [formControl]=\"pageControl\">\r\n    </bb-table-pager>\r\n</ng-template>\r\n\r\n<!--\r\n    This the template that will be used when the user\r\n    has filled in some data in the query control.\r\n-->\r\n\r\n<ng-template #closeButtonTemplate>\r\n    <button bbSuffix\r\n            (click)=\"queryControl?.patchValue('')\"\r\n            type=\"button\"\r\n            class=\"bb-table-progress-close-query\">\r\n        <bb-icon src=\"material:close\"></bb-icon>\r\n    </button>\r\n</ng-template>\r\n\r\n<ng-template #errorTemplate let-content=\"content\">\r\n    <tr>\r\n        <td [attr.colspan]=\"tableColumnTemplates?.length\"\r\n            class=\"bb-table-template\">\r\n            <div class=\"bb-table-template-content\">\r\n                <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"64\" height=\"41\">\r\n                    <g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(0 1)\">\r\n                        <ellipse cx=\"32\" cy=\"33\" fill=\"#F5F5F5\" rx=\"32\" ry=\"7\"></ellipse>\r\n                        <g fill-rule=\"nonzero\" stroke=\"#D9D9D9\">\r\n                            <path\r\n                                d=\"M55 12.7605604L44.8543047 1.25739633C44.3674414.47382661 43.6558789 0 42.9067617 0H21.0932383c-.7491172 0-1.4606797.47395025-1.947543 1.25739633L9 12.7605604V22h46v-9.2394396z\"></path>\r\n                            <path fill=\"#FAFAFA\"\r\n                                  d=\"M41.6132813 15.9315c0-1.6056489.9936718-2.931266 2.2264179-2.9315H55v18.1371277C55 33.2589574 53.6793867 35 52.0504297 35H11.94957031C10.32052344 35 9 33.2588404 9 31.1371277V13h11.1603008c1.2327461 0 2.226418 1.3228085 2.226418 2.9284574v.0211809c0 1.6056489 1.0049921 2.9015426 2.2377382 2.9015426h14.751086c1.2327461 0 2.2377383-1.3078298 2.2377383-2.9134788V15.9315z\"></path>\r\n                        </g>\r\n                    </g>\r\n                </svg>\r\n                {{ content | bbLocalize }}\r\n            </div>\r\n        </td>\r\n    </tr>\r\n</ng-template>\r\n",
                host: { 'class': 'bb-table' },
                changeDetection: ChangeDetectionStrategy.OnPush,
                encapsulation: ViewEncapsulation.None,
                preserveWhitespaces: false,
                styles: [".bb-table{width:100%;display:block;box-shadow:0 6px 6px -6px rgba(0,0,0,.1)}.bb-table.plain{box-shadow:none}.bb-table.plain>.bb-table-wrapper{border-top-left-radius:0;border-top-right-radius:0}.bb-table-wrapper{overflow-x:auto;position:relative;background-color:#fff;border-top-left-radius:4px;border-top-right-radius:4px}.bb-table-container{width:100%}.bb-table-container td{border-bottom:1px solid #d8d8d8}.bb-table-top-actions{display:-ms-flexbox;display:flex;-ms-flex-wrap:wrap;flex-wrap:wrap;-ms-flex-direction:column;flex-direction:column;padding:20px 20px 10px;background-color:#fff;-ms-flex-pack:end;justify-content:flex-end;border-top-left-radius:4px;border-top-right-radius:4px;border-bottom:1px solid #d8d8d8}.bb-table-top-actions>*{margin-left:0;margin-bottom:10px}.bb-table-top-actions+.bb-table-wrapper{border-radius:0}.bb-table-bottom-actions{height:60px;display:-ms-flexbox;display:flex;padding:0 20px;overflow-x:auto;-ms-flex-align:center;align-items:center;background-color:#fff;border-bottom-left-radius:4px;border-bottom-right-radius:4px}.bb-table-template{border:none;background-color:#f5f5f5;border-bottom:1px solid #d8d8d8}.bb-table-template-content{padding:20px;display:-ms-flexbox;display:flex;color:#3d464d;font-size:16px;line-height:1.2;font-weight:500;min-height:140px;text-align:center;-ms-flex-align:center;align-items:center;-ms-flex-direction:column;flex-direction:column;-ms-flex-pack:center;justify-content:center}.bb-table-template-content>svg{margin-bottom:12px}.bb-table-progress{top:0;left:0;right:0;opacity:0;width:100%;height:4px;display:block;overflow:hidden;position:absolute;background-color:rgba(47,64,139,.3);-webkit-animation:250ms cubic-bezier(0,0,.2,1) .1s forwards fadeIn;animation:250ms cubic-bezier(0,0,.2,1) .1s forwards fadeIn}.bb-table-progress .bb-table-indeterminate{background-color:#2f408b}.bb-table-progress .bb-table-indeterminate:before{top:0;left:0;bottom:0;content:\"\";position:absolute;will-change:left,right;background-color:inherit;-webkit-animation:2.1s cubic-bezier(.65,.815,.735,.395) infinite indeterminate;animation:2.1s cubic-bezier(.65,.815,.735,.395) infinite indeterminate}.bb-table-progress .bb-table-indeterminate:after{top:0;left:0;bottom:0;content:\"\";position:absolute;-webkit-animation-delay:1.15s;animation-delay:1.15s;will-change:left,right;background-color:inherit;-webkit-animation:2.1s cubic-bezier(.165,.84,.44,1) infinite indeterminate-short;animation:2.1s cubic-bezier(.165,.84,.44,1) infinite indeterminate-short}.bb-table-progress-close-query{padding:0;border:none;border-radius:4px;background-color:transparent;transition:background-color 250ms cubic-bezier(0,0,.2,1)}.bb-table-progress-close-query:hover{background-color:rgba(0,0,0,.08)}.bb-table-progress-close-query:focus{background-color:rgba(0,0,0,.12)}.bb-table-progress-close-query:active{background-color:rgba(0,0,0,.16)}@-webkit-keyframes fadeIn{from{opacity:0}to{opacity:1}}@keyframes fadeIn{from{opacity:0}to{opacity:1}}@-webkit-keyframes indeterminate{0%{left:-35%;right:100%}100%,60%{left:100%;right:-90%}}@keyframes indeterminate{0%{left:-35%;right:100%}100%,60%{left:100%;right:-90%}}@-webkit-keyframes indeterminate-short{0%{left:-200%;right:100%}100%,60%{left:107%;right:-8%}}@keyframes indeterminate-short{0%{left:-200%;right:100%}100%,60%{left:107%;right:-8%}}@media only screen and (min-width:768px){.bb-table-top-actions{-ms-flex-direction:row;flex-direction:row}.bb-table-top-actions>:not(:first-child){margin-left:10px;margin-bottom:10px}}"]
            }] }
];
BbTable.propDecorators = {
    headerCells: [{ type: ContentChildren, args: [BbTableHeaderCell,] }],
    tableColumnTemplates: [{ type: ContentChildren, args: [BbTableColumnDef, { read: TemplateRef },] }],
    headerTemplate: [{ type: ViewChild, args: ['headerTemplate', { static: true, read: TemplateRef },] }],
    footerTemplate: [{ type: ViewChild, args: ['footerTemplate', { static: true, read: TemplateRef },] }],
    identifier: [{ type: Input }],
    dataSource: [{ type: Input }],
    queryFields: [{ type: Input }],
    sizes: [{ type: Input }],
    header: [{ type: Input }],
    footer: [{ type: Input }]
};
if (false) {
    /** @type {?} */
    BbTable.prototype.headerCells;
    /** @type {?} */
    BbTable.prototype.tableColumnTemplates;
    /** @type {?} */
    BbTable.prototype.headerTemplate;
    /** @type {?} */
    BbTable.prototype.footerTemplate;
    /** @type {?} */
    BbTable.prototype.identifier;
    /** @type {?} */
    BbTable.prototype.dataSource;
    /** @type {?} */
    BbTable.prototype.queryFields;
    /**
     * @type {?}
     * @private
     */
    BbTable.prototype._sizes;
    /**
     * @type {?}
     * @private
     */
    BbTable.prototype._header;
    /**
     * @type {?}
     * @private
     */
    BbTable.prototype._footer;
    /** @type {?} */
    BbTable.prototype.trackByFn;
    /** @type {?} */
    BbTable.prototype.trackByValue;
    /** @type {?} */
    BbTable.prototype.request;
    /** @type {?} */
    BbTable.prototype.data$;
    /** @type {?} */
    BbTable.prototype.isLoading$;
    /** @type {?} */
    BbTable.prototype.queryControl;
    /** @type {?} */
    BbTable.prototype.sizeControl;
    /** @type {?} */
    BbTable.prototype.pageControl;
    /**
     * @type {?}
     * @private
     */
    BbTable.prototype._subscription;
}
//# sourceMappingURL=data:application/json;base64, 