import * as tslib_1 from "tslib";
import { Component, Input, Output, EventEmitter, forwardRef, ChangeDetectorRef, ViewEncapsulation, ChangeDetectionStrategy, ElementRef } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { OnChange } from '../core/decorators';
import { addClass } from '../util/dom';
/*
 * Provider Expression that allows component to register as a ControlValueAccessor.
 * This allows it to support [(ngModel)].
 * @docs-private
 */
var RATING_VALUE_ACCESSOR = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(function () { return RatingComponent; }),
    multi: true
};
var RatingComponent = /** @class */ (function () {
    function RatingComponent(_cd, _el) {
        this._cd = _cd;
        this._el = _el;
        /**
         * event emitted when rating change
         */
        this.change = new EventEmitter();
        /**
         * total stars
         * @default 5
         */
        this.total = 5;
        /**
         * current star rating
         */
        this.value = 3;
        /**
         * whether to support half star, not yet supported
         * @default false
         */
        this.halfStar = false;
        /**
         * whether the rating is read-only, when in read-only mode, the stars loss interactivity
         * @default false
         */
        this.readonly = false;
        /**
         * whether show tip when hover the stars
         * @default false
         */
        this.showTip = false;
        /**
         * whether the rating is disabled
         * @default false
         */
        this.disabled = false;
        /**
         * custom theme
         */
        this.theme = '';
        /**
         * when `showTip` is set, you can use this to set tooltip message, you can use a string template
         *  or you can use you own custom tooltip function
         */
        this.tipTemplate = '{i} 星';
        this._starArray = [];
    }
    /**
     * get tooltip message
     *
     * @param {number} i - star number
     * @return {string}
     * @docs-private
     */
    RatingComponent.prototype.getTip = function (i) {
        if (this.readonly || this.disabled) {
            return null;
        }
        if (typeof this.tipTemplate === 'string') {
            return this.tipTemplate.replace('{i}', i + '');
        }
        else if (typeof this.tipTemplate === 'function') {
            return this.tipTemplate.call(this, i);
        }
    };
    /**
     * whether allow tooltip
     *
     * @return {boolean}
     * @docs-private
     */
    RatingComponent.prototype.allowShowTip = function () {
        if (this.disabled || this.readonly) {
            return false;
        }
        return this.showTip;
    };
    RatingComponent.prototype.ngOnInit = function () {
        this._starArray = this._genArray(this.total);
    };
    RatingComponent.prototype._genArray = function (limit) {
        var arr = [];
        var i = 1;
        while (i <= limit) {
            arr.push(i++);
        }
        return arr;
    };
    /**
     * star mouse over event
     *
     * @param {number} v - star number
     * @docs-private
     */
    RatingComponent.prototype.onMouseOver = function (v) {
        if (this.readonly || this.disabled) {
            return;
        }
        this._tempValue = v;
    };
    /**
     * mouse leave event
     * @docs-private
     */
    RatingComponent.prototype.onMouseLeave = function () {
        this._tempValue = null;
    };
    /**
     * rating event
     *
     * @param {number} v - current rating
     * @docs-private
     */
    RatingComponent.prototype.onClick = function (v) {
        if (this.readonly || this.disabled) {
            return;
        }
        this.value = v;
        this._tempValue = null;
        this.change.emit(this.value);
        this._updateFormModel();
    };
    /**
     * Registers a callback that will be triggered when the value has changed.
     * Implemented as part of ControlValueAccessor.
     * @param fn On change callback function.
     */
    RatingComponent.prototype.registerOnChange = function (fn) {
        this._onModelChange = fn;
    };
    /**
     * Registers a callback that will be triggered when the control has been touched.
     * Implemented as part of ControlValueAccessor.
     * @param fn On touch callback function.
     */
    RatingComponent.prototype.registerOnTouched = function (fn) {
        this._onTouch = fn;
    };
    /**
     * Sets the model value. Implemented as part of ControlValueAccessor.
     * @param value Value to be set to the model.
     */
    RatingComponent.prototype.writeValue = function (value) {
        if (value) {
            this.value = Math.ceil(Number(value));
            this._cd.markForCheck();
        }
    };
    /**
     * Toggles the disabled state of the component. Implemented as part of ControlValueAccessor.
     * @param isDisabled Whether the component should be disabled.
     */
    RatingComponent.prototype.setDisabledState = function (isDisabled) {
        this.disabled = isDisabled;
    };
    /**
     * update form model value and mark for check
     */
    RatingComponent.prototype._updateFormModel = function () {
        if (this._onModelChange) {
            this._onModelChange(this.value);
        }
        if (this._onTouch) {
            this._onTouch(this.value);
        }
    };
    RatingComponent.prototype.ngAfterViewInit = function () {
        if (this.theme) {
            addClass(this._el.nativeElement, "nb-rating-" + this.theme);
        }
    };
    tslib_1.__decorate([
        Output(),
        tslib_1.__metadata("design:type", EventEmitter)
    ], RatingComponent.prototype, "change", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Number)
    ], RatingComponent.prototype, "total", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Number)
    ], RatingComponent.prototype, "value", void 0);
    tslib_1.__decorate([
        OnChange(true),
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], RatingComponent.prototype, "halfStar", void 0);
    tslib_1.__decorate([
        OnChange(true),
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], RatingComponent.prototype, "readonly", void 0);
    tslib_1.__decorate([
        OnChange(true),
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], RatingComponent.prototype, "showTip", void 0);
    tslib_1.__decorate([
        OnChange(true),
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], RatingComponent.prototype, "disabled", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String)
    ], RatingComponent.prototype, "theme", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Object)
    ], RatingComponent.prototype, "tipTemplate", void 0);
    RatingComponent = tslib_1.__decorate([
        Component({
            selector: 'nb-rating',
            template: "<div class=\"nb-rating-container\" (mouseleave)=\"onMouseLeave()\"><ng-container *ngIf=\"!allowShowTip()\"><div class=\"nb-rating-item\" *ngFor=\"let i of _starArray\" (mouseover)=\"onMouseOver(i)\" (click)=\"onClick(i)\"><ng-container *ngIf=\"i <= (_tempValue || value) else noselected\"><i class=\"iconfont icon-collected\"></i></ng-container><ng-template #noselected><i class=\"iconfont icon-collect\"></i></ng-template></div></ng-container><ng-container *ngIf=\"allowShowTip()\"><div class=\"nb-rating-item\" *ngFor=\"let i of _starArray\" (mouseover)=\"onMouseOver(i)\" (click)=\"onClick(i)\" [nbTooltip]=\"getTip(i)\" placement=\"top\" hasArrow><ng-container *ngIf=\"i <= (_tempValue || value) else noselected\"><i class=\"iconfont icon-collected\"></i></ng-container><ng-template #noselected><i class=\"iconfont icon-collect\"></i></ng-template></div></ng-container></div>",
            encapsulation: ViewEncapsulation.None,
            changeDetection: ChangeDetectionStrategy.OnPush,
            preserveWhitespaces: false,
            providers: [RATING_VALUE_ACCESSOR],
            host: {
                'class': 'nb-widget nb-rating',
                '[class.nb-rating-readonly]': 'readonly',
                '[class.nb-rating-disabled]': 'disabled'
            },
            exportAs: 'nbRating'
        }),
        tslib_1.__metadata("design:paramtypes", [ChangeDetectorRef,
            ElementRef])
    ], RatingComponent);
    return RatingComponent;
}());
export { RatingComponent };
//# sourceMappingURL=rating.js.map