"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 __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 });
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = __importDefault(require("react"));
var Container_1 = require("../../../components/Container");
var styles_1 = require("../../../utils/styles");
var Button_1 = __importDefault(require("./Button"));
var ButtonDropdown_1 = __importDefault(require("./ButtonDropdown"));
var Input_1 = __importDefault(require("./Input"));
exports.default = react_1.default.memo(function (_a) {
    var selectedOption = _a.selectedOption, _b = _a.options, options = _b === void 0 ? [] : _b, _c = _a.position, position = _c === void 0 ? 'left' : _c, onChangeOption = _a.onChangeOption, props = __rest(_a, ["selectedOption", "options", "position", "onChangeOption"]);
    var selectedOptionObject = react_1.default.useMemo(function () {
        return options.find(function (_a) {
            var value = _a.value;
            return !!selectedOption && value === selectedOption;
        });
    }, [selectedOption, options]);
    var _d = react_1.default.useState(''), inputValue = _d[0], setInputValue = _d[1];
    var displayedOptions = react_1.default.useMemo(function () {
        return options.filter(function (_a) {
            var text = _a.text;
            return text.toString().startsWith(inputValue);
        });
    }, [options, inputValue]);
    var reservatedDropdownRef = react_1.default.useRef(null);
    var dropdownRef = react_1.default.useRef(null);
    var scrollBodyRef = react_1.default.useRef(null);
    var _e = react_1.default.useState(null), opendOptionValue = _e[0], setOpendOptionValue = _e[1];
    var _f = react_1.default.useState(10), dropdownPositionTop = _f[0], setDropdownPositionTop = _f[1];
    var onScroll = react_1.default.useCallback(function () {
        if (opendOptionValue)
            setOpendOptionValue(null);
    }, [opendOptionValue]);
    react_1.default.useEffect(function () {
        var onClickOutside = function (event) {
            setOpendOptionValue(function (prev) {
                var _a, _b, _c, _d, _e, _f;
                if (reservatedDropdownRef.current) {
                    var value = reservatedDropdownRef.current[0];
                    var positionButton = (_b = (_a = reservatedDropdownRef.current[1]) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect) === null || _b === void 0 ? void 0 : _b.call(_a);
                    var positionBody = (_d = (_c = scrollBodyRef.current) === null || _c === void 0 ? void 0 : _c.getBoundingClientRect) === null || _d === void 0 ? void 0 : _d.call(_c);
                    if (positionButton && positionBody) {
                        setDropdownPositionTop(positionButton.top - positionBody.top + positionButton.height);
                    }
                    reservatedDropdownRef.current = null;
                    return value;
                }
                return prev && ((_f = (_e = dropdownRef.current) === null || _e === void 0 ? void 0 : _e.contains) === null || _f === void 0 ? void 0 : _f.call(_e, event.target))
                    ? prev
                    : null;
            });
        };
        document.addEventListener('click', onClickOutside);
        return function () { return document.removeEventListener('click', onClickOutside); };
    }, [setOpendOptionValue, dropdownRef, reservatedDropdownRef]);
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Container_1.Container, { "$position": "relative", children: (0, jsx_runtime_1.jsxs)(Container_1.Container, __assign({ ref: scrollBodyRef, "$width": "300px", "$position": "absolute", "$top": "100%", "$left": position === 'left' ? '0' : undefined, "$right": position === 'right' ? '0' : undefined, "$padding": "12px", "$borderRadius": "16px", "$background": styles_1.theme.color.extra.white, "$zIndex": styles_1.theme.zIndex.dropdown, "$css": styles_1.theme.effect.dropdown }, props, { children: [(0, jsx_runtime_1.jsx)(Input_1.default, { value: inputValue, onChangeValue: setInputValue, selectedOption: selectedOptionObject, "$marginBottom": "16px" }), (0, jsx_runtime_1.jsx)(Container_1.Container, { onScroll: onScroll, "$maxHeight": "300px", "$overflowX": "hidden", "$overflowY": "auto", "$display": "flex", "$flexDirection": "column", "$gap": "8px", children: displayedOptions.map(function (option) { return ((0, jsx_runtime_1.jsx)(Button_1.default, { value: option, isSelected: !!selectedOption && option.value === selectedOption, isOpenMore: (opendOptionValue === null || opendOptionValue === void 0 ? void 0 : opendOptionValue.value) === option.value, onClickMore: function (event) {
                                    reservatedDropdownRef.current = [option, event.target];
                                } }, option.value)); }) }), !!opendOptionValue && ((0, jsx_runtime_1.jsx)(ButtonDropdown_1.default, { ref: dropdownRef, value: opendOptionValue, onClose: function () { return setOpendOptionValue(null); }, "$top": "".concat(dropdownPositionTop, "px") }))] })) }), (0, jsx_runtime_1.jsx)(Container_1.Container, { "$height": "500px" })] }));
});
//# sourceMappingURL=index.js.map