import { __assign, __awaiter, __generator } from "tslib";
import { Component, triggerEvent, getValueFromProps } from '../_util/simply';
import { RateDefaultProps } from './props';
import createValue from '../mixins/value';
import { getInstanceBoundingClientRect } from '../_util/jsapi/get-instance-bounding-client-rect';
Component(RateDefaultProps, {
    getInstance: function () {
        if (this.$id) {
            return my;
        }
        return this;
    },
    getRate: function (clientX) {
        return __awaiter(this, void 0, void 0, function () {
            var _a, gutter, count, allowHalf, _b, left, width, halfRateWidth, num, halfRateCount, val, rate;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        _a = getValueFromProps(this, [
                            'gutter',
                            'count',
                            'allowHalf',
                        ]), gutter = _a[0], count = _a[1], allowHalf = _a[2];
                        return [4 /*yield*/, getInstanceBoundingClientRect(this.getInstance(), "#ant-rate-container".concat(this.$id ? "-".concat(this.$id) : ''))];
                    case 1:
                        _b = _c.sent(), left = _b.left, width = _b.width;
                        halfRateWidth = (width - (count - 1) * gutter) / count / 2;
                        num = clientX - left;
                        halfRateCount = 0;
                        /* eslint-disable no-constant-condition */
                        while (true) {
                            val = halfRateWidth * halfRateCount +
                                gutter * Math.floor(halfRateCount / 2);
                            if (halfRateCount >= count * 2 || num <= val) {
                                break;
                            }
                            halfRateCount++;
                        }
                        rate = allowHalf
                            ? halfRateCount * 0.5
                            : Math.ceil(halfRateCount * 0.5);
                        return [2 /*return*/, rate];
                }
            });
        });
    },
    handleStarTap: function (e) {
        return __awaiter(this, void 0, void 0, function () {
            var _a, readonly, allowClear, _b, clientX, x, clickX, rateValue, rate;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        _a = getValueFromProps(this, [
                            'readonly',
                            'allowClear',
                        ]), readonly = _a[0], allowClear = _a[1];
                        if (readonly) {
                            return [2 /*return*/];
                        }
                        _b = e.detail, clientX = _b.clientX, x = _b.x;
                        clickX = typeof x === 'number' ? x : clientX;
                        rateValue = this.getValue();
                        return [4 /*yield*/, this.getRate(clickX)];
                    case 1:
                        rate = _c.sent();
                        if (rateValue === rate && allowClear) {
                            rate = 0;
                        }
                        if (!this.isControlled()) {
                            this.update(rate);
                        }
                        if (rateValue !== rate) {
                            triggerEvent(this, 'change', rate);
                        }
                        return [2 /*return*/];
                }
            });
        });
    },
    handleStarMove: function (e) {
        return __awaiter(this, void 0, void 0, function () {
            var readonly, touches, clientX, rate;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        readonly = getValueFromProps(this, ['readonly'])[0];
                        if (readonly) {
                            return [2 /*return*/];
                        }
                        touches = e.touches;
                        clientX = touches[0].clientX;
                        if (!this.moveRate) {
                            this.moveRate = {
                                originalRate: this.getValue(),
                            };
                        }
                        return [4 /*yield*/, this.getRate(clientX)];
                    case 1:
                        rate = _a.sent();
                        if (this.moveRate) {
                            this.moveRate = __assign(__assign({}, this.moveRate), { currentRate: rate });
                            if (this.isControlled()) {
                                this.setData({ displayValue: rate });
                            }
                            else {
                                this.update(rate);
                            }
                        }
                        return [2 /*return*/];
                }
            });
        });
    },
    handleStarMoveEnd: function () {
        var readonly = getValueFromProps(this, 'readonly');
        if (readonly) {
            return;
        }
        if (!this.moveRate) {
            return;
        }
        var _a = this.moveRate, currentRate = _a.currentRate, originalRate = _a.originalRate;
        this.moveRate = null;
        if (this.isControlled()) {
            this.setData({ displayValue: null });
        }
        if (currentRate !== originalRate) {
            triggerEvent(this, 'change', currentRate);
        }
    },
}, { displayValue: null }, [
    createValue({
        transformValue: function (value) {
            var allowHalf = getValueFromProps(this, 'allowHalf');
            if (allowHalf) {
                return {
                    needUpdate: true,
                    value: value % 0.5 !== 0 ? Math.round(value) : value,
                };
            }
            return {
                needUpdate: true,
                value: Math.ceil(value),
            };
        },
    }),
]);