"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PullToRefresh", {
    enumerable: true,
    get: function() {
        return PullToRefresh;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _ts_generator = require("@swc/helpers/_/_ts_generator");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _components = require("@tarojs/components");
var _iconsreacttaro = require("@nutui/icons-react-taro");
var _taro = /*#__PURE__*/ _interop_require_default._(require("@tarojs/taro"));
var _index = require("../configprovider/index");
var _usetouch = require("../../hooks/use-touch");
var _rubberband = require("../../utils/rubberband");
var _sleep = require("../../utils/sleep");
var _typings = require("../../utils/typings");
var _pxtransform = require("../../utils/taro/px-transform");
var _getsysteminfo = require("../../utils/taro/get-system-info");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    type: 'default',
    pullingText: '',
    canReleaseText: '',
    refreshingText: '',
    completeText: '',
    completeDelay: 500,
    disabled: false,
    headHeight: 80,
    threshold: 60,
    scrollTop: 0,
    onRefresh: function onRefresh() {}
});
var PullToRefresh = function PullToRefresh(p) {
    var doRefresh = function doRefresh() {
        return (0, _async_to_generator._)(function() {
            var e;
            return (0, _ts_generator._)(this, function(_state) {
                switch(_state.label){
                    case 0:
                        setHeight(headHeight);
                        setStatus('refreshing');
                        _state.label = 1;
                    case 1:
                        _state.trys.push([
                            1,
                            3,
                            ,
                            4
                        ]);
                        return [
                            4,
                            props.onRefresh()
                        ];
                    case 2:
                        _state.sent();
                        setStatus('complete');
                        return [
                            3,
                            4
                        ];
                    case 3:
                        e = _state.sent();
                        setHeight(0);
                        setStatus('pulling');
                        throw e;
                    case 4:
                        if (!(props.completeDelay > 0)) return [
                            3,
                            6
                        ];
                        return [
                            4,
                            (0, _sleep.sleep)(props.completeDelay)
                        ];
                    case 5:
                        _state.sent();
                        _state.label = 6;
                    case 6:
                        setHeight(0);
                        setStatus('pulling');
                        return [
                            2
                        ];
                }
            });
        })();
    };
    var classPrefix = 'nut-pulltorefresh';
    var locale = (0, _index.useConfig)().locale;
    var touch = (0, _usetouch.useTouch)();
    var props = (0, _object_spread._)({}, defaultProps, p, {
        pullingText: p.pullingText || locale.pullToRefresh.pullingText,
        canReleaseText: p.canReleaseText || locale.pullToRefresh.canReleaseText,
        refreshingText: p.refreshingText || locale.pullToRefresh.refreshingText,
        completeText: p.completeText || locale.pullToRefresh.completeText
    });
    var classes = (0, _classnames.default)(classPrefix, props.className, "".concat(classPrefix, "-").concat(props.type));
    var headHeight = props.headHeight;
    var threshold = props.threshold;
    var pullingRef = (0, _react.useRef)(false);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)('pulling'), 2), status = _useState[0], setStatus = _useState[1];
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)(0), 2), height = _useState1[0], setHeight = _useState1[1];
    var timer = (0, _react.useRef)();
    var renderIcons = function renderIcons(status) {
        return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, (status === 'pulling' || status === 'complete') && /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.Loading, null), (status === 'canRelease' || status === 'refreshing') && /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.More, null));
    };
    var renderStatusIcon = function renderStatusIcon() {
        if (props.renderIcon) {
            var _props_renderIcon;
            return (_props_renderIcon = props.renderIcon) === null || _props_renderIcon === void 0 ? void 0 : _props_renderIcon.call(props, status);
        }
        return renderIcons(status);
    };
    var renderStatusText = function renderStatusText() {
        if (props.renderText) {
            var _props_renderText;
            return (_props_renderText = props.renderText) === null || _props_renderText === void 0 ? void 0 : _props_renderText.call(props, status);
        }
        if (status === 'pulling') return props.pullingText;
        if (status === 'canRelease') return props.canReleaseText;
        if (status === 'refreshing') return props.refreshingText;
        if (status === 'complete') return props.completeText;
        return '';
    };
    var handleTouchStart = function handleTouchStart(e) {
        if (props.disabled) return;
        touch.start(e);
    };
    var handleTouchMove = function handleTouchMove(e) {
        if (props.scrollTop > 0 || props.disabled) {
            return;
        }
        if (status === 'refreshing' || status === 'complete') return;
        // touch 的封装不好，比如在使用高度控制的时候，就很迷
        touch.move(e);
        if (touch.isVertical()) {
            pullingRef.current = true;
            setHeight(Math.max((0, _rubberband.rubberbandIfOutOfBounds)(touch.deltaY.current, 0, 0, headHeight * 5, 0.5), 0));
            setStatus(height > threshold ? 'canRelease' : 'pulling');
        }
        clearTimeout(timer.current);
    };
    var handleTouchEnd = function handleTouchEnd() {
        if (props.disabled) return;
        pullingRef.current = false;
        if (status === 'canRelease') {
            doRefresh();
        } else {
            // 时序问题，可能会先setStatus再修改heightRef，导致下拉不到位后组件不会自动回弹
            setHeight(0);
            // heightRef.current = 0
            setStatus('pulling');
        }
    };
    // 安卓微信小程序onTouchMove回调次数少导致下拉卡顿，增加动效会更顺畅
    var isAndroidWeApp = (0, _getsysteminfo.getDeviceInfo)().platform === 'android' && _taro.default.getEnv() === 'WEAPP';
    var springStyles = (0, _object_spread._)({
        height: (0, _pxtransform.pxTransform)(height)
    }, !pullingRef.current || isAndroidWeApp ? {
        transition: 'height .3s ease'
    } : {});
    var _obj, _obj1, _obj2, _obj3, _obj4;
    return /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: classes,
        style: props.style,
        catchMove: props.catchMove,
        onTouchStart: handleTouchStart,
        onTouchMove: handleTouchMove,
        onTouchEnd: handleTouchEnd
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        style: springStyles,
        className: (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-head"), true), (0, _define_property._)(_obj, "".concat(classPrefix, "-primary-head"), props.type === 'primary'), _obj))
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)((_obj1 = {}, (0, _define_property._)(_obj1, "".concat(classPrefix, "-head-content"), true), (0, _define_property._)(_obj1, "".concat(classPrefix, "-primary-head-content"), props.type === 'primary'), _obj1)),
        style: {
            height: (0, _pxtransform.pxTransform)(headHeight)
        }
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)((_obj2 = {}, (0, _define_property._)(_obj2, "".concat(classPrefix, "-status-icon"), true), (0, _define_property._)(_obj2, "".concat(classPrefix, "-primary-status-icon"), props.type === 'primary'), _obj2))
    }, renderStatusIcon()), /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)((_obj3 = {}, (0, _define_property._)(_obj3, "".concat(classPrefix, "-status-text"), true), (0, _define_property._)(_obj3, "".concat(classPrefix, "-primary-status-text"), props.type === 'primary'), _obj3))
    }, renderStatusText()))), /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)((_obj4 = {}, (0, _define_property._)(_obj4, "".concat(classPrefix, "-content"), true), (0, _define_property._)(_obj4, "".concat(classPrefix, "-primary-content}"), props.type === 'primary'), _obj4))
    }, props.children));
};
PullToRefresh.displayName = 'NutPullToRefresh';
