"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SideBar", {
    enumerable: true,
    get: function() {
        return SideBar;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
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 _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _components = require("@tarojs/components");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _taro = /*#__PURE__*/ _interop_require_wildcard._(require("@tarojs/taro"));
var _typings = require("../../utils/typings");
var _usepropsvalue = require("../../hooks/use-props-value");
var _useforceupdate = require("../../hooks/use-force-update");
var _raf = /*#__PURE__*/ _interop_require_default._(require("../../utils/raf"));
var _useuuid = require("../../hooks/use-uuid");
var _index = /*#__PURE__*/ _interop_require_default._(require("../sidebaritem/index"));
var _mergeprops = require("../../utils/merge-props");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    contentDuration: 0,
    sidebarDuration: 0
});
var classPrefix = 'nut-sidebar';
var SideBar = function SideBar(props) {
    var _mergeProps = (0, _mergeprops.mergeProps)(defaultProps, props), contentDuration = _mergeProps.contentDuration, sidebarDuration = _mergeProps.sidebarDuration, children = _mergeProps.children, onClick = _mergeProps.onClick, onChange = _mergeProps.onChange, className = _mergeProps.className, rest = (0, _object_without_properties._)(_mergeProps, [
        "contentDuration",
        "sidebarDuration",
        "children",
        "onClick",
        "onChange",
        "className"
    ]);
    var uuid = (0, _useuuid.useUuid)();
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: props.value,
        defaultValue: props.defaultValue,
        finalValue: 0,
        onChange: onChange
    }), 2), value = _usePropsValue[0], setValue = _usePropsValue[1];
    var titleItemsRef = (0, _react.useRef)([]);
    var navRef = (0, _react.useRef)(null);
    var getTitles = function getTitles() {
        var titles = [];
        _react.default.Children.forEach(children, function(child, idx) {
            if (/*#__PURE__*/ _react.default.isValidElement(child)) {
                var _$props = child === null || child === void 0 ? void 0 : child.props;
                if ((_$props === null || _$props === void 0 ? void 0 : _$props.title) || (_$props === null || _$props === void 0 ? void 0 : _$props.value)) {
                    titles.push({
                        title: _$props.title,
                        value: _$props.value || idx,
                        disabled: _$props.disabled
                    });
                }
            }
        });
        return titles;
    };
    var titles = (0, _react.useRef)(getTitles());
    var forceUpdate = (0, _useforceupdate.useForceUpdate)();
    (0, _react.useEffect)(function() {
        titles.current = getTitles();
        var current = '';
        titles.current.forEach(function(title) {
            if (title.value === value) {
                current = value;
            }
        });
        forceUpdate();
    }, [
        children
    ]);
    var classes = (0, _classnames.default)(classPrefix, className);
    var classesTitle = (0, _classnames.default)("".concat(classPrefix, "-titles"), "".concat(classPrefix, "-titles-scrollable"));
    var getRect = function getRect(selector) {
        return new Promise(function(resolve) {
            (0, _taro.createSelectorQuery)().select(selector).boundingClientRect().exec(function() {
                var rect = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
                resolve(rect[0]);
            });
        });
    };
    var getAllRect = function getAllRect(selector) {
        return new Promise(function(resolve) {
            (0, _taro.createSelectorQuery)().selectAll(selector).boundingClientRect().exec(function() {
                var rect = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
                resolve(rect[0]);
            });
        });
    };
    var scrollWithAnimation = (0, _react.useRef)(false);
    var navRectRef = (0, _react.useRef)();
    var titleRectRef = (0, _react.useRef)([]);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(0), 2), scrollTop = _useState[0], setScrollTop = _useState[1];
    var scrollDirection = function scrollDirection(to) {
        if (sidebarDuration === 0) {
            setScrollTop(to);
            return;
        }
        var from = scrollTop;
        var frames = Math.round(sidebarDuration / 16);
        var count = 0;
        function animate() {
            var progress = count / frames;
            var current = from + (to - from) * progress;
            setScrollTop(current);
            if (count < frames) {
                count++;
                (0, _raf.default)(animate);
            }
        }
        animate();
    };
    var scrollIntoView = function scrollIntoView(index) {
        (0, _raf.default)(function() {
            Promise.all([
                getRect("#".concat(classPrefix, "-titles-").concat(uuid, " .").concat(classPrefix, "-list")),
                getAllRect("#".concat(classPrefix, "-titles-").concat(uuid, " .").concat(classPrefix, "-titles-item"))
            ]).then(function(param) {
                var _param = (0, _sliced_to_array._)(param, 2), navRect = _param[0], titleRects = _param[1];
                navRectRef.current = navRect;
                titleRectRef.current = titleRects;
                var titleRect = titleRectRef.current[index];
                if (!titleRect) return;
                (0, _taro.nextTick)(function() {
                    scrollWithAnimation.current = true;
                });
                scrollDirection(titleRect.height * (index - 1));
            });
        });
    };
    var getContentStyle = function getContentStyle() {
        var index = titles.current.findIndex(function(t) {
            return String(t.value) === String(value);
        });
        index = index < 0 ? 0 : index;
        return {
            transform: "translateY(-".concat(index * 100, "%)"),
            transitionDuration: "".concat(contentDuration, "ms")
        };
    };
    (0, _react.useEffect)(function() {
        var index = titles.current.findIndex(function(t) {
            return String(t.value) === String(value);
        });
        index = index < 0 ? 0 : index;
        scrollIntoView(index);
    }, [
        value
    ]);
    var tabChange = function tabChange(item, index) {
        if (item.disabled) return;
        onClick === null || onClick === void 0 ? void 0 : onClick(item.value);
        setValue(item.value);
    };
    return /*#__PURE__*/ _react.default.createElement(_components.View, (0, _object_spread._)({
        className: classes
    }, rest), /*#__PURE__*/ _react.default.createElement(_components.ScrollView, {
        enableFlex: true,
        scrollX: false,
        scrollY: true,
        scrollTop: scrollTop,
        showScrollbar: false,
        scrollWithAnimation: _taro.default.getEnv() !== 'WEB' ? false : scrollWithAnimation.current,
        id: "".concat(classPrefix, "-titles-").concat(uuid),
        className: classesTitle
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, "-list"),
        ref: navRef
    }, titles.current.map(function(item, index) {
        return /*#__PURE__*/ _react.default.createElement(_components.View, {
            ref: function ref(ref) {
                return titleItemsRef.current.push(ref);
            },
            id: "scrollIntoView".concat(index),
            onClick: function onClick(e) {
                tabChange(item, index);
            },
            className: (0, _classnames.default)("".concat(classPrefix, "-titles-item"), (0, _define_property._)({}, "".concat(classPrefix, "-titles-item-active"), !item.disabled && String(item.value) === String(value))),
            key: item.value
        }, /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: (0, _classnames.default)("".concat(classPrefix, "-ellipsis"), "".concat(classPrefix, "-titles-item-text"), (0, _define_property._)({}, "".concat(classPrefix, "-titles-item-disabled"), item.disabled))
        }, item.title));
    }))), /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, "-content-wrap")
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, "-content"),
        style: getContentStyle()
    }, _react.default.Children.map(children, function(child, idx) {
        if (!/*#__PURE__*/ _react.default.isValidElement(child)) {
            return null;
        }
        var childProps = (0, _object_spread_props._)((0, _object_spread._)({}, child.props), {
            active: value === child.props.value
        });
        var _child_props_value;
        if (String(value) !== String((_child_props_value = child.props.value) !== null && _child_props_value !== void 0 ? _child_props_value : idx)) {
            childProps = (0, _object_spread._)({}, childProps);
        }
        return /*#__PURE__*/ _react.default.cloneElement(child, childProps);
    }))));
};
SideBar.displayName = 'NutSideBar';
SideBar.Item = _index.default;
