/*
 * @license
 * Copyright Endlessjs. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { ChangeDetectorRef, Directive, ElementRef, HostBinding, Inject, PLATFORM_ID, } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { DomSanitizer } from '@angular/platform-browser';
import { filter, takeWhile } from 'rxjs/operators';
import { ElLayoutDirectionService } from '../../services/direction.service';
import { EL_WINDOW } from '../../theme.options';
import { ElCellDirective, ElFooterCellDirective, ElHeaderCellDirective } from '../cdk/table/cell';
import { ElCdkCell, ElCdkFooterCell, ElCdkHeaderCell } from '../cdk/table/type-mappings';
import { EL_TREE_GRID } from './tree-grid-injection-tokens';
import { ElTreeGridColumnDefDirective } from './tree-grid-column-def.directive';
import { EL_DEFAULT_ROW_LEVEL } from './data-source/tree-grid.model';
import { ElColumnsService } from './tree-grid-columns.service';
var ElTreeGridCellDirective = /** @class */ (function (_super) {
    __extends(ElTreeGridCellDirective, _super);
    function ElTreeGridCellDirective(columnDef, elementRef, tree, platformId, window, sanitizer, directionService, columnService, cd) {
        var _this = _super.call(this, columnDef, elementRef) || this;
        _this.platformId = platformId;
        _this.window = window;
        _this.sanitizer = sanitizer;
        _this.directionService = directionService;
        _this.columnService = columnService;
        _this.cd = cd;
        _this.alive = true;
        _this.initialLeftPadding = '';
        _this.initialRightPadding = '';
        _this.tree = tree;
        _this.columnDef = columnDef;
        _this.elementRef = elementRef;
        return _this;
    }
    ElTreeGridCellDirective_1 = ElTreeGridCellDirective;
    Object.defineProperty(ElTreeGridCellDirective.prototype, "columnWidth", {
        get: function () {
            this.latestWidth = this.tree.getColumnWidth();
            return this.latestWidth || null;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ElTreeGridCellDirective.prototype, "leftPadding", {
        get: function () {
            if (this.directionService.isLtr()) {
                return this.getStartPadding();
            }
            return null;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ElTreeGridCellDirective.prototype, "rightPadding", {
        get: function () {
            if (this.directionService.isRtl()) {
                return this.getStartPadding();
            }
            return null;
        },
        enumerable: true,
        configurable: true
    });
    ElTreeGridCellDirective.prototype.ngOnInit = function () {
        var _this = this;
        if (isPlatformBrowser(this.platformId)) {
            var style = this.window.getComputedStyle(this.elementRef.nativeElement);
            this.initialLeftPadding = style.paddingLeft;
            this.initialRightPadding = style.paddingRight;
        }
        this.columnService.onColumnsChange()
            .pipe(takeWhile(function () { return _this.alive; }), filter(function () { return _this.latestWidth !== _this.tree.getColumnWidth(); }))
            .subscribe(function () { return _this.cd.detectChanges(); });
    };
    ElTreeGridCellDirective.prototype.ngOnDestroy = function () {
        this.alive = false;
    };
    ElTreeGridCellDirective.prototype.toggleRow = function () {
        this.tree.toggleCellRow(this);
    };
    Object.defineProperty(ElTreeGridCellDirective.prototype, "initialStartPadding", {
        get: function () {
            return this.directionService.isLtr()
                ? this.initialLeftPadding
                : this.initialRightPadding;
        },
        enumerable: true,
        configurable: true
    });
    ElTreeGridCellDirective.prototype.getStartPadding = function () {
        var rowLevel = this.tree.getCellLevel(this, this.columnDef.name);
        if (rowLevel === EL_DEFAULT_ROW_LEVEL) {
            return null;
        }
        var nestingLevel = rowLevel + 1;
        var padding = '';
        if (this.tree.levelPadding) {
            padding = "calc(" + this.tree.levelPadding + " * " + nestingLevel + ")";
        }
        else if (this.initialStartPadding) {
            padding = "calc(" + this.initialStartPadding + " * " + nestingLevel + ")";
        }
        if (!padding) {
            return null;
        }
        return this.sanitizer.bypassSecurityTrustStyle(padding);
    };
    var ElTreeGridCellDirective_1;
    __decorate([
        HostBinding('style.width'),
        __metadata("design:type", String),
        __metadata("design:paramtypes", [])
    ], ElTreeGridCellDirective.prototype, "columnWidth", null);
    __decorate([
        HostBinding('style.padding-left'),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], ElTreeGridCellDirective.prototype, "leftPadding", null);
    __decorate([
        HostBinding('style.padding-right'),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], ElTreeGridCellDirective.prototype, "rightPadding", null);
    ElTreeGridCellDirective = ElTreeGridCellDirective_1 = __decorate([
        Directive({
            selector: 'td[elTreeGridCell]',
            host: {
                'class': 'el-tree-grid-cell',
                'role': 'gridcell',
            },
            providers: [{ provide: ElCdkCell, useExisting: ElTreeGridCellDirective_1 }],
        }),
        __param(2, Inject(EL_TREE_GRID)),
        __param(3, Inject(PLATFORM_ID)),
        __param(4, Inject(EL_WINDOW)),
        __metadata("design:paramtypes", [ElTreeGridColumnDefDirective,
            ElementRef, Object, Object, Object, DomSanitizer,
            ElLayoutDirectionService,
            ElColumnsService,
            ChangeDetectorRef])
    ], ElTreeGridCellDirective);
    return ElTreeGridCellDirective;
}(ElCellDirective));
export { ElTreeGridCellDirective };
var ElTreeGridHeaderCellDirective = /** @class */ (function (_super) {
    __extends(ElTreeGridHeaderCellDirective, _super);
    function ElTreeGridHeaderCellDirective(columnDef, elementRef, tree, columnService, cd) {
        var _this = _super.call(this, columnDef, elementRef) || this;
        _this.columnService = columnService;
        _this.cd = cd;
        _this.alive = true;
        _this.tree = tree;
        return _this;
    }
    ElTreeGridHeaderCellDirective_1 = ElTreeGridHeaderCellDirective;
    Object.defineProperty(ElTreeGridHeaderCellDirective.prototype, "columnWidth", {
        get: function () {
            this.latestWidth = this.tree.getColumnWidth();
            return this.latestWidth || null;
        },
        enumerable: true,
        configurable: true
    });
    ElTreeGridHeaderCellDirective.prototype.ngOnInit = function () {
        var _this = this;
        this.columnService.onColumnsChange()
            .pipe(takeWhile(function () { return _this.alive; }), filter(function () { return _this.latestWidth !== _this.tree.getColumnWidth(); }))
            .subscribe(function () { return _this.cd.detectChanges(); });
    };
    ElTreeGridHeaderCellDirective.prototype.ngOnDestroy = function () {
        this.alive = false;
    };
    var ElTreeGridHeaderCellDirective_1;
    __decorate([
        HostBinding('style.width'),
        __metadata("design:type", String),
        __metadata("design:paramtypes", [])
    ], ElTreeGridHeaderCellDirective.prototype, "columnWidth", null);
    ElTreeGridHeaderCellDirective = ElTreeGridHeaderCellDirective_1 = __decorate([
        Directive({
            selector: 'th[elTreeGridHeaderCell]',
            host: {
                'class': 'el-tree-grid-header-cell',
                'role': 'columnheader',
            },
            providers: [{ provide: ElCdkHeaderCell, useExisting: ElTreeGridHeaderCellDirective_1 }],
        }),
        __param(2, Inject(EL_TREE_GRID)),
        __metadata("design:paramtypes", [ElTreeGridColumnDefDirective,
            ElementRef, Object, ElColumnsService,
            ChangeDetectorRef])
    ], ElTreeGridHeaderCellDirective);
    return ElTreeGridHeaderCellDirective;
}(ElHeaderCellDirective));
export { ElTreeGridHeaderCellDirective };
var ElTreeGridFooterCellDirective = /** @class */ (function (_super) {
    __extends(ElTreeGridFooterCellDirective, _super);
    function ElTreeGridFooterCellDirective(columnDef, elementRef, tree, columnService, cd) {
        var _this = _super.call(this, columnDef, elementRef) || this;
        _this.columnService = columnService;
        _this.cd = cd;
        _this.alive = true;
        _this.tree = tree;
        return _this;
    }
    ElTreeGridFooterCellDirective_1 = ElTreeGridFooterCellDirective;
    Object.defineProperty(ElTreeGridFooterCellDirective.prototype, "columnWidth", {
        get: function () {
            this.latestWidth = this.tree.getColumnWidth();
            return this.latestWidth || null;
        },
        enumerable: true,
        configurable: true
    });
    ElTreeGridFooterCellDirective.prototype.ngOnInit = function () {
        var _this = this;
        this.columnService.onColumnsChange()
            .pipe(takeWhile(function () { return _this.alive; }), filter(function () { return _this.latestWidth !== _this.tree.getColumnWidth(); }))
            .subscribe(function () { return _this.cd.detectChanges(); });
    };
    ElTreeGridFooterCellDirective.prototype.ngOnDestroy = function () {
        this.alive = false;
    };
    var ElTreeGridFooterCellDirective_1;
    __decorate([
        HostBinding('style.width'),
        __metadata("design:type", String),
        __metadata("design:paramtypes", [])
    ], ElTreeGridFooterCellDirective.prototype, "columnWidth", null);
    ElTreeGridFooterCellDirective = ElTreeGridFooterCellDirective_1 = __decorate([
        Directive({
            selector: 'td[elTreeGridFooterCell]',
            host: {
                'class': 'el-tree-grid-footer-cell',
                'role': 'gridcell',
            },
            providers: [{ provide: ElCdkFooterCell, useExisting: ElTreeGridFooterCellDirective_1 }],
        }),
        __param(2, Inject(EL_TREE_GRID)),
        __metadata("design:paramtypes", [ElTreeGridColumnDefDirective,
            ElementRef, Object, ElColumnsService,
            ChangeDetectorRef])
    ], ElTreeGridFooterCellDirective);
    return ElTreeGridFooterCellDirective;
}(ElFooterCellDirective));
export { ElTreeGridFooterCellDirective };
//# sourceMappingURL=tree-grid-cell.component.js.map