"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Select = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const FormControl_1 = __importDefault(require("@mui/material/FormControl"));
const InputLabel_1 = __importDefault(require("@mui/material/InputLabel"));
const ListItemIcon_1 = __importDefault(require("@mui/material/ListItemIcon"));
const react_1 = require("react");
const CrossOutlineMd_1 = require("../../icons/CrossOutlineMd");
const DownOutlineMd_1 = require("../../icons/DownOutlineMd");
const UpOutlineMd_1 = require("../../icons/UpOutlineMd");
const Checkbox_1 = require("../Checkbox");
const FormHelperText_1 = require("../FormHelperText");
const forwardRefWithGeneric_1 = require("../forwardRefWithGeneric");
const IconButton_1 = require("../IconButton");
const Loader_1 = require("../Loader");
const MenuItem_1 = require("../MenuItem");
const TagsList_1 = require("../TagsList");
const constants_1 = require("./constants");
const styles_1 = require("./styles");
const useLogic_1 = require("./useLogic");
const ClearButton = (0, react_1.forwardRef)(({ onClick, disabled }, ref) => ((0, jsx_runtime_1.jsx)(IconButton_1.IconButton, { disabled: disabled, ref: ref, onClick: onClick, variant: "text", title: constants_1.CLEAR_TEXT, "aria-label": constants_1.CLEAR_TEXT, children: (0, jsx_runtime_1.jsx)(CrossOutlineMd_1.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, } = (0, useLogic_1.useLogic)({
        ...props,
        children,
        value,
        onChange: externalOnChange,
        isShowClearButton,
        isEnableAllSelection,
        valueOptions,
        placeholder,
        onSelectAll,
        onUnselectAll,
        loading,
    });
    const renderValue = (selectedOptions) => {
        // Массив с выбранными опциями
        if (Array.isArray(selectedOptions) && selectedOptions.length) {
            return ((0, jsx_runtime_1.jsx)(TagsList_1.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 ((0, jsx_runtime_1.jsxs)(FormControl_1.default, { error: error, fullWidth: fullWidth, children: [label && ((0, jsx_runtime_1.jsx)(InputLabel_1.default, { htmlFor: "grouped-select", required: required, disabled: disabled, children: label })), (0, jsx_runtime_1.jsxs)(styles_1.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: (0, jsx_runtime_1.jsxs)(styles_1.EndAdornmentWrapper, { children: [loading && (0, jsx_runtime_1.jsx)(Loader_1.Loader, { color: "primary", size: "small" }), !loading && isShowingClearButton && ((0, jsx_runtime_1.jsx)(ClearButton, { disabled: disabled, ref: resetButtonRef, onClick: onClearAll })), !loading && ((0, jsx_runtime_1.jsx)(IconButton_1.IconButton, { variant: "text", disabled: disabled, onClick: openSelect, children: !isOpened ? (0, jsx_runtime_1.jsx)(DownOutlineMd_1.DownOutlineMd, {}) : (0, jsx_runtime_1.jsx)(UpOutlineMd_1.UpOutlineMd, {}) }))] }), children: [(0, jsx_runtime_1.jsx)(styles_1.Placeholder, { value: "", children: placeholder }), !loading && isShowingSelectAll && ((0, jsx_runtime_1.jsxs)(MenuItem_1.MenuItem, { className: selectAllClassName, selected: isSelectedAll, value: constants_1.SELECT_ALL_VALUE, children: [(0, jsx_runtime_1.jsx)(ListItemIcon_1.default, { children: (0, jsx_runtime_1.jsx)(Checkbox_1.Checkbox, { checked: isSelectedAll, indeterminate: isIndeterminate }) }), "\u0412\u044B\u0431\u0440\u0430\u0442\u044C \u0432\u0441\u0435"] })), !loading && children, !loading && isNoData && (0, jsx_runtime_1.jsx)(styles_1.StateWrapper, { children: "\u041D\u0435\u0442 \u0434\u0430\u043D\u043D\u044B\u0445" })] }), (0, jsx_runtime_1.jsx)(FormHelperText_1.FormHelperText, { error: error, success: success, hideHelperText: hideHelperText, children: helperText })] }));
};
exports.Select = (0, forwardRefWithGeneric_1.forwardRefWithGeneric)(SelectInner);
