"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get Keys () {
        return Keys;
    },
    get useKeyNavigation () {
        return useKeyNavigation;
    }
});
var _react = require("react");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var Keys = {
    Enter: 'Enter',
    Tab: 'Tab',
    Escape: 'Escape',
    ArrowLeft: 'ArrowLeft',
    ArrowRight: 'ArrowRight',
    Backspace: 'Backspace'
};
var useKeyNavigation = function useKeyNavigation(param) {
    var _param_controlledRefs = param.controlledRefs, chipsRefs = _param_controlledRefs.chipsRefs, contentRef = _param_controlledRefs.contentRef, inputRef = _param_controlledRefs.inputRef, disabled = param.disabled, readOnly = param.readOnly, chips = param.chips, newCustomChips = param.newCustomChips, enumerationType = param.enumerationType, updateChips = param.updateChips, onChange = param.onChange, onSearch = param.onSearch, _param_onEnterDisabled = param.onEnterDisabled, onEnterDisabled = _param_onEnterDisabled === void 0 ? false : _param_onEnterDisabled, onChipCloseClick = param.onChipCloseClick, _interaction_disabled = param._interaction_disabled;
    var _useState = _sliced_to_array((0, _react.useState)(-1), 2), activeChipIndex = _useState[0], setActiveChipIndex = _useState[1];
    var handleInputKeydown = function handleInputKeydown(event) {
        var _inputRef_current, _inputRef_current1;
        var value = event.target.value;
        if (disabled || readOnly || _interaction_disabled) {
            return;
        }
        if (event.key === Keys.Enter && !onEnterDisabled) {
            if (enumerationType === 'chip' && value) {
                var nextIndex = (chips === null || chips === void 0 ? void 0 : chips.length) || 0;
                var newChips = _to_consumable_array(chips).concat([
                    {
                        id: "".concat(nextIndex, "_").concat(value),
                        text: value
                    }
                ]);
                var newValues = newChips.map(function(param) {
                    var text = param.text;
                    return text;
                });
                updateChips(newChips, newValues);
                event.target.value = '';
                onChange === null || onChange === void 0 ? void 0 : onChange(event);
            }
            if (onSearch) {
                onSearch(value, event);
            }
        }
        if (event.key === Keys.Backspace && (newCustomChips === null || newCustomChips === void 0 ? void 0 : newCustomChips.length) && (inputRef === null || inputRef === void 0 ? void 0 : (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : _inputRef_current.selectionStart) === 0) {
            var lastChip = newCustomChips.at(-1);
            if (onChipCloseClick && lastChip) {
                onChipCloseClick(lastChip);
                chipsRefs === null || chipsRefs === void 0 ? void 0 : chipsRefs.current.pop();
            }
        }
        if (event.key === Keys.Backspace && chips.length && (inputRef === null || inputRef === void 0 ? void 0 : (_inputRef_current1 = inputRef.current) === null || _inputRef_current1 === void 0 ? void 0 : _inputRef_current1.selectionStart) === 0) {
            var newChips1 = _to_consumable_array(chips);
            newChips1 === null || newChips1 === void 0 ? void 0 : newChips1.pop();
            chipsRefs === null || chipsRefs === void 0 ? void 0 : chipsRefs.current.pop();
            var newValues1 = newChips1.map(function(param) {
                var text = param.text;
                return text;
            });
            updateChips(newChips1, newValues1);
        }
    };
    var onChipClear = function onChipClear(clearId, index) {
        if (!chips.length && !(newCustomChips === null || newCustomChips === void 0 ? void 0 : newCustomChips.length)) {
            return;
        }
        var newChips = (chips === null || chips === void 0 ? void 0 : chips.filter(function(param) {
            var chipId = param.id;
            return chipId !== clearId;
        })) || [];
        var newValues = newChips.map(function(param) {
            var text = param.text;
            return text;
        });
        chipsRefs === null || chipsRefs === void 0 ? void 0 : chipsRefs.current.splice(index, 1);
        updateChips(newChips, newValues);
    };
    var handleChipKeyDown = function handleChipKeyDown(event, chipId, chipIndex) {
        if (event.key === Keys.Tab) {
            event.preventDefault();
        }
        if (event.key === Keys.Backspace) {
            onChipClear(chipId, chipIndex);
        }
    };
    var handleContentKeyDown = function handleContentKeyDown(event) {
        var _inputRef_current;
        var chipsRefsCount = chipsRefs.current.length;
        var currentCaretPosition = (inputRef === null || inputRef === void 0 ? void 0 : (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : _inputRef_current.selectionStart) || 0;
        var newActiveChipIndex = activeChipIndex;
        var focusedItem = null;
        if (event.key === Keys.ArrowLeft && enumerationType === 'chip' && currentCaretPosition <= 0) {
            var _chipsRefs_current;
            var index = activeChipIndex - 1 <= 0 ? 0 : activeChipIndex - 1;
            newActiveChipIndex = document.activeElement === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) ? chipsRefsCount - 1 : index;
            focusedItem = (_chipsRefs_current = chipsRefs.current) === null || _chipsRefs_current === void 0 ? void 0 : _chipsRefs_current[newActiveChipIndex];
        }
        if (event.key === Keys.ArrowRight && enumerationType === 'chip' && document.activeElement !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current)) {
            var _chipsRefs_current1;
            var index1 = activeChipIndex + 1 >= chipsRefsCount ? activeChipIndex : activeChipIndex + 1;
            newActiveChipIndex = document.activeElement === (contentRef === null || contentRef === void 0 ? void 0 : contentRef.current) ? 0 : index1;
            focusedItem = index1 === activeChipIndex ? inputRef === null || inputRef === void 0 ? void 0 : inputRef.current : (_chipsRefs_current1 = chipsRefs.current) === null || _chipsRefs_current1 === void 0 ? void 0 : _chipsRefs_current1[newActiveChipIndex];
        }
        if (event.key === Keys.Backspace && document.activeElement !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current)) {
            var _chipsRefs_current2;
            var index2 = activeChipIndex - 1 <= 0 ? 0 : activeChipIndex - 1;
            newActiveChipIndex = chipsRefsCount === 0 ? -1 : index2;
            focusedItem = chipsRefsCount === 0 ? inputRef === null || inputRef === void 0 ? void 0 : inputRef.current : chipsRefs === null || chipsRefs === void 0 ? void 0 : (_chipsRefs_current2 = chipsRefs.current) === null || _chipsRefs_current2 === void 0 ? void 0 : _chipsRefs_current2[newActiveChipIndex];
        }
        focusedItem === null || focusedItem === void 0 ? void 0 : focusedItem.focus();
        focusedItem === null || focusedItem === void 0 ? void 0 : focusedItem.scrollIntoView({
            behavior: 'smooth',
            block: 'center',
            inline: 'center'
        });
        setActiveChipIndex(newActiveChipIndex);
    };
    return {
        handleInputKeydown: handleInputKeydown,
        handleChipKeyDown: handleChipKeyDown,
        onChipClear: onChipClear,
        handleContentKeyDown: handleContentKeyDown
    };
};
