"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Input = exports.ViewPasswordToggle = exports.InputPure = void 0;
var react_1 = __importStar(require("react"));
var react_hooks_1 = require("@devesharp/react-hooks");
var free_solid_svg_icons_1 = require("@fortawesome/free-solid-svg-icons");
var react_fontawesome_1 = require("@fortawesome/react-fontawesome");
var AppContext_1 = __importDefault(require("../AppProvider/AppContext"));
var LoaderIcon_1 = require("../LoaderIcon");
var mask_utils_1 = require("../MaskText/mask.utils");
var styles_1 = require("../styles");
var Input_style_1 = __importDefault(require("./Input.style"));
var InputCore_1 = require("./InputCore");
exports.InputPure = react_1.default.forwardRef(function InputPure(props, inputRef) {
    var _a;
    var title = props.title, color = props.color, error = props.error, textRight = props.textRight, textCenter = props.textCenter, defaultValue = props.defaultValue, loading = props.loading, prefix = props.prefix, onEnter = props.onEnter, password = props.password, valueInput = props.value, suffix = props.suffix, withoutDotNumber = props.withoutDotNumber, as = props.as, CustomComponent = props.CustomComponent, rest = __rest(props, ["title", "color", "error", "textRight", "textCenter", "defaultValue", "loading", "prefix", "onEnter", "password", "value", "suffix", "withoutDotNumber", "as", "CustomComponent"]);
    var ref = react_1.useRef();
    react_1.useImperativeHandle(inputRef, function () { return ref.current; }, []);
    var _b = react_1.useState(false), focus = _b[0], setFocus = _b[1];
    var value;
    if (valueInput != undefined) {
        value = mask_utils_1.maskValue(props.value, props.mask, props);
    }
    react_hooks_1.useEventListener('keydown.enter', function () { return onEnter && onEnter(ref.current.value); }, inputRef);
    var _c = react_1.useState(true), hidePassword = _c[0], setHidePassword = _c[1];
    var className = ['input-border'];
    if (textRight)
        className.push('text-right');
    if ((rest === null || rest === void 0 ? void 0 : rest.type) === 'hidden')
        className.push('input-hidden');
    if (error)
        className.push('has-error');
    if (focus)
        className.push('focus');
    var context = react_1.useContext(AppContext_1.default);
    var CustomInputComponent = (_a = context === null || context === void 0 ? void 0 : context.customComponents) === null || _a === void 0 ? void 0 : _a.Input;
    if (CustomComponent || CustomInputComponent) {
        var Component = CustomComponent !== null && CustomComponent !== void 0 ? CustomComponent : CustomInputComponent;
        return (react_1.default.createElement(Component, { inputProps: __assign(__assign({ ref: ref, defaultValue: value ? undefined : mask_utils_1.maskValue(defaultValue, props.mask, props), type: hidePassword && password ? 'password' : 'text', value: value }, rest), { onBlur: function (ev) {
                    if (rest.onBlur)
                        rest.onBlur(ev);
                    setFocus(false);
                }, onFocus: function (ev) {
                    if (rest.onFocus)
                        rest.onFocus(ev);
                    setFocus(true);
                }, onInput: function (evt) {
                    if (rest.onInput)
                        rest.onInput(evt.target.value);
                } }), title: title, prefix: prefix, suffix: suffix, error: error, focus: focus, loading: loading, password: password, textCenter: textCenter, hidePassword: hidePassword, setHidePassword: setHidePassword }));
    }
    return (react_1.default.createElement(styles_1.FormStyle, { className: "input-container" },
        react_1.default.createElement(Input_style_1.default, null,
            title && react_1.default.createElement("div", { className: "input-title " + (focus && 'focus') }, title),
            react_1.default.createElement("div", { className: className.join(' ') },
                prefix && react_1.default.createElement("div", { className: "prefix" }, prefix),
                react_1.default.createElement("input", __assign({ ref: ref, defaultValue: value ? undefined : mask_utils_1.maskValue(defaultValue, props.mask, props), type: hidePassword && password ? 'password' : 'text' }, rest, { value: value, autoComplete: "off", onBlur: function (ev) {
                        var _a;
                        (_a = ref.current) === null || _a === void 0 ? void 0 : _a.removeAttribute('autocomplete');
                        if (rest)
                            rest.onBlur(ev);
                        setFocus(false);
                    }, onFocus: function (ev) {
                        var _a;
                        (_a = ref.current) === null || _a === void 0 ? void 0 : _a.setAttribute('autocomplete', 'chrome-off');
                        if (rest.onFocus)
                            rest.onFocus(ev);
                        setFocus(true);
                    }, onInput: function (evt) {
                        if (rest.onInput)
                            rest.onInput(evt.target.value);
                    }, className: "" + (textCenter && ' text-center') })),
                loading && (react_1.default.createElement("div", { className: "suffix suffix-loading" },
                    react_1.default.createElement(LoaderIcon_1.LoaderIcon, { size: 18 }))),
                suffix && react_1.default.createElement("div", { className: "suffix" }, suffix),
                password && react_1.default.createElement(ViewPasswordToggle, { hidePassword: hidePassword, setHidePassword: setHidePassword })),
            error && (react_1.default.createElement("div", { className: "error-input", "data-testid": "input-error" }, error)))));
});
function ViewPasswordToggle(_a) {
    var hidePassword = _a.hidePassword, setHidePassword = _a.setHidePassword;
    return (react_1.default.createElement("div", { className: "suffix password-btn", onClick: function () { return setHidePassword(!hidePassword); } },
        hidePassword && react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: free_solid_svg_icons_1.faEyeSlash }),
        !hidePassword && react_1.default.createElement(react_fontawesome_1.FontAwesomeIcon, { icon: free_solid_svg_icons_1.faEye })));
}
exports.ViewPasswordToggle = ViewPasswordToggle;
exports.Input = react_1.default.forwardRef(function Input(props, inputRef) {
    var ref = react_1.useRef();
    react_1.useImperativeHandle(inputRef, function () { return ref.current; }, []);
    react_hooks_1.useEventListener('input', function () {
        var _a;
        if (ref.current) {
            ref.current.value = mask_utils_1.maskValue((_a = ref.current) === null || _a === void 0 ? void 0 : _a.value, props.mask, props);
        }
    }, ref);
    return react_1.default.createElement(InputCore_1.InputCore, __assign({}, props, { ref: ref, as: exports.InputPure }));
});
//# sourceMappingURL=Input.js.map