import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
import React, { useEffect, useImperativeHandle, useMemo } from "react";
import { Tips } from "@nutui/icons-react-taro";
import classNames from "classnames";
import { View } from "@tarojs/components";
import Popup from "../popup/index";
import { useConfig } from "../configprovider/index";
import { ComponentDefaults } from "../../utils/typings";
import { usePropsValue } from "../../hooks/use-props-value";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    value: '',
    visible: false,
    plain: false,
    hideFooter: true,
    length: 6,
    autoFocus: false
});
export var InternalShortPassword = function(props, ref) {
    var locale = useConfig().locale;
    var _ref = _object_spread({}, defaultProps, props), title = _ref.title, description = _ref.description, tips = _ref.tips, outerVisible = _ref.visible, value = _ref.value, error = _ref.error, hideFooter = _ref.hideFooter, length = _ref.length, plain = _ref.plain, style = _ref.style, className = _ref.className, closeable = _ref.closeable, autoFocus = _ref.autoFocus, round = _ref.round, onFocus = _ref.onFocus, onChange = _ref.onChange, onConfirm = _ref.onConfirm, onTips = _ref.onTips, onCancel = _ref.onCancel, onClose = _ref.onClose, onComplete = _ref.onComplete, onOverlayClick = _ref.onOverlayClick, onCloseIconClick = _ref.onCloseIconClick, rest = _object_without_properties(_ref, [
        "title",
        "description",
        "tips",
        "visible",
        "value",
        "error",
        "hideFooter",
        "length",
        "plain",
        "style",
        "className",
        "closeable",
        "autoFocus",
        "round",
        "onFocus",
        "onChange",
        "onConfirm",
        "onTips",
        "onCancel",
        "onClose",
        "onComplete",
        "onOverlayClick",
        "onCloseIconClick"
    ]);
    var classPrefix = 'nut-shortpassword';
    var comLen = useMemo(function() {
        return Math.min(Math.max(4, length || 4), 6);
    }, [
        length
    ]);
    var format = function(val) {
        return val.slice(0, comLen);
    };
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: outerVisible,
        defaultValue: false,
        finalValue: false
    }), 2), visible = _usePropsValue[0], setVisible = _usePropsValue[1];
    var handleClose = function() {
        onClose === null || onClose === void 0 ? void 0 : onClose();
        setVisible(false);
    };
    var actions = {
        open: function() {
            setVisible(true);
        },
        close: function() {
            setVisible(false);
        }
    };
    useImperativeHandle(ref, function() {
        return actions;
    });
    var _usePropsValue1 = _sliced_to_array(usePropsValue({
        value: value,
        onChange: onChange
    }), 2), inputValue = _usePropsValue1[0], setInputValue = _usePropsValue1[1];
    useEffect(function() {
        if (visible && autoFocus) {
            onFocus && onFocus();
        }
    }, [
        visible
    ]);
    useEffect(function() {
        var val = format(value);
        if (val.length >= comLen) {
            onComplete && onComplete(val);
        }
        setInputValue(format(value));
    }, [
        value
    ]);
    var sure = function() {
        onConfirm && onConfirm(inputValue);
    };
    return /*#__PURE__*/ React.createElement(Popup, _object_spread({
        className: "".concat(classPrefix, "-popup"),
        visible: visible,
        closeable: true,
        onOverlayClick: onClose,
        onCloseIconClick: onClose,
        round: round
    }, rest), /*#__PURE__*/ React.createElement(View, {
        className: classNames(classPrefix, className),
        style: style
    }, /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-title")
    }, title || locale.shortpassword.title), /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-description")
    }, description || locale.shortpassword.description), /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-input"),
        onClick: onFocus
    }, /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-input-site")
    }), /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-input-fake")
    }, _to_consumable_array(new Array(comLen).keys()).map(function(item, index) {
        return /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-input-fake-li"),
            key: index
        }, inputValue.length > index && /*#__PURE__*/ React.createElement(React.Fragment, null, plain ? inputValue[index] : /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-input-fake-li-icon")
        })));
    }))), /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-message")
    }, /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-message-error")
    }, error), /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-message-forget"),
        onClick: onTips
    }, tips || /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Tips, {
        className: "".concat(classPrefix, "-message-tips-icon")
    }), locale.shortpassword.tips))), !hideFooter && /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-footer")
    }, /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-footer-cancel"),
        onClick: onCancel
    }, locale.cancel), /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-footer-sure"),
        onClick: sure
    }, locale.confirm))));
};
export var ShortPassword = /*#__PURE__*/ React.forwardRef(InternalShortPassword);
