"use strict";var _baseComponent=_interopRequireDefault(require("../helpers/baseComponent")),_classNames2=_interopRequireDefault(require("../helpers/libs/classNames")),_eventsMixin=_interopRequireDefault(require("../helpers/mixins/eventsMixin")),_useDOM=require("../helpers/hooks/useDOM");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_unsupportedIterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,t){var r;if(e)return"string"==typeof e?_arrayLikeToArray(e,t):"Map"===(r="Object"===(r=Object.prototype.toString.call(e).slice(8,-1))&&e.constructor?e.constructor.name:r)||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}function _iterableToArray(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e))return _arrayLikeToArray(e)}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,a=new Array(t);r<t;r++)a[r]=e[r];return a}function _defineProperty(e,t,r){return(t=_toPropertyKey(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _toPropertyKey(e){e=_toPrimitive(e,"string");return"symbol"==_typeof(e)?e:String(e)}function _toPrimitive(e,t){if("object"!=_typeof(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0===r)return("string"===t?String:Number)(e);r=r.call(e,t||"default");if("object"!=_typeof(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}(0,_baseComponent.default)({behaviors:[(0,_eventsMixin.default)()],relations:{"../field/index":{type:"ancestor"}},properties:{prefixCls:{type:String,value:"wux-rater"},max:{type:Number,value:5,observer:function(){this.setValue(this.data.inputValue)}},icon:{type:String,value:""},star:{type:String,value:"★"},defaultValue:{type:Number,value:0},value:{type:Number,value:0,observer:function(e){this.data.controlled&&this.setValue(e)}},activeColor:{type:String,value:"#ffc900"},margin:{type:Number,value:2},fontSize:{type:Number,value:25},disabled:{type:Boolean,value:!1},allowHalf:{type:Boolean,value:!1},allowClear:{type:Boolean,value:!1},allowTouchMove:{type:Boolean,value:!1},controlled:{type:Boolean,value:!1}},data:{inputValue:-1},computed:{classes:["prefixCls, disabled",function(e,t){return{wrap:(0,_classNames2.default)(e,_defineProperty({},"".concat(e,"--disabled"),t)),star:"".concat(e,"__star"),box:"".concat(e,"__box"),inner:"".concat(e,"__inner"),outer:"".concat(e,"__outer"),icon:"".concat(e,"__icon")}}]},observers:_defineProperty({},"inputValue, max, activeColor",function(a,e,n){var e=_toConsumableArray(new Array(e)).map(function(e,t){return t}),t=e.reduce(function(e,t,r){return[].concat(_toConsumableArray(e),[r<=a-1?n:"#ccc"])},[]),r=a.toString().split("."),r=1===r.length?[r[0],0]:r;this.setData({stars:e,colors:t,cutIndex:+r[0],cutPercent:10*r[1]})}),methods:{updated:function(e){this.hasFieldDecorator||this.data.inputValue!==e&&this.setData({inputValue:e})},setValue:function(e){var t=this.data.max;this.updated(e<=0?0:t<e?t:e)},updateHalfStarValue:function(r,a,n){var o=this,e=this.data.prefixCls;(0,_useDOM.useRectAll)(".".concat(e,"__star"),this).then(function(e){var t;e.filter(function(e){return!e}).length||(t=(e=e[r]).left,e=e.width,n.call(o,a-t<e/2?r+.5:r+1,r))})},onTap:function(e){var r=this,t=e.currentTarget.dataset.index,a=this.data,n=a.inputValue,o=a.disabled,i=a.allowHalf,u=a.allowClear;o||(i?this.updateHalfStarValue(t,e.detail.x,function(e,t){r.onChange(u&&e===n?0:e,t)}):(a=t+1,this.onChange(u&&a===n?0:a,t)))},onChange:function(e,t){this.data.controlled||this.setValue(e),this.triggerEvent("change",{value:e,index:t})},onTouchMove:function(e){var n,o=this,t=this.data,r=t.disabled,i=t.allowHalf,t=t.allowTouchMove;!r&&t&&(n=e.changedTouches[0].pageX,r=this.data.prefixCls,(0,_useDOM.useRectAll)(".".concat(r,"__star"),this).then(function(e){var t,r,a;e.filter(function(e){return!e}).length||(t=(a=e[0]).left,a=a.width,r=e.map(function(e){return e.width}).reduce(function(e,t){return e+t}),t=n-t,a=Math.ceil(t/a),0<t&&t<r&&(t=a-1,i&&(r=e[t],a=n-r.left<r.width/2?a-.5:a),o.onChange(a,t)))}))}},attached:function(){var e=this.data,t=e.defaultValue,r=e.value,e=e.controlled;this.setValue(e?r:t)}});