import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import classNames from "classnames";
import { Text, View } from "@tarojs/components";
import { useReady, nextTick } from "@tarojs/taro";
import { pxTransform } from "../../utils/taro/px-transform";
import { useTouch } from "../../hooks/use-touch";
import { ComponentDefaults } from "../../utils/typings";
import { usePropsValue } from "../../hooks/use-props-value";
import { getRectInMultiPlatform } from "../../utils/taro/get-rect";
import { useRtl } from "../configprovider/index";
import { harmony } from "../../utils/taro/platform";
import { mergeProps } from "../../utils/merge-props";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    range: false,
    min: 0,
    max: 100,
    step: 1,
    vertical: false,
    marks: {}
});
var isHm = harmony();
var classPrefix = 'nut-range';
var verticalClassPrefix = "".concat(classPrefix, "-vertical");
var isSameValue = function(newValue, oldValue) {
    return JSON.stringify(newValue) === JSON.stringify(oldValue);
};
var handleOverlap = function(value) {
    return value[0] > value[1] ? value.slice(0).reverse() : value;
};
export var Range = function(props) {
    var rtl = useRtl();
    var _mergeProps = mergeProps(defaultProps, props), className = _mergeProps.className, style = _mergeProps.style, range = _mergeProps.range, disabled = _mergeProps.disabled, button = _mergeProps.button, vertical = _mergeProps.vertical, marks = _mergeProps.marks, minDescription = _mergeProps.minDescription, maxDescription = _mergeProps.maxDescription, currentDescription = _mergeProps.currentDescription, min = _mergeProps.min, max = _mergeProps.max, step = _mergeProps.step, value = _mergeProps.value, defaultValue = _mergeProps.defaultValue, onChange = _mergeProps.onChange, onStart = _mergeProps.onStart, onEnd = _mergeProps.onEnd;
    var rtlClassPrefix = useMemo(function() {
        return "rtl-".concat(vertical ? verticalClassPrefix : classPrefix);
    }, [
        vertical
    ]);
    var buttonRef = useRef(0);
    var dragStatusRef = useRef('start');
    var touch = useTouch();
    var root = useRef(null);
    var rootRect = useRef(null);
    var _useState = _sliced_to_array(useState([]), 2), marksList = _useState[0], setMarksList = _useState[1];
    var _useState1 = _sliced_to_array(useState(0), 2), startValue = _useState1[0], setStartValue = _useState1[1];
    var scope = useMemo(function() {
        if (max < min || max === min) {
            console.log('max 的值需要大于 min的值');
        }
        return max - min;
    }, [
        max,
        min
    ]);
    var handleChange = function(value) {
        onChange && onChange(value);
    };
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: value,
        defaultValue: defaultValue,
        finalValue: 0,
        onChange: handleChange
    }), 2), innerValue = _usePropsValue[0], setInnerValue = _usePropsValue[1];
    var exactValueRef = useRef(value || defaultValue || 0);
    var marksRef = useRef({});
    useEffect(function() {
        if (marks) {
            if (Array.isArray(marks)) {
                var list = marks.sort(function(a, b) {
                    return a.value - b.value;
                }).filter(function(point) {
                    return point.value >= min && point.value <= max;
                });
                setMarksList(list.map(function(mark) {
                    return mark.value;
                }));
                list.forEach(function(mark) {
                    marksRef.current[mark.value] = mark.label !== undefined ? mark.label : mark.value;
                });
            } else {
                var marksKeys = Object.keys(marks);
                var list1 = marksKeys.map(parseFloat).sort(function(a, b) {
                    return a - b;
                }).filter(function(point) {
                    return point >= min && point <= max;
                });
                setMarksList(list1);
            }
        }
    }, [
        marks,
        max,
        min
    ]);
    var _obj;
    var classes = classNames(classPrefix, (_obj = {}, _define_property(_obj, "".concat(classPrefix, "-disabled"), disabled), _define_property(_obj, verticalClassPrefix, vertical), _define_property(_obj, "".concat(classPrefix, "-native"), isHm), _obj));
    var _obj1;
    var containerClasses = classNames("".concat(classPrefix, "-container"), (_obj1 = {}, _define_property(_obj1, "".concat(classPrefix, "-container-native"), isHm), _define_property(_obj1, "".concat(verticalClassPrefix, "-container"), vertical), _obj1), className);
    var markClassName = useCallback(function(mark) {
        var classPrefix = 'nut-range-mark';
        var verticalClassPrefix = 'nut-range-vertical-mark';
        var lowerBound = min;
        var upperBound = max;
        if (range && Array.isArray(innerValue)) {
            lowerBound = innerValue[0];
            upperBound = innerValue[1];
        } else {
            upperBound = innerValue;
        }
        var isActive = mark <= upperBound && mark >= lowerBound;
        var _$classNames = [
            "".concat(classPrefix, "-text-wrapper"),
            "".concat(isActive ? "".concat(classPrefix, "-text-wrapper-active") : '')
        ];
        if (vertical) {
            _$classNames.push("".concat(verticalClassPrefix, "-text-wrapper"));
            isActive && _$classNames.push("".concat(verticalClassPrefix, "-text-wrapper-active"));
        }
        if (rtl) {
            _$classNames.push("".concat(rtlClassPrefix, "-mark-text-wrapper"));
        }
        return _$classNames.join(' ');
    }, [
        min,
        max,
        range,
        innerValue,
        vertical,
        rtl,
        rtlClassPrefix
    ]);
    var isRange = useCallback(function(val) {
        return !!range && Array.isArray(val);
    }, [
        range
    ]);
    var calcMainAxis = useMemo(function() {
        var modelVal = innerValue;
        return isRange(modelVal) ? "".concat((modelVal[1] - modelVal[0]) * 100 / scope, "%") : "".concat((modelVal - min) * 100 / scope, "%");
    }, [
        innerValue,
        isRange,
        min,
        scope
    ]);
    var calcOffset = useMemo(function() {
        var modelVal = innerValue;
        return isRange(modelVal) ? "".concat((modelVal[0] - min) * 100 / scope, "%") : '0%';
    }, [
        innerValue,
        isRange,
        min,
        scope
    ]);
    var barStyle = useMemo(function() {
        if (vertical) {
            return {
                height: calcMainAxis,
                top: calcOffset,
                transition: dragStatusRef.current ? 'none' : undefined
            };
        }
        var dir = rtl ? 'right' : 'left';
        var _obj;
        return _obj = {
            width: calcMainAxis
        }, _define_property(_obj, dir, calcOffset), _define_property(_obj, "transition", dragStatusRef.current ? 'none' : undefined), _obj;
    }, [
        calcMainAxis,
        calcOffset,
        rtl,
        vertical
    ]);
    var marksStyle = useCallback(function(mark) {
        var dir = rtl ? 'right' : 'left';
        var style = _define_property({}, dir, "".concat((mark - min) / scope * 100, "%"));
        if (vertical) {
            style = {
                top: "".concat((mark - min) / scope * 100, "%")
            };
        }
        return style;
    }, [
        min,
        rtl,
        scope,
        vertical
    ]);
    var tickClass = useCallback(function(mark) {
        if (range && Array.isArray(innerValue)) {
            return mark <= innerValue[1] && mark >= innerValue[0];
        }
        return mark <= innerValue;
    }, [
        innerValue,
        range
    ]);
    var format = useCallback(function(value) {
        value = Math.max(+min, Math.min(value, +max));
        return Math.round(value / +step) * +step;
    }, [
        max,
        min,
        step
    ]);
    var updateValue = useCallback(function(value, end) {
        if (isRange(value)) {
            value = handleOverlap(value).map(format);
        } else {
            value = format(value);
        }
        if (!isSameValue(value, innerValue)) {
            setInnerValue(value);
        }
        end && onEnd && onEnd(value);
    }, [
        innerValue,
        format,
        isRange,
        onEnd,
        setInnerValue
    ]);
    var handleClick = useCallback(function(event) {
        return _async_to_generator(function() {
            var _event_detail, rect, _event_detail_x, x, delta, total, _event_detail1, _event_detail_y, y, value, _innerValue, left, right, middle;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        if (disabled || !root.current) return [
                            2
                        ];
                        // TODO 鸿蒙获取clientX的数据有误，windowX 也变成了 undefined。暂不支持，待上游支持。
                        if (isHm) return [
                            2
                        ];
                        dragStatusRef.current = '';
                        return [
                            4,
                            getRectInMultiPlatform(root.current)
                        ];
                    case 1:
                        rect = _state.sent();
                        x = (_event_detail_x = (_event_detail = event.detail) === null || _event_detail === void 0 ? void 0 : _event_detail.x) !== null && _event_detail_x !== void 0 ? _event_detail_x : event.clientX;
                        if (isHm) x = parseFloat(pxTransform(event.windowX || x));
                        delta = x - rect.left;
                        total = rect.width;
                        if (vertical) {
                            ;
                            ;
                            y = (_event_detail_y = (_event_detail1 = event.detail) === null || _event_detail1 === void 0 ? void 0 : _event_detail1.y) !== null && _event_detail_y !== void 0 ? _event_detail_y : event.clientY;
                            if (isHm) y = parseFloat(pxTransform(event.windowY || y));
                            delta = y - rect.top;
                            total = rect.height;
                        }
                        value = min + delta / total * scope;
                        exactValueRef.current = innerValue;
                        if (isRange(innerValue)) {
                            _innerValue = _sliced_to_array(innerValue, 2), left = _innerValue[0], right = _innerValue[1];
                            middle = (left + right) / 2;
                            if (value <= middle) {
                                updateValue([
                                    value,
                                    right
                                ], true);
                            } else {
                                updateValue([
                                    left,
                                    value
                                ], true);
                            }
                        } else {
                            updateValue(value, true);
                        }
                        return [
                            2
                        ];
                }
            });
        })();
    }, [
        innerValue,
        disabled,
        isRange,
        min,
        scope,
        updateValue,
        vertical
    ]);
    useReady(function() {
        var getRootRect = function() {
            return _async_to_generator(function() {
                var rect;
                return _ts_generator(this, function(_state) {
                    switch(_state.label){
                        case 0:
                            if (!root.current) return [
                                3,
                                2
                            ];
                            return [
                                4,
                                getRectInMultiPlatform(root.current)
                            ];
                        case 1:
                            rect = _state.sent();
                            rootRect.current = rect;
                            _state.label = 2;
                        case 2:
                            return [
                                2
                            ];
                    }
                });
            })();
        };
        nextTick(function() {
            getRootRect();
        });
    });
    var onTouchStart = useCallback(function(event) {
        if (disabled) return;
        touch.start(event);
        exactValueRef.current = innerValue;
        if (isRange(innerValue)) {
            setStartValue(innerValue.map(format));
        } else {
            setStartValue(format(innerValue));
        }
        dragStatusRef.current = 'start';
    }, [
        innerValue,
        disabled,
        format,
        isRange,
        touch
    ]);
    var onTouchMove = useCallback(function(event) {
        return _async_to_generator(function() {
            var handleMove;
            return _ts_generator(this, function(_state) {
                // @TODO RN、鸿蒙端垂直滑动时，页面会一同滑动，待解决
                if (disabled || !root.current) return [
                    2
                ];
                if (dragStatusRef.current === 'start') {
                    onStart && onStart();
                    dragStatusRef.current = 'draging';
                }
                touch.move(event);
                handleMove = function() {
                    return _async_to_generator(function() {
                        var delta, total, diff, newValue;
                        return _ts_generator(this, function(_state) {
                            if (!rootRect.current) return [
                                2
                            ];
                            delta = isHm ? parseFloat(pxTransform(touch.deltaX.current)) : touch.deltaX.current;
                            total = rootRect.current.width;
                            diff = delta / total * scope;
                            diff = rtl ? -diff : diff;
                            if (vertical) {
                                delta = isHm ? parseFloat(pxTransform(touch.deltaY.current)) : touch.deltaY.current;
                                total = rootRect.current.height;
                                diff = delta / total * scope;
                            }
                            newValue = startValue + diff;
                            if (isRange(startValue)) {
                                newValue = exactValueRef.current.slice();
                                newValue[buttonRef.current] = startValue[buttonRef.current] + diff;
                            }
                            exactValueRef.current = newValue;
                            updateValue(newValue);
                            return [
                                2
                            ];
                        });
                    })();
                };
                requestAnimationFrame(handleMove);
                return [
                    2
                ];
            });
        })();
    }, [
        disabled,
        isRange,
        onStart,
        rtl,
        scope,
        startValue,
        touch,
        updateValue,
        vertical
    ]);
    var onTouchEnd = useCallback(function() {
        if (disabled) return;
        if (dragStatusRef.current === 'draging') {
            updateValue(innerValue, true);
        }
        dragStatusRef.current = '';
    }, [
        innerValue,
        disabled,
        updateValue
    ]);
    var curValue = useCallback(function(idx) {
        var modelVal = innerValue;
        var value = typeof idx === 'number' ? modelVal[idx] : modelVal;
        return value;
    }, [
        innerValue
    ]);
    var buttonNumberTransform = useMemo(function() {
        return vertical ? 'translate(100%, -50%)' : 'translate(-50%, -100%)';
    }, [
        vertical
    ]);
    var renderButton = useCallback(function(index) {
        var _obj, _obj1;
        return /*#__PURE__*/ React.createElement(React.Fragment, null, button || /*#__PURE__*/ React.createElement(View, {
            className: classNames("".concat(classPrefix, "-button"), (_obj = {}, _define_property(_obj, "".concat(verticalClassPrefix, "-button"), vertical), _define_property(_obj, "".concat(rtlClassPrefix, "-button"), rtl), _obj)),
            style: {
                transform: 'translate(-50%, -50%)'
            }
        }, currentDescription !== null && /*#__PURE__*/ React.createElement(Text, {
            className: classNames("".concat(classPrefix, "-button-number"), (_obj1 = {}, _define_property(_obj1, "".concat(verticalClassPrefix, "-button-number"), vertical), _define_property(_obj1, "".concat(rtlClassPrefix, "-button-number"), rtl), _obj1)),
            style: {
                transform: buttonNumberTransform
            }
        }, currentDescription ? currentDescription(curValue(index)) : curValue(index))));
    }, [
        button,
        buttonNumberTransform,
        curValue,
        currentDescription,
        rtl,
        rtlClassPrefix,
        vertical
    ]);
    var renderMarks = useCallback(function() {
        if (marksList.length <= 0) return null;
        var _obj;
        var markcls = classNames("".concat(classPrefix, "-mark"), (_obj = {}, _define_property(_obj, "".concat(verticalClassPrefix, "-mark"), vertical), _define_property(_obj, "".concat(rtlClassPrefix, "-mark"), rtl), _define_property(_obj, "".concat(vertical ? verticalClassPrefix : classPrefix, "-mark-hm"), isHm), _obj));
        var textcls = classNames("".concat(classPrefix, "-mark-text"), _define_property({}, "".concat(verticalClassPrefix, "-mark-text"), vertical));
        return /*#__PURE__*/ React.createElement(View, {
            className: markcls
        }, marksList.map(function(mark) {
            var _obj;
            return /*#__PURE__*/ React.createElement(View, {
                key: mark,
                className: markClassName(mark),
                style: marksStyle(mark)
            }, /*#__PURE__*/ React.createElement(Text, {
                className: textcls
            }, Array.isArray(marks) ? marksRef.current[mark] : marks[mark]), /*#__PURE__*/ React.createElement(View, {
                className: classNames("".concat(vertical ? verticalClassPrefix : classPrefix, "-tick"), (_obj = {}, _define_property(_obj, "".concat(vertical ? verticalClassPrefix : classPrefix, "-tick-active"), tickClass(mark)), _define_property(_obj, "".concat(rtlClassPrefix, "-tick"), rtl), _obj))
            }));
        }));
    }, [
        markClassName,
        marks,
        marksList,
        marksStyle,
        rtl,
        rtlClassPrefix,
        tickClass,
        vertical
    ]);
    var renderRangeButton = useCallback(function() {
        return [
            0,
            1
        ].map(function(_, index) {
            var suffix = index === 0 ? 'left' : 'right';
            var _obj;
            var cls = classNames("".concat(classPrefix, "-button-wrapper-").concat(suffix), (_obj = {}, _define_property(_obj, "".concat(verticalClassPrefix, "-button-wrapper-").concat(suffix), vertical), _define_property(_obj, "".concat(rtlClassPrefix, "-button-wrapper-").concat(suffix), rtl), _obj));
            return /*#__PURE__*/ React.createElement(View, {
                key: index,
                className: cls,
                style: {
                    transform: 'translate(-50%, -50%)'
                },
                onTouchStart: function(e) {
                    buttonRef.current = index;
                    onTouchStart(e);
                },
                onTouchMove: onTouchMove,
                onTouchEnd: onTouchEnd,
                onTouchCancel: onTouchEnd,
                onClick: function(e) {
                    return e.stopPropagation();
                }
            }, renderButton(index));
        });
    }, [
        onTouchEnd,
        onTouchMove,
        onTouchStart,
        renderButton,
        vertical,
        rtl,
        rtlClassPrefix
    ]);
    var renderSingleButton = useCallback(function() {
        return /*#__PURE__*/ React.createElement(View, {
            catchMove: true,
            className: classNames("".concat(classPrefix, "-button-wrapper"), _define_property({}, "".concat(verticalClassPrefix, "-button-wrapper"), vertical)),
            style: {
                transform: 'translate(-50%, -50%)'
            },
            onTouchStart: onTouchStart,
            onTouchMove: onTouchMove,
            onTouchEnd: onTouchEnd,
            onTouchCancel: onTouchEnd,
            onClick: function(e) {
                return e.stopPropagation();
            }
        }, renderButton());
    }, [
        onTouchEnd,
        onTouchMove,
        onTouchStart,
        renderButton,
        vertical
    ]);
    var renderButtonWrapper = useCallback(function() {
        return /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-bar ").concat(isHm ? '' : "".concat(classPrefix, "-bar-animate"), "}"),
            style: barStyle
        }, range ? renderRangeButton() : renderSingleButton());
    }, [
        renderRangeButton,
        renderSingleButton,
        range,
        barStyle
    ]);
    return /*#__PURE__*/ React.createElement(View, {
        className: containerClasses,
        style: style
    }, minDescription !== null && /*#__PURE__*/ React.createElement(Text, {
        className: "".concat(classPrefix, "-min")
    }, minDescription || min), /*#__PURE__*/ React.createElement(View, {
        ref: root,
        className: classes,
        onClick: handleClick
    }, renderMarks(), renderButtonWrapper()), maxDescription !== null && /*#__PURE__*/ React.createElement(Text, {
        className: "".concat(classPrefix, "-max")
    }, maxDescription || max));
};
Range.displayName = 'NutRange';
