import { h } from "@stencil/core";
/**
 * Rate 评分组件
 * 评分组件
 */
export class Rate {
    /**
     * 绑定值
     */
    modelValue = 0;
    /**
     * 最大分值
     * @default 5
     */
    max = 5;
    /**
     * 是否禁用
     * @default false
     */
    disabled = false;
    /**
     * 是否允许半选
     * @default false
     */
    allowHalf = false;
    /**
     * 只读
     * @default false
     */
    readonly = false;
    /**
     * 是否显示分数
     * @default false
     */
    showScore = false;
    /**
     * 是否显示文本
     * @default false
     */
    showText = false;
    /**
     * 辅助文字数组
     */
    texts;
    /**
     * 分数显示模板
     */
    scoreTemplate;
    hoverValue = 0;
    currentValue = 0;
    /**
     * 值改变事件
     */
    hbChange;
    componentDidLoad() {
        this.currentValue = this.modelValue;
    }
    handleValueChange() {
        this.currentValue = this.modelValue;
    }
    handleClick = (value, isHalf) => {
        if (this.disabled || this.readonly)
            return;
        const newValue = isHalf && this.allowHalf ? value - 0.5 : value;
        this.currentValue = newValue;
        this.modelValue = newValue;
        this.hoverValue = 0;
        this.hbChange.emit(newValue);
    };
    handleMouseEnter = (value) => {
        if (this.disabled || this.readonly)
            return;
        this.hoverValue = value;
    };
    handleMouseLeave = () => {
        if (this.disabled || this.readonly)
            return;
        this.hoverValue = 0;
    };
    getDisplayValue() {
        return this.hoverValue || this.currentValue;
    }
    getText() {
        const value = Math.ceil(this.currentValue);
        if (this.texts && this.texts[value - 1]) {
            return this.texts[value - 1];
        }
        return '';
    }
    getScoreText() {
        if (this.scoreTemplate) {
            return this.scoreTemplate.replace('{value}', String(this.currentValue));
        }
        return `${this.currentValue} / ${this.max}`;
    }
    render() {
        const displayValue = this.getDisplayValue();
        const stars = Array.from({ length: this.max }, (_, i) => i + 1);
        return (h("div", { key: 'cf15cfe1af8d6f65b2a08a398214dd1f762fc7e7', class: "hb-rate" }, stars.map((value) => {
            const isFull = displayValue >= value;
            const isHalf = this.allowHalf && displayValue >= value - 0.5 && displayValue < value;
            const isActive = isFull || isHalf;
            return (h("span", { class: {
                    'hb-rate__item': true,
                    'hb-rate__item--active': isActive,
                    'hb-rate__item--half': isHalf,
                }, onMouseEnter: () => this.handleMouseEnter(value), onMouseLeave: this.handleMouseLeave, onClick: () => this.handleClick(value, false) }, this.allowHalf && (h("span", { class: {
                    'hb-rate__half': true,
                    'hb-rate__half--active': isHalf,
                }, onClick: (e) => {
                    e.stopPropagation();
                    this.handleClick(value, true);
                } }, "\u2605")), h("span", { class: "hb-rate__icon" }, "\u2605")));
        }), this.showScore && (h("span", { key: '2f3d24aff4a4be719971e6162ae7bc21983cf62f', class: "hb-rate__text" }, this.getScoreText())), this.showText && this.getText() && (h("span", { key: '83107780b12701836a8badbc3139b791b2d9f07a', class: "hb-rate__text" }, this.getText()))));
    }
    static get is() { return "hb-rate"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["rate.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["rate.css"]
        };
    }
    static get properties() {
        return {
            "modelValue": {
                "type": "number",
                "mutable": true,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "\u7ED1\u5B9A\u503C"
                },
                "getter": false,
                "setter": false,
                "attribute": "model-value",
                "reflect": false,
                "defaultValue": "0"
            },
            "max": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "5"
                        }],
                    "text": "\u6700\u5927\u5206\u503C"
                },
                "getter": false,
                "setter": false,
                "attribute": "max",
                "reflect": false,
                "defaultValue": "5"
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "false"
                        }],
                    "text": "\u662F\u5426\u7981\u7528"
                },
                "getter": false,
                "setter": false,
                "attribute": "disabled",
                "reflect": false,
                "defaultValue": "false"
            },
            "allowHalf": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "false"
                        }],
                    "text": "\u662F\u5426\u5141\u8BB8\u534A\u9009"
                },
                "getter": false,
                "setter": false,
                "attribute": "allow-half",
                "reflect": false,
                "defaultValue": "false"
            },
            "readonly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "false"
                        }],
                    "text": "\u53EA\u8BFB"
                },
                "getter": false,
                "setter": false,
                "attribute": "readonly",
                "reflect": false,
                "defaultValue": "false"
            },
            "showScore": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "false"
                        }],
                    "text": "\u662F\u5426\u663E\u793A\u5206\u6570"
                },
                "getter": false,
                "setter": false,
                "attribute": "show-score",
                "reflect": false,
                "defaultValue": "false"
            },
            "showText": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "false"
                        }],
                    "text": "\u662F\u5426\u663E\u793A\u6587\u672C"
                },
                "getter": false,
                "setter": false,
                "attribute": "show-text",
                "reflect": false,
                "defaultValue": "false"
            },
            "texts": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "string[]",
                    "resolved": "string[]",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "\u8F85\u52A9\u6587\u5B57\u6570\u7EC4"
                },
                "getter": false,
                "setter": false
            },
            "scoreTemplate": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "\u5206\u6570\u663E\u793A\u6A21\u677F"
                },
                "getter": false,
                "setter": false,
                "attribute": "score-template",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "hoverValue": {},
            "currentValue": {}
        };
    }
    static get events() {
        return [{
                "method": "hbChange",
                "name": "hbChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "\u503C\u6539\u53D8\u4E8B\u4EF6"
                },
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                }
            }];
    }
    static get watchers() {
        return [{
                "propName": "modelValue",
                "methodName": "handleValueChange"
            }];
    }
}
//# sourceMappingURL=Rate.js.map
