import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import FormControl from '@mui/material/FormControl';
import InputLabel from '@mui/material/InputLabel';
import ListItemIcon from '@mui/material/ListItemIcon';
import { forwardRef } from 'react';
import { CrossOutlineMd } from '../../icons/CrossOutlineMd';
import { DownOutlineMd } from '../../icons/DownOutlineMd';
import { UpOutlineMd } from '../../icons/UpOutlineMd';
import { Checkbox } from '../Checkbox';
import { FormHelperText } from '../FormHelperText';
import { forwardRefWithGeneric } from '../forwardRefWithGeneric';
import { IconButton } from '../IconButton';
import { Loader } from '../Loader';
import { MenuItem } from '../MenuItem';
import { TagsList } from '../TagsList';
import { CLEAR_TEXT, SELECT_ALL_VALUE } from './constants';
import { EndAdornmentWrapper, Placeholder, StateWrapper, StyledSelect, } from './styles';
import { useLogic } from './useLogic';
const ClearButton = forwardRef(({ onClick, disabled }, ref) => (_jsx(IconButton, { disabled: disabled, ref: ref, onClick: onClick, variant: "text", title: CLEAR_TEXT, "aria-label": CLEAR_TEXT, children: _jsx(CrossOutlineMd, {}) })));
const SelectInner = ({ required, getOptionLabel, placeholder, helperText, hideHelperText = false, loading, success, children, disabled, error, label, fullWidth, onChange: externalOnChange, isShowClearButton, isEnableAllSelection = false, valueOptions, value, onSelectAll, onUnselectAll, ...props }, ref) => {
    const { isOpened, isShowingClearButton, isNoData, onClearAll, openSelect, closeSelect, resetButtonRef, isShowingSelectAll, handleChangeOption, isSelectedAll, isIndeterminate, handleChangeArrayValue, selectAllClassName, findMenuItemsLabel, isShowingPlaceholder, } = useLogic({
        ...props,
        children,
        value,
        onChange: externalOnChange,
        isShowClearButton,
        isEnableAllSelection,
        valueOptions,
        placeholder,
        onSelectAll,
        onUnselectAll,
        loading,
    });
    const renderValue = (selectedOptions) => {
        // Массив с выбранными опциями
        if (Array.isArray(selectedOptions) && selectedOptions.length) {
            return (_jsx(TagsList, { data: selectedOptions, keyId: "value", getOptionLabel: getOptionLabel || ((optionLabel) => optionLabel), onChange: handleChangeArrayValue, onClick: openSelect }));
        }
        // Пустой массив или пустая строка
        if ((Array.isArray(selectedOptions) || typeof selectedOptions === 'string') &&
            !selectedOptions.length) {
            return placeholder;
        }
        if (getOptionLabel) {
            // Строка
            return getOptionLabel(selectedOptions);
        }
        return findMenuItemsLabel(children, selectedOptions);
    };
    return (_jsxs(FormControl, { error: error, fullWidth: fullWidth, children: [label && (_jsx(InputLabel, { htmlFor: "grouped-select", required: required, disabled: disabled, children: label })), _jsxs(StyledSelect, { ...props, value: value === null ? '' : value, open: isOpened, disabled: disabled, onOpen: openSelect, onClose: closeSelect, renderValue: renderValue, IconComponent: () => null, displayEmpty: true, ref: ref, fullWidth: fullWidth, onChange: handleChangeOption, "$isShowingPlaceholder": isShowingPlaceholder, endAdornment: _jsxs(EndAdornmentWrapper, { children: [loading && _jsx(Loader, { color: "primary", size: "small" }), !loading && isShowingClearButton && (_jsx(ClearButton, { disabled: disabled, ref: resetButtonRef, onClick: onClearAll })), !loading && (_jsx(IconButton, { variant: "text", disabled: disabled, onClick: openSelect, children: !isOpened ? _jsx(DownOutlineMd, {}) : _jsx(UpOutlineMd, {}) }))] }), children: [_jsx(Placeholder, { value: "", children: placeholder }), !loading && isShowingSelectAll && (_jsxs(MenuItem, { className: selectAllClassName, selected: isSelectedAll, value: SELECT_ALL_VALUE, children: [_jsx(ListItemIcon, { children: _jsx(Checkbox, { checked: isSelectedAll, indeterminate: isIndeterminate }) }), "\u0412\u044B\u0431\u0440\u0430\u0442\u044C \u0432\u0441\u0435"] })), !loading && children, !loading && isNoData && _jsx(StateWrapper, { children: "\u041D\u0435\u0442 \u0434\u0430\u043D\u043D\u044B\u0445" })] }), _jsx(FormHelperText, { error: error, success: success, hideHelperText: hideHelperText, children: helperText })] }));
};
export const Select = forwardRefWithGeneric(SelectInner);
