/*
 * @license
 * Copyright Endlessjs. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */
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 { ChangeDetectionStrategy, Component, ContentChild, Directive, EventEmitter, HostBinding, HostListener, Inject, Input, Output, TemplateRef, } from '@angular/core';
import { convertToBoolProperty } from '../helpers';
import { EL_SORT_HEADER_COLUMN_DEF } from '../cdk/table/cell';
export var ElSortDirection;
(function (ElSortDirection) {
    ElSortDirection["ASCENDING"] = "asc";
    ElSortDirection["DESCENDING"] = "desc";
    ElSortDirection["NONE"] = "";
})(ElSortDirection || (ElSortDirection = {}));
const sortDirections = [
    ElSortDirection.ASCENDING,
    ElSortDirection.DESCENDING,
    ElSortDirection.NONE,
];
/**
 * Directive triggers sort method of passed object when sort header changes direction
 */
let ElSortDirective = class ElSortDirective {
    /**
     * Directive triggers sort method of passed object when sort header changes direction
     */
    constructor() {
        this.sort = new EventEmitter();
    }
    emitSort(sortRequest) {
        if (this.sortable && this.sortable.sort) {
            this.sortable.sort(sortRequest);
        }
        this.sort.emit(sortRequest);
    }
};
__decorate([
    Input('elSort'),
    __metadata("design:type", Object)
], ElSortDirective.prototype, "sortable", void 0);
__decorate([
    Output(),
    __metadata("design:type", EventEmitter)
], ElSortDirective.prototype, "sort", void 0);
ElSortDirective = __decorate([
    Directive({ selector: '[elSort]' })
], ElSortDirective);
export { ElSortDirective };
/**
 * Directive for headers sort icons. Mark you icon implementation with this structural directive and
 * it'll set template's implicit context with current direction. Context also has `isAscending`,
 * `isDescending` and `isNone` properties.
 */
let ElSortHeaderIconDirective = class ElSortHeaderIconDirective {
};
ElSortHeaderIconDirective = __decorate([
    Directive({ selector: '[elSortHeaderIcon]' })
], ElSortHeaderIconDirective);
export { ElSortHeaderIconDirective };
let ElSortIconComponent = class ElSortIconComponent {
    constructor() {
        this.direction = ElSortDirection.NONE;
    }
    isAscending() {
        return this.direction === ElSortDirection.ASCENDING;
    }
    isDescending() {
        return this.direction === ElSortDirection.DESCENDING;
    }
    isDirectionSet() {
        return this.isAscending() || this.isDescending();
    }
};
__decorate([
    Input(),
    __metadata("design:type", String)
], ElSortIconComponent.prototype, "direction", void 0);
ElSortIconComponent = __decorate([
    Component({
        selector: 'el-sort-icon',
        template: `
    <ng-container *ngIf="isDirectionSet()">
      <el-icon *ngIf="isAscending()" icon="chevron-down-outline" pack="endlessjs-essentials" aria-hidden="true"></el-icon>
      <el-icon *ngIf="isDescending()" icon="chevron-up-outline" pack="endlessjs-essentials" aria-hidden="true"></el-icon>
    </ng-container>
  `
    })
], ElSortIconComponent);
export { ElSortIconComponent };
/**
 * Marks header as sort header so it emitting sort event when clicked.
 */
let ElSortHeaderComponent = class ElSortHeaderComponent {
    constructor(sort, columnDef) {
        this.sort = sort;
        this.columnDef = columnDef;
        this.disabledValue = false;
    }
    /**
     * Disable sort header
     */
    set disabled(value) {
        this.disabledValue = convertToBoolProperty(value);
    }
    get disabled() {
        return this.disabledValue;
    }
    sortIfEnabled() {
        if (!this.disabled) {
            this.sortData();
        }
    }
    isAscending() {
        return this.direction === ElSortDirection.ASCENDING;
    }
    isDescending() {
        return this.direction === ElSortDirection.DESCENDING;
    }
    sortData() {
        const sortRequest = this.createSortRequest();
        this.sort.emitSort(sortRequest);
    }
    getIconContext() {
        return {
            $implicit: this.direction,
            isAscending: this.isAscending(),
            isDescending: this.isDescending(),
            isNone: !this.isAscending() && !this.isDescending(),
        };
    }
    getDisabledAttributeValue() {
        return this.disabled ? '' : null;
    }
    createSortRequest() {
        this.direction = this.getNextDirection();
        return { direction: this.direction, column: this.columnDef.name };
    }
    getNextDirection() {
        const sortDirectionCycle = sortDirections;
        let nextDirectionIndex = sortDirectionCycle.indexOf(this.direction) + 1;
        if (nextDirectionIndex >= sortDirectionCycle.length) {
            nextDirectionIndex = 0;
        }
        return sortDirectionCycle[nextDirectionIndex];
    }
};
__decorate([
    ContentChild(ElSortHeaderIconDirective, { read: TemplateRef, static: false }),
    __metadata("design:type", TemplateRef)
], ElSortHeaderComponent.prototype, "sortIcon", void 0);
__decorate([
    Input('elSortHeader'),
    __metadata("design:type", String)
], ElSortHeaderComponent.prototype, "direction", void 0);
__decorate([
    Input(),
    HostBinding('class.disabled'),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Object])
], ElSortHeaderComponent.prototype, "disabled", null);
__decorate([
    HostListener('click'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ElSortHeaderComponent.prototype, "sortIfEnabled", null);
ElSortHeaderComponent = __decorate([
    Component({
        selector: '[elSortHeader]',
        template: `
    <button
      class="el-tree-grid-header-change-sort-button"
      type="button"
      [attr.disabled]="getDisabledAttributeValue()"
      (click)="sortData()">
      <ng-content></ng-content>
    </button>
    <el-sort-icon *ngIf="!sortIcon; else customIcon" [direction]="direction"></el-sort-icon>
    <ng-template #customIcon [ngTemplateOutlet]="sortIcon" [ngTemplateOutletContext]="getIconContext()"></ng-template>
  `,
        changeDetection: ChangeDetectionStrategy.OnPush
    }),
    __param(1, Inject(EL_SORT_HEADER_COLUMN_DEF)),
    __metadata("design:paramtypes", [ElSortDirective, Object])
], ElSortHeaderComponent);
export { ElSortHeaderComponent };
//# sourceMappingURL=tree-grid-sort.component.js.map