"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    InternalShortPassword: function() {
        return InternalShortPassword;
    },
    ShortPassword: function() {
        return ShortPassword;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
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 _to_consumable_array = require("@swc/helpers/_/_to_consumable_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _iconsreact = require("@nutui/icons-react");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _index = /*#__PURE__*/ _interop_require_default._(require("../popup/index"));
var _configprovider = require("../configprovider");
var _typings = require("../../utils/typings");
var _usepropsvalue = require("../../hooks/use-props-value");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    value: '',
    visible: false,
    plain: false,
    hideFooter: true,
    length: 6,
    autoFocus: false
});
var InternalShortPassword = function InternalShortPassword(props, ref) {
    var locale = (0, _configprovider.useConfig)().locale;
    var _ref = (0, _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, 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 = (0, _object_without_properties._)(_ref, [
        "title",
        "description",
        "tips",
        "visible",
        "value",
        "error",
        "hideFooter",
        "length",
        "plain",
        "style",
        "className",
        "closeable",
        "autoFocus",
        "onFocus",
        "onChange",
        "onConfirm",
        "onTips",
        "onCancel",
        "onClose",
        "onComplete",
        "onOverlayClick",
        "onCloseIconClick"
    ]);
    var classPrefix = 'nut-shortpassword';
    var comLen = (0, _react.useMemo)(function() {
        return Math.min(Math.max(4, length || 4), 6);
    }, [
        length
    ]);
    var format = function format(val) {
        return val.slice(0, comLen);
    };
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: outerVisible,
        defaultValue: false,
        finalValue: false
    }), 2), visible = _usePropsValue[0], setVisible = _usePropsValue[1];
    var handleClose = function handleClose() {
        onClose === null || onClose === void 0 ? void 0 : onClose();
        setVisible(false);
    };
    var actions = {
        open: function open() {
            setVisible(true);
        },
        close: function close() {
            setVisible(false);
        }
    };
    (0, _react.useImperativeHandle)(ref, function() {
        return actions;
    });
    var _usePropsValue1 = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: value,
        onChange: onChange
    }), 2), inputValue = _usePropsValue1[0], setInputValue = _usePropsValue1[1];
    (0, _react.useEffect)(function() {
        if (visible && autoFocus) {
            onFocus && onFocus();
        }
    }, [
        visible
    ]);
    (0, _react.useEffect)(function() {
        var val = format(value);
        if (val.length >= comLen) {
            onComplete && onComplete(val);
        }
        setInputValue(format(value));
    }, [
        value
    ]);
    var sure = function sure() {
        onConfirm && onConfirm(inputValue);
    };
    return /*#__PURE__*/ _react.default.createElement(_index.default, (0, _object_spread._)({
        style: {
            padding: '32px 24px 28px 24px',
            borderRadius: '12px',
            textAlign: 'center'
        },
        visible: visible,
        closeable: true,
        onOverlayClick: handleClose,
        onCloseIconClick: handleClose
    }, rest), /*#__PURE__*/ _react.default.createElement("div", {
        className: (0, _classnames.default)(classPrefix, className),
        style: style
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-title")
    }, title || locale.shortpassword.title), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-description")
    }, description || locale.shortpassword.description), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-input"),
        onClick: onFocus
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-input-site")
    }), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-input-fake")
    }, (0, _to_consumable_array._)(new Array(comLen).keys()).map(function(item, index) {
        return /*#__PURE__*/ _react.default.createElement("div", {
            className: "".concat(classPrefix, "-input-fake-li"),
            key: index
        }, inputValue.length > index && /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, plain ? inputValue[index] : /*#__PURE__*/ _react.default.createElement("div", {
            className: "".concat(classPrefix, "-input-fake-li-icon")
        })));
    }))), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-message")
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-message-error")
    }, error), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-message-forget"),
        onClick: onTips
    }, tips || /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_iconsreact.Tips, {
        width: 11,
        height: 11
    }), locale.shortpassword.tips))), !hideFooter && /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-footer")
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-footer-cancel"),
        onClick: onCancel
    }, locale.cancel), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-footer-sure"),
        onClick: sure
    }, locale.confirm))));
};
var ShortPassword = /*#__PURE__*/ _react.default.forwardRef(InternalShortPassword);
