"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 _iconsreact = require("@nutui/icons-react");
var _utils = require("./utils");
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), {
    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 rtl = (0, _configprovider.useRtl)();
    var locale = (0, _configprovider.useConfig)().locale;
    var _ref = (0, _object_spread._)({}, defaultProps, props), 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, onChange = _ref.onChange, onFocus = _ref.onFocus, onClear = _ref.onClear, formatter = _ref.formatter, onClick = _ref.onClick, confirmType = _ref.confirmType, plain = _ref.plain, defaultValue = _ref.defaultValue, _value = _ref.value, onCompositionStart = _ref.onCompositionStart, onCompositionEnd = _ref.onCompositionEnd, rest = (0, _object_without_properties._)(_ref, [
        "type",
        "name",
        "placeholder",
        "align",
        "disabled",
        "readOnly",
        "maxLength",
        "clearable",
        "clearIcon",
        "formatTrigger",
        "autoFocus",
        "style",
        "className",
        "onChange",
        "onFocus",
        "onClear",
        "formatter",
        "onClick",
        "confirmType",
        "plain",
        "defaultValue",
        "value",
        "onCompositionStart",
        "onCompositionEnd"
    ]);
    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 composingRef = (0, _react.useRef)(false);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), active = _useState[0], setActive = _useState[1];
    (0, _react.useImperativeHandle)(ref, function() {
        return {
            clear: function clear() {
                setValue('');
            },
            focus: function focus() {
                var _inputRef_current;
                (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : _inputRef_current.focus();
            },
            blur: function blur() {
                var _inputRef_current;
                (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : _inputRef_current.blur();
            },
            get nativeElement () {
                return inputRef.current;
            }
        };
    });
    var inputClass = (0, _react.useCallback)(function() {
        var classPrefix = 'nut-input';
        return [
            classPrefix,
            disabled ? "".concat(classPrefix, "-disabled") : '',
            readOnly ? "".concat(classPrefix, "-readonly") : '',
            plain ? "".concat(classPrefix, "-plain") : "".concat(classPrefix, "-container")
        ].filter(Boolean).join(' ');
    }, [
        disabled
    ]);
    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);
        var eventHandler = props[trigger];
        if (eventHandler && typeof eventHandler === 'function' && trigger !== 'onChange') {
            eventHandler(val);
        }
    };
    var handleFocus = function handleFocus(event) {
        var val = event.target.value;
        onFocus && onFocus(val);
        setActive(true);
    };
    var handleInput = function handleInput(value) {
        updateValue(value, 'onChange');
    };
    var handleBlur = function handleBlur(event) {
        var val = event.target.value;
        updateValue(val, 'onBlur');
        setTimeout(function() {
            setActive(false);
        }, 200);
    };
    var inputType = function inputType(type) {
        if (type === 'digit') {
            return 'text';
        }
        if (type === 'number') {
            return 'tel';
        }
        return type;
    };
    return /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(inputClass(), "  ").concat(className || ''),
        style: style,
        onClick: function onClick1(e) {
            onClick && onClick(e);
        }
    }, /*#__PURE__*/ _react.default.createElement("input", (0, _object_spread_props._)((0, _object_spread._)({}, rest), {
        name: name,
        className: "nut-input-native",
        ref: inputRef,
        style: {
            // eslint-disable-next-line no-nested-ternary
            textAlign: rtl ? align === 'right' ? 'left' : align === 'left' ? 'right' : 'center' : align
        },
        type: inputType(type),
        maxLength: maxLength,
        placeholder: placeholder === undefined ? locale.placeholder : placeholder,
        disabled: disabled,
        readOnly: readOnly,
        value: value,
        autoFocus: autoFocus,
        enterKeyHint: confirmType,
        onBlur: function onBlur(e) {
            handleBlur(e);
        },
        onFocus: function onFocus(e) {
            handleFocus(e);
        },
        onChange: function onChange(e) {
            handleInput(e.currentTarget.value);
        },
        onCompositionStart: function onCompositionStart1(e) {
            composingRef.current = true;
            onCompositionStart === null || onCompositionStart === void 0 ? void 0 : onCompositionStart(e);
        },
        onCompositionEnd: function onCompositionEnd1(e) {
            composingRef.current = false;
            onCompositionEnd === null || onCompositionEnd === void 0 ? void 0 : onCompositionEnd(e);
        }
    })), clearable && !readOnly && active && value.length > 0 ? /*#__PURE__*/ _react.default.createElement("span", {
        style: {
            display: 'flex',
            alignItems: 'center',
            cursor: 'pointer'
        },
        onClick: function onClick() {
            if (!disabled) {
                setValue('');
                onClear && onClear('');
            }
        }
    }, clearIcon || /*#__PURE__*/ _react.default.createElement(_iconsreact.MaskClose, {
        className: "nut-input-clear"
    })) : null);
});
Input.displayName = 'NutInput';
