"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Input", {
    enumerable: true,
    get: function() {
        return Input;
    }
});
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 _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _components = require("@tarojs/components");
var _iconsreacttaro = require("@nutui/icons-react-taro");
var _taro = /*#__PURE__*/ _interop_require_wildcard._(require("@tarojs/taro"));
var _utils = require("./utils");
var _index = require("../configprovider/index");
var _typings = require("../../utils/typings");
var _usepropsvalue = require("../../hooks/use-props-value");
var ENV_TYPE = {
    WEAPP: 'WEAPP',
    SWAN: 'SWAN',
    ALIPAY: 'ALIPAY',
    TT: 'TT',
    QQ: 'QQ',
    JD: 'JD',
    WEB: 'WEB',
    RN: 'RN',
    HARMONY: 'HARMONY',
    QUICKAPP: 'QUICKAPP'
};
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    type: 'text',
    name: '',
    placeholder: undefined,
    confirmType: 'done',
    align: 'left',
    required: false,
    disabled: false,
    readOnly: false,
    maxLength: 9999,
    clearable: false,
    clearIcon: null,
    formatTrigger: 'onChange',
    autoFocus: false,
    plain: false
});
var Input = /*#__PURE__*/ (0, _react.forwardRef)(function(props, ref) {
    var classPrefix = 'nut-input';
    var rtl = (0, _index.useRtl)();
    var locale = (0, _index.useConfig)().locale;
    var _ref = (0, _object_spread._)({}, defaultProps, props), focus = _ref.focus, type = _ref.type, name = _ref.name, placeholder = _ref.placeholder, align = _ref.align, disabled = _ref.disabled, readOnly = _ref.readOnly, maxLength = _ref.maxLength, clearable = _ref.clearable, clearIcon = _ref.clearIcon, formatTrigger = _ref.formatTrigger, autoFocus = _ref.autoFocus, style = _ref.style, className = _ref.className, plain = _ref.plain, inputStyle = _ref.inputStyle, onChange = _ref.onChange, onFocus = _ref.onFocus, onBlur = _ref.onBlur, onClear = _ref.onClear, formatter = _ref.formatter, onClick = _ref.onClick, confirmType = _ref.confirmType, defaultValue = _ref.defaultValue, _value = _ref.value, rest = (0, _object_without_properties._)(_ref, [
        "focus",
        "type",
        "name",
        "placeholder",
        "align",
        "disabled",
        "readOnly",
        "maxLength",
        "clearable",
        "clearIcon",
        "formatTrigger",
        "autoFocus",
        "style",
        "className",
        "plain",
        "inputStyle",
        "onChange",
        "onFocus",
        "onBlur",
        "onClear",
        "formatter",
        "onClick",
        "confirmType",
        "defaultValue",
        "value"
    ]);
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: _value,
        defaultValue: defaultValue,
        finalValue: '',
        onChange: onChange
    }), 2), value = _usePropsValue[0], setValue = _usePropsValue[1];
    var inputRef = (0, _react.useRef)(null);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), active = _useState[0], setActive = _useState[1];
    // 兼容H5和小程序获取原生input标签
    var getNativeInput = function getNativeInput() {
        if (_taro.default.getEnv() === 'WEB') {
            var taroInputCoreEl = inputRef.current;
            var inputEl = taroInputCoreEl.querySelector('input');
            return inputEl;
        }
        return inputRef.current;
    };
    (0, _react.useImperativeHandle)(ref, function() {
        return {
            clear: function clear() {
                setValue('');
            },
            focus: function focus() {
                var nativeInput = getNativeInput();
                nativeInput === null || nativeInput === void 0 ? void 0 : nativeInput.focus();
            },
            blur: function blur() {
                var nativeInput = getNativeInput();
                nativeInput === null || nativeInput === void 0 ? void 0 : nativeInput.blur();
            },
            get nativeElement () {
                return inputRef.current;
            }
        };
    });
    var inputClass = (0, _react.useCallback)(function() {
        return [
            classPrefix,
            "".concat(disabled ? "".concat(classPrefix, "-disabled") : ''),
            readOnly ? "".concat(classPrefix, "-readonly") : '',
            "".concat(plain ? "".concat(classPrefix, "-plain") : "".concat(classPrefix, "-container"))
        ].filter(Boolean).join(' ');
    }, [
        disabled
    ]);
    var _React_useState = (0, _sliced_to_array._)(_react.default.useState(), 2), updateState = _React_useState[1];
    var forceUpdate = _react.default.useCallback(function() {
        return updateState({});
    }, []);
    var updateValue = function updateValue(value) {
        var trigger = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'onChange';
        var val = value;
        if (type === 'number') val = (0, _utils.formatNumber)(val, false, true);
        if (type === 'digit') val = (0, _utils.formatNumber)(val, true, true);
        if (formatter && trigger === formatTrigger) val = formatter(val);
        setValue(val);
        if (trigger !== 'onChange') {
            var eventHandler = props[trigger];
            eventHandler === null || eventHandler === void 0 ? void 0 : eventHandler(val);
        }
        forceUpdate();
    };
    var handleFocus = function handleFocus(event) {
        if (_taro.default.getEnv() === 'WEB') {
            var val = event.target.value;
            onFocus && onFocus(val);
        } else {
            var height = (event.detail || {}).height;
            onFocus === null || onFocus === void 0 ? void 0 : onFocus(value, height);
        }
        setActive(true);
    };
    var handleInput = function handleInput(event) {
        updateValue((event.detail || event.currentTarget).value, 'onChange');
    };
    var handleBlur = function handleBlur(event) {
        var val = _taro.default.getEnv() === 'WEB' ? event.target.value : event.detail.value;
        updateValue(val, 'onBlur');
        setTimeout(function() {
            return setActive(false);
        }, 200);
    };
    var inputType = function inputType(type) {
        if ((0, _taro.getEnv)() === ENV_TYPE.WEB) {
            if (type === 'digit') return 'text';
            if (type === 'number') return 'tel';
        } else if (type === 'password') {
            return 'text';
        }
        return type;
    };
    var getTextAlign = function getTextAlign() {
        if (rtl) {
            if (align === 'right') return 'left';
            if (align === 'left') return 'right';
        }
        return align;
    };
    return /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(inputClass(), "  ").concat(className || ''),
        style: style,
        onClick: function onClick1(e) {
            onClick && onClick(e);
        }
    }, /*#__PURE__*/ _react.default.createElement(_components.Input, (0, _object_spread_props._)((0, _object_spread._)({}, rest), {
        name: name,
        className: "nut-input-native",
        ref: inputRef,
        style: (0, _object_spread._)({}, {
            textAlign: getTextAlign()
        }, inputStyle),
        type: inputType(type),
        password: type === 'password',
        maxlength: maxLength,
        placeholder: placeholder === undefined ? locale.placeholder : placeholder,
        placeholderClass: "".concat(classPrefix, "-placeholder"),
        disabled: disabled,
        value: value,
        focus: autoFocus || focus,
        confirmType: confirmType,
        onBlur: handleBlur,
        onFocus: handleFocus,
        onInput: handleInput
    })), /*#__PURE__*/ _react.default.createElement(_components.View, {
        style: {
            display: clearable && !readOnly && active && value.length > 0 ? 'flex' : 'none',
            alignItems: 'center',
            cursor: 'pointer'
        },
        onClick: function onClick(e) {
            e.stopPropagation();
            if (!disabled) {
                setValue('');
                onClear === null || onClear === void 0 ? void 0 : onClear('');
            }
        }
    }, clearIcon || /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.MaskClose, {
        className: "nut-input-clear"
    })));
});
Input.displayName = 'NutInput';
