"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectAutocomplete = void 0;
var react_1 = __importStar(require("react"));
var _ = __importStar(require("lodash"));
var AutocompletePure_1 = require("../AutocompletePure");
var CloseBtn_1 = require("../CloseBtn");
var Input_1 = require("../Input");
var UnformInput_1 = require("../UnformInput");
var S = __importStar(require("./SelectAutocomplete.style"));
var InputAutoComplete = react_1.default.forwardRef(function InputAutoComplete(_a, ref) {
    var _handleInputChange = _a._handleInputChange, _openAutoComplete = _a._openAutoComplete, _closeAutoComplete = _a._closeAutoComplete, _loading = _a._loading, onChange = _a.onChange, valueExternal = _a.value, placeholder = _a.placeholder, name = _a.name, rest = __rest(_a, ["_handleInputChange", "_openAutoComplete", "_closeAutoComplete", "_loading", "onChange", "value", "placeholder", "name"]);
    var inputRef = react_1.useRef();
    var _b = react_1.useState(false), typing = _b[0], setTyping = _b[1];
    var _c = react_1.useState(''), textSelected = _c[0], setTextSelected = _c[1];
    var _d = react_1.useState(null), _value = _d[0], setValue = _d[1];
    var value = valueExternal || _value;
    var _e = react_1.useState(), error = _e[0], setError = _e[1];
    react_1.useLayoutEffect(function () {
        if (!_.isEqual(valueExternal, value)) {
            setValue(valueExternal);
            setTextSelected(valueExternal === null || valueExternal === void 0 ? void 0 : valueExternal.name);
        }
    }, [valueExternal]);
    react_1.useImperativeHandle(ref, function () { return ({
        onSelect: function (item) {
            inputRef.current.value = '';
            if (rest.onInput)
                rest.onInput('');
            setTyping(false);
            setTextSelected(item.name);
            setValue(item
                ? {
                    name: item === null || item === void 0 ? void 0 : item.name,
                    value: item === null || item === void 0 ? void 0 : item.value,
                }
                : null);
            if (onChange)
                onChange(item);
        },
    }); });
    function onInput(v) {
        setTyping(inputRef.current.value != '');
        _handleInputChange(v);
        if (rest.onInput)
            rest.onInput(v);
    }
    function clearInput() {
        setTextSelected('');
        setValue(null);
        if (onChange)
            onChange(null);
    }
    function onFocus(e) {
        _openAutoComplete();
        if (rest.onFocus)
            rest.onFocus(e);
    }
    function onBlur(e) {
        _closeAutoComplete();
        inputRef.current.value = '';
        setTyping(false);
        if (rest.onBlur)
            rest.onBlur(e);
    }
    function onChangeUnform(item) {
        setValue(item
            ? {
                name: item === null || item === void 0 ? void 0 : item.name,
                value: item === null || item === void 0 ? void 0 : item.value,
            }
            : null);
        setTextSelected(item === null || item === void 0 ? void 0 : item.name);
        if (onChange)
            onChange(item);
    }
    function onError(e) {
        setError(e);
    }
    return (react_1.default.createElement(react_1.default.Fragment, null,
        !!name && react_1.default.createElement(UnformInput_1.UnformInput, { name: name, value: value, onChange: onChangeUnform, onError: onError }),
        react_1.default.createElement(Input_1.Input, __assign({}, rest, { ref: inputRef, error: error, onInput: onInput, onFocus: onFocus, onBlur: onBlur, loading: _loading, placeholder: !textSelected && !typing ? placeholder : '', "data-testid": "autocomplete--input", className: "focus", prefix: react_1.default.createElement(react_1.default.Fragment, null, !typing && (react_1.default.createElement("div", { className: "text-selected", "data-testid": "text-selected-label" }, textSelected))), suffix: react_1.default.createElement(react_1.default.Fragment, null, textSelected && (react_1.default.createElement(CloseBtn_1.CloseBtn, { size: 13, color: "#444", onClick: clearInput, "data-testid": "select-autocomplete--clear" }))) }))));
});
function SelectAutocomplete(props) {
    return (react_1.default.createElement(S.Container, { className: "select-autocomplete" },
        react_1.default.createElement(AutocompletePure_1.AutocompletePure, __assign({}, props, { as: InputAutoComplete }))));
}
exports.SelectAutocomplete = SelectAutocomplete;
//# sourceMappingURL=SelectAutocomplete.js.map