"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "MenuItem", {
    enumerable: true,
    get: function() {
        return MenuItem;
    }
});
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 _taro = require("@tarojs/taro");
var _components = require("@tarojs/components");
var _reacttransitiongroup = require("react-transition-group");
var _iconsreacttaro = require("@nutui/icons-react-taro");
var _getsysteminfo = require("../../utils/taro/get-system-info");
var _overlay = require("../overlay/overlay");
var _getrect = require("../../utils/taro/get-rect");
var _typings = require("../../utils/typings");
var _usepropsvalue = require("../../hooks/use-props-value");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    titleIcon: null,
    columns: 1,
    direction: 'down',
    icon: null,
    closeOnClickAway: true,
    activeTitleClass: '',
    inactiveTitleClass: '',
    onChange: function onChange(value) {
        return undefined;
    }
});
var MenuItem = /*#__PURE__*/ (0, _react.forwardRef)(function(props, ref) {
    var _$_object_spread = (0, _object_spread._)({}, defaultProps, props), className = _$_object_spread.className, style = _$_object_spread.style, options = _$_object_spread.options, value = _$_object_spread.value, defaultValue = _$_object_spread.defaultValue, columns = _$_object_spread.columns, title = _$_object_spread.title, icon = _$_object_spread.icon, direction = _$_object_spread.direction, onChange = _$_object_spread.onChange, activeTitleClass = _$_object_spread.activeTitleClass, inactiveTitleClass = _$_object_spread.inactiveTitleClass, closeOnClickAway = _$_object_spread.closeOnClickAway, children = _$_object_spread.children, activeColor = _$_object_spread.activeColor, show = _$_object_spread.show, parent = _$_object_spread.parent, index = _$_object_spread.index;
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(show), 2), showPopup = _useState[0], setShowPopup = _useState[1];
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        defaultValue: defaultValue,
        value: value,
        finalValue: undefined,
        onChange: function onChange1(v) {
            var _options_filter = (0, _sliced_to_array._)(options.filter(function(o) {
                return o.value === v;
            }), 1), option = _options_filter[0];
            onChange === null || onChange === void 0 ? void 0 : onChange(option);
        }
    }), 2), innerValue = _usePropsValue[0], setValue = _usePropsValue[1];
    var cssRef = (0, _react.useRef)(null);
    (0, _react.useEffect)(function() {
        setShowPopup(show);
    }, [
        show
    ]);
    var getParentOffset = (0, _react.useCallback)(function() {
        setTimeout(function() {
            return (0, _async_to_generator._)(function() {
                var p, rect;
                return (0, _ts_generator._)(this, function(_state) {
                    switch(_state.label){
                        case 0:
                            p = parent.menuRef.current;
                            return [
                                4,
                                (0, _getrect.getRectInMultiPlatform)(p)
                            ];
                        case 1:
                            rect = _state.sent();
                            setPosition({
                                height: rect.height,
                                top: rect.top
                            });
                            return [
                                2
                            ];
                    }
                });
            })();
        }, 100);
    }, [
        parent.menuRef
    ]);
    (0, _react.useEffect)(function() {
        getParentOffset();
    }, [
        showPopup,
        getParentOffset
    ]);
    var windowHeight = (0, _react.useMemo)(function() {
        return (0, _getsysteminfo.getWindowInfo)().windowHeight;
    }, []);
    var updateItemOffset = (0, _react.useCallback)(function() {
        if (!parent.lockScroll) return;
        var p = parent.menuRef.current;
        (0, _getrect.getRectInMultiPlatform)(p).then(function(rect) {
            if (rect) {
                setPosition({
                    height: rect.height,
                    top: rect.top
                });
            }
        });
    }, [
        direction,
        windowHeight,
        parent.lockScroll,
        parent.menuRef
    ]);
    (0, _taro.usePageScroll)(updateItemOffset);
    (0, _react.useImperativeHandle)(ref, function() {
        return {
            toggle: function toggle(s) {
                var from = 'REF';
                s ? parent.toggleMenuItem(index, from) : parent.hideMenuItem(index, from);
            }
        };
    });
    var getIconCName = function getIconCName(optionVal, value) {
        var _obj;
        return (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, activeTitleClass, optionVal === value), (0, _define_property._)(_obj, inactiveTitleClass, optionVal !== value), _obj));
    };
    var setTitle = function setTitle(text) {
        if (!title) {
            parent.updateTitle(text, index);
        }
    };
    var handleClick = function handleClick(item) {
        parent.toggleMenuItem(index);
        setTitle(item.text);
        setValue(item.value);
    };
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)({
        top: 0,
        height: 0
    }), 2), position = _useState1[0], setPosition = _useState1[1];
    var isShow = function isShow() {
        if (showPopup) return {};
        return {
            display: 'none'
        };
    };
    var getPosition = function getPosition() {
        return direction === 'down' ? {
            top: "".concat(position.top + position.height, "px"),
            bottom: '0',
            height: 'initial'
        } : {
            bottom: "".concat((0, _getsysteminfo.getWindowInfo)().windowHeight - position.top, "px"),
            top: '0',
            height: 'initial'
        };
    };
    var placeholderStyle = function placeholderStyle() {
        if (direction === 'down') {
            return (0, _object_spread._)({
                height: "".concat(position.top + position.height, "px")
            }, isShow());
        }
        return (0, _object_spread._)({
            height: "".concat((0, _getsysteminfo.getWindowInfo)().windowHeight - position.top, "px"),
            top: 'auto'
        }, isShow());
    };
    return /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "nut-menu-container"
    }, closeOnClickAway ? /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "nut-menu-placeholder-element ".concat((0, _classnames.default)({
            up: direction === 'up'
        })),
        style: placeholderStyle(),
        onClick: function onClick() {
            return parent.toggleMenuItem(index);
        }
    }) : null, parent.overlay ? /*#__PURE__*/ _react.default.createElement(_overlay.Overlay, {
        className: "nut-menu-container-overlay",
        style: getPosition(),
        lockScroll: parent.lockScroll,
        visible: showPopup,
        closeOnOverlayClick: parent.closeOnOverlayClick,
        onClick: function onClick() {
            parent.closeOnOverlayClick && parent.hideMenuItem(index);
        }
    }) : null, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: (0, _classnames.default)({
            'nut-menu-container-wrap': direction === 'down',
            'nut-menu-container-wrap-up': direction !== 'down'
        }, className),
        style: (0, _object_spread._)({}, style, isShow())
    }, /*#__PURE__*/ _react.default.createElement(_reacttransitiongroup.CSSTransition, {
        nodeRef: cssRef,
        in: showPopup,
        timeout: 100,
        classNames: direction === 'down' ? 'nut-menu-container-down' : 'nut-menu-container-up'
    }, /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "nut-menu-container-content"
    }, options === null || options === void 0 ? void 0 : options.map(function(item, index) {
        return /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "nut-menu-container-item ".concat((0, _classnames.default)({
                active: item.value === innerValue
            })),
            key: item.text,
            style: {
                flexBasis: "".concat(100 / columns, "%")
            },
            onClick: function onClick() {
                handleClick(item);
            }
        }, item.value === innerValue ? /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, icon ? /*#__PURE__*/ _react.default.cloneElement(icon, {
            className: "nut-menu-container-item-icon ".concat(icon.props.className || '')
        }) : /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.Check, {
            color: activeColor,
            className: "nut-menu-container-item-icon ".concat(getIconCName(item.value, value))
        })) : null, /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "nut-menu-container-item-title ".concat(getIconCName(item.value, value)),
            style: {
                color: "".concat(item.value === innerValue ? activeColor : '')
            }
        }, item.text));
    }), children))));
});
MenuItem.displayName = 'NutMenuItem';
