function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _type_of(obj) {
    "@swc/helpers - typeof";
    return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
import React, { cloneElement, isValidElement, Children } from "react";
var isReactObject = function isReactObject(element) {
    return (typeof element === "undefined" ? "undefined" : _type_of(element)) === 'object' || typeof element === 'function';
};
export var getValidComponent = function getValidComponent(element, props) {
    if (/*#__PURE__*/ isValidElement(element)) {
        return /*#__PURE__*/ cloneElement(element, props);
    }
    if (isReactObject(element)) {
        var Component = element;
        return /*#__PURE__*/ React.createElement(Component, props);
    }
    return element;
};
export var getNewSelected = function getNewSelected(selected, newValue, type) {
    if (newValue === undefined || newValue === null) {
        return newValue;
    }
    if ((type === 'single' || type === 'native') && (typeof newValue === 'string' || typeof newValue === 'number' || typeof newValue === 'boolean')) {
        return newValue;
    }
    var newSelected = selected || [];
    var set = new Set(newSelected);
    if (set.has(newValue)) {
        set.delete(newValue);
    } else {
        set.add(newValue);
    }
    return Array.from(set);
};
var getChildrenInnerText = function getChildrenInnerText1(children) {
    if (!children || typeof children === 'string') {
        return children;
    }
    return getChildrenInnerText(children.props.children);
};
export var getValues = function getValues1(items, value) {
    var result = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : [];
    items.forEach(function(item) {
        if (item.props === undefined) {
            return result;
        }
        var _item_props = item.props, newValue = _item_props.value;
        if (item.props.children && Array.isArray(item.props.children)) {
            return getValues(item.props.children, value, result);
        }
        if (!Array.isArray(value) && newValue === value || Array.isArray(value) && (value === null || value === void 0 ? void 0 : value.find(function(v) {
            return newValue === v;
        }))) {
            var index = Array.isArray(value) && (value === null || value === void 0 ? void 0 : value.findIndex(function(v) {
                return newValue === v;
            })) || 0;
            if (item.props.text) {
                var _item_props1 = item.props, newText = _item_props1.text;
                result[index] = [
                    newValue,
                    newText
                ];
                return;
            }
            var newText1 = getChildrenInnerText(item.props.children);
            result[index] = [
                newValue,
                newText1
            ];
        }
        return result;
    });
    return result;
};
var getChildrenItemChecked = function getChildrenItemChecked(value, child) {
    return !Array.isArray(value) && (child === null || child === void 0 ? void 0 : child.props.value) === value || Array.isArray(value) && Boolean(value === null || value === void 0 ? void 0 : value.find(function(v) {
        return (child === null || child === void 0 ? void 0 : child.props.value) === v;
    }));
};
// INFO: Функция для рекурсивного обновления пропсов
export var updatePropsRecursively = function updatePropsRecursively1(children, externalProps, value, search, filterFunction) {
    return Children.map(children || [], function(child) {
        if (!/*#__PURE__*/ isValidElement(child)) {
            return child;
        }
        var hasValue = 'value' in (child === null || child === void 0 ? void 0 : child.props);
        var props = _object_spread({
            key: "".concat(value, "$"),
            checked: getChildrenItemChecked(value, child)
        }, hasValue && {
            ref: function ref(element) {
                var _externalProps_childrenRefs;
                if ((externalProps === null || externalProps === void 0 ? void 0 : (_externalProps_childrenRefs = externalProps.childrenRefs) === null || _externalProps_childrenRefs === void 0 ? void 0 : _externalProps_childrenRefs.current) && element) {
                    externalProps.childrenRefs.current.push(element);
                }
            },
            onClick: function onClick(event) {
                var _child_props_onClick, _child_props, _externalProps_onClick;
                (_child_props_onClick = (_child_props = child.props).onClick) === null || _child_props_onClick === void 0 ? void 0 : _child_props_onClick.call(_child_props, event);
                externalProps === null || externalProps === void 0 ? void 0 : (_externalProps_onClick = externalProps.onClick) === null || _externalProps_onClick === void 0 ? void 0 : _externalProps_onClick.call(externalProps, event);
            }
        });
        var updatedChild = updatePropsRecursively(child.props.children, externalProps, value, search, filterFunction);
        if (filterFunction) {
            var _child_props;
            var text = ((_child_props = child.props) === null || _child_props === void 0 ? void 0 : _child_props.text) || '';
            return filterFunction(text, search) ? /*#__PURE__*/ cloneElement(child, props, updatedChild) : null;
        }
        return /*#__PURE__*/ cloneElement(child, props, updatedChild);
    });
};
var defaultFilterFunction = function defaultFilterFunction(text) {
    var filterValue = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : '';
    return text.includes(filterValue);
};
export var getChildren = function getChildren(children, externalProps, value, search) {
    var filterFunction = arguments.length > 4 && arguments[4] !== void 0 ? arguments[4] : defaultFilterFunction;
    if (externalProps.childrenRefs) {
        externalProps.childrenRefs.current = [];
    }
    return updatePropsRecursively(children, externalProps, value, search, filterFunction);
};
