/*
 * @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, ChangeDetectorRef, Component, ElementRef, EventEmitter, HostBinding, HostListener, Inject, Input, Output, } from '@angular/core';
import { Subject } from 'rxjs';
import { convertToBoolProperty } from '../helpers';
import { EL_SELECT_INJECTION_TOKEN } from './select-injection-tokens';
var ElOptionComponent = /** @class */ (function () {
    function ElOptionComponent(parent, elementRef, cd) {
        this.elementRef = elementRef;
        this.cd = cd;
        this.disabledByGroup = false;
        this._disabled = false;
        /**
         * Fires value when option selection change.
         * */
        this.selectionChange = new EventEmitter();
        /**
         * Fires when option clicked
         */
        this.click$ = new Subject();
        this.selected = false;
        this.alive = true;
        this.parent = parent;
    }
    Object.defineProperty(ElOptionComponent.prototype, "disabled", {
        get: function () {
            return this._disabled || this.disabledByGroup;
        },
        set: function (value) {
            this._disabled = convertToBoolProperty(value);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ElOptionComponent.prototype, "click", {
        get: function () {
            return this.click$.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    ElOptionComponent.prototype.ngOnDestroy = function () {
        this.alive = false;
    };
    Object.defineProperty(ElOptionComponent.prototype, "withCheckbox", {
        /**
         * Determines should we render checkbox.
         * */
        get: function () {
            return this.multiple && this.value != null;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ElOptionComponent.prototype, "content", {
        get: function () {
            return this.elementRef.nativeElement.textContent;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ElOptionComponent.prototype, "multiple", {
        get: function () {
            return this.parent.multiple;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ElOptionComponent.prototype, "selectedClass", {
        get: function () {
            return this.selected;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ElOptionComponent.prototype, "disabledAttribute", {
        get: function () {
            return this.disabled ? '' : null;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ElOptionComponent.prototype, "tabindex", {
        get: function () {
            return '-1';
        },
        enumerable: true,
        configurable: true
    });
    ElOptionComponent.prototype.onClick = function (event) {
        this.click$.next(this);
        // Prevent scroll on space click, etc.
        event.preventDefault();
    };
    ElOptionComponent.prototype.select = function () {
        this.setSelection(true);
    };
    ElOptionComponent.prototype.deselect = function () {
        this.setSelection(false);
    };
    /**
     * Sets disabled by group state and marks component for check.
     */
    ElOptionComponent.prototype.setDisabledByGroupState = function (disabled) {
        if (this.disabledByGroup !== disabled) {
            this.disabledByGroup = disabled;
            this.cd.markForCheck();
        }
    };
    ElOptionComponent.prototype.setSelection = function (selected) {
        /**
         * In case of changing options in runtime the reference to the selected option will be kept in select component.
         * This may lead to exceptions with detecting changes in destroyed component.
         *
         * Also Angular can call writeValue on destroyed view (select implements ControlValueAccessor).
         * angular/angular#27803
         * */
        if (this.alive && this.selected !== selected) {
            this.selected = selected;
            this.selectionChange.emit(this);
            this.cd.markForCheck();
        }
    };
    ElOptionComponent.prototype.focus = function () {
        this.elementRef.nativeElement.focus();
    };
    ElOptionComponent.prototype.getLabel = function () {
        return this.content;
    };
    __decorate([
        Input(),
        __metadata("design:type", Object)
    ], ElOptionComponent.prototype, "value", void 0);
    __decorate([
        Input(),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [Boolean])
    ], ElOptionComponent.prototype, "disabled", null);
    __decorate([
        Output(),
        __metadata("design:type", EventEmitter)
    ], ElOptionComponent.prototype, "selectionChange", void 0);
    __decorate([
        HostBinding('class.selected'),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [])
    ], ElOptionComponent.prototype, "selectedClass", null);
    __decorate([
        HostBinding('attr.disabled'),
        __metadata("design:type", String),
        __metadata("design:paramtypes", [])
    ], ElOptionComponent.prototype, "disabledAttribute", null);
    __decorate([
        HostBinding('tabIndex'),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], ElOptionComponent.prototype, "tabindex", null);
    __decorate([
        HostListener('click', ['$event']),
        HostListener('keydown.space', ['$event']),
        HostListener('keydown.enter', ['$event']),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Event]),
        __metadata("design:returntype", void 0)
    ], ElOptionComponent.prototype, "onClick", null);
    ElOptionComponent = __decorate([
        Component({
            selector: 'el-option',
            changeDetection: ChangeDetectionStrategy.OnPush,
            template: "\n    <el-checkbox *ngIf=\"withCheckbox\"\n                 [checked]=\"selected\"\n                 [disabled]=\"disabled\"\n                 aria-hidden=\"true\">\n    </el-checkbox>\n    <ng-content></ng-content>\n  ",
            styles: ["/*!\n * @license\n * Copyright Endlessjs. All Rights Reserved.\n * Licensed under the MIT License. See License.txt in the project root for license information.\n */:host{display:flex;transition-duration:0.15s;transition-property:background-color,color;transition-timing-function:ease-in}:host:hover{cursor:pointer}:host el-checkbox{display:flex;pointer-events:none}:host el-checkbox ::ng-deep .label{padding:0}:host([disabled]){pointer-events:none}\n"]
        }),
        __param(0, Inject(EL_SELECT_INJECTION_TOKEN)),
        __metadata("design:paramtypes", [Object, ElementRef,
            ChangeDetectorRef])
    ], ElOptionComponent);
    return ElOptionComponent;
}());
export { ElOptionComponent };
//# sourceMappingURL=option.component.js.map