"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 __spreadArray = (this && this.__spreadArray) || function (to, from) {
    for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
        to[j] = from[i];
    return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutocompleteCoreWithoutUnform = exports.AutocompleteCoreWithUnform = exports.InputTagsCore = void 0;
var react_1 = __importStar(require("react"));
var core_1 = require("@unform/core");
var use_immer_1 = require("use-immer");
exports.InputTagsCore = react_1.default.forwardRef(function AutocompleteCore(props, inputRef) {
    var ref = react_1.useRef();
    react_1.useImperativeHandle(inputRef, function () { return ref.current; }, []);
    if (props.name) {
        return react_1.default.createElement(exports.AutocompleteCoreWithUnform, __assign({}, props, { ref: ref }));
    }
    return react_1.default.createElement(exports.AutocompleteCoreWithoutUnform, __assign({}, props, { ref: ref }));
});
exports.AutocompleteCoreWithUnform = react_1.default.forwardRef(function AutocompleteCoreWithUnform(props, inputRef) {
    var ref = react_1.useRef();
    react_1.useImperativeHandle(inputRef, function () { return ref.current; }, []);
    var _a = core_1.useField(props.name), fieldName = _a.fieldName, registerField = _a.registerField, _b = _a.defaultValue, defaultValue = _b === void 0 ? [] : _b, error = _a.error, clearError = _a.clearError;
    var _c = use_immer_1.useImmer(defaultValue), tags = _c[0], setTags = _c[1];
    var refTags = react_1.useRef(tags);
    function removeTag(tagValue) {
        setTags(function (v) {
            if (v.find(function (tag) { return tag.id == tagValue.id; })) {
                var tmp = __spreadArray([], v);
                tmp.splice(v.findIndex(function (tag) { return tag.id == tagValue.id; }), 1);
                refTags.current = tmp;
                return tmp;
            }
            return v;
        });
    }
    react_1.useEffect(function () {
        registerField({
            name: fieldName,
            ref: ref,
            path: 'current',
            clearValue: function (_ref) {
                refTags.current = [];
                setTags([]);
            },
            setValue: function (_ref, value) {
                refTags.current = value;
                setTags(value);
            },
            getValue: function (_ref) {
                return refTags.current;
            },
        });
    }, [fieldName, registerField]);
    var onInput = react_1.useCallback(function onSelectItem(text) {
        if (error)
            clearError();
        if (props.onInput)
            props.onInput(text);
    }, [error]);
    var onSelect = react_1.useCallback(function onSelectItem(text, tagValue) {
        setTags(function (v) {
            if (v.find(function (tag) { return tag.id == tagValue.value.id; })) {
                var tmp = __spreadArray([], v);
                tmp.splice(v.findIndex(function (tag) { return tag.id == tagValue.value.id; }), 1);
                refTags.current = tmp;
                return tmp;
            }
            refTags.current = __spreadArray(__spreadArray([], v), [tagValue.value]);
            return __spreadArray(__spreadArray([], v), [tagValue.value]);
        });
        if (props.onSelect)
            props.onSelect(text, tagValue);
    }, []);
    return (react_1.default.createElement(exports.AutocompleteCoreWithoutUnform, __assign({}, props, { ref: ref, tags: tags, removeTag: removeTag, onSelect: onSelect, value: null, error: error, defaultValue: "" })));
});
exports.AutocompleteCoreWithoutUnform = react_1.default.forwardRef(function AutocompleteCoreWithoutUnform(props, inputRef) {
    var as = props.as, rest = __rest(props, ["as"]);
    var ref = react_1.useRef(null);
    react_1.useImperativeHandle(inputRef, function () { return ref.current; }, []);
    var PureComponent = react_1.useMemo(function () { return as; }, [as]);
    return react_1.default.createElement(PureComponent, __assign({}, rest, { ref: ref }));
});
//# sourceMappingURL=InputTagsCore.js.map