(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "@greenatom/icons", "react-select", "../utils/getValueObject", "../Avatar", "../SelectBase", "./components/SelectDropdownIndicator", "./components/SelectMenu", "./components/SelectOption"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var icons_1 = require("@greenatom/icons");
    var react_select_1 = (0, tslib_1.__importDefault)(require("react-select"));
    var getValueObject_1 = (0, tslib_1.__importDefault)(require("../utils/getValueObject"));
    var Avatar_1 = require("../Avatar");
    var SelectBase_1 = require("../SelectBase");
    var SelectDropdownIndicator_1 = (0, tslib_1.__importDefault)(require("./components/SelectDropdownIndicator"));
    var SelectMenu_1 = (0, tslib_1.__importDefault)(require("./components/SelectMenu"));
    var SelectOption_1 = (0, tslib_1.__importDefault)(require("./components/SelectOption"));
    var SelectComponent = function (props) {
        var _a = props.placeholder, placeholder = _a === void 0 ? "" : _a, isDisabled = props.isDisabled, options = props.options, menuIsOpen = props.menuIsOpen, onChange = props.onChange, defaultValue = props.defaultValue, _b = props.isClearable, isClearable = _b === void 0 ? true : _b, name = props.name, value = props.value, onInputChange = props.onInputChange, isSearchable = props.isSearchable, className = props.className, errorMessage = props.errorMessage, _c = props.isError, isError = _c === void 0 ? false : _c, width = props.width, isRequired = props.isRequired, _d = props.size, size = _d === void 0 ? "l" : _d, onInnerMenuOpen = props.onInnerMenuOpen, status = props.status, statusText = props.statusText, iconLeft = props.iconLeft, _e = props.fieldNames, fieldNames = _e === void 0 ? { value: "value", label: "label" } : _e, _f = props.onChangeReturnType, onChangeReturnType = _f === void 0 ? "instance" : _f, onBlur = props.onBlur, onFocus = props.onFocus, _g = props.type, type = _g === void 0 ? "basic" : _g, isDrawer = props.isDrawer, viewType = props.viewType, isReadOnly = props.isReadOnly, otherProps = (0, tslib_1.__rest)(props, ["placeholder", "isDisabled", "options", "menuIsOpen", "onChange", "defaultValue", "isClearable", "name", "value", "onInputChange", "isSearchable", "className", "errorMessage", "isError", "width", "isRequired", "size", "onInnerMenuOpen", "status", "statusText", "iconLeft", "fieldNames", "onChangeReturnType", "onBlur", "onFocus", "type", "isDrawer", "viewType", "isReadOnly"]);
        var _h = (0, tslib_1.__read)((0, react_1.useState)(undefined), 2), avatarUrl = _h[0], setAvatarUrl = _h[1];
        var localValue = (0, react_1.useMemo)(function () {
            if (options && typeof value !== "object") {
                return options.find(function (option) { return (0, getValueObject_1.default)(option, fieldNames.value) === value; });
            }
            return value;
        }, [options, fieldNames, value, isReadOnly]);
        var iconLeftSelect = (0, react_1.useMemo)(function () {
            if (type === "avatar") {
                return (react_1.default.createElement(Avatar_1.Avatar, { size: "s", as: "div", icon: react_1.default.createElement(icons_1.IconBrokenUser, null), image: avatarUrl, isDisabled: isDisabled }));
            }
            return iconLeft;
        }, [type, iconLeft, avatarUrl, isDisabled]);
        // У react-select есть лимит на получение количества пропсов, чтобы его обойти использую left props
        var leftProps = (0, tslib_1.__assign)({ errorMessage: errorMessage, isError: isError, width: width, isRequired: isRequired, size: size, onInnerMenuOpen: onInnerMenuOpen, status: status, statusText: statusText, onFocus: onFocus, onBlur: onBlur, iconLeft: iconLeftSelect, type: type, isDrawer: isDrawer, viewType: viewType, isReadOnly: isReadOnly }, otherProps);
        var handleChange = function (newValue, actionMeta) {
            var _a, _b, _c;
            if (onChangeReturnType === "instance") {
                (_a = onChange) === null || _a === void 0 ? void 0 : _a(newValue, actionMeta);
            }
            else if (newValue) {
                (_b = onChange) === null || _b === void 0 ? void 0 : _b((0, getValueObject_1.default)(newValue, fieldNames.value, null), actionMeta);
            }
            if (type === "avatar") {
                setAvatarUrl((_c = newValue) === null || _c === void 0 ? void 0 : _c.url);
            }
        };
        // Есть проблемы с TS в самом реакт селекте и не получается всё типизировать
        return (react_1.default.createElement(react_select_1.default, (0, tslib_1.__assign)({ components: {
                SelectContainer: SelectBase_1.SelectContainer,
                Menu: SelectMenu_1.default,
                IndicatorsContainer: SelectBase_1.SelectIndicators,
                DropdownIndicator: SelectDropdownIndicator_1.default,
                ClearIndicator: SelectBase_1.SelectClearIndicator,
                Control: SelectBase_1.SelectControl,
                Placeholder: SelectBase_1.SelectPlaceholder,
                ValueContainer: SelectBase_1.SelectValueContainer,
                SingleValue: SelectBase_1.SelectSingleValue,
                Option: SelectOption_1.default
            }, name: name, maxMenuHeight: 328, value: localValue, defaultValue: defaultValue, getOptionValue: function (option) {
                return (0, getValueObject_1.default)(option, fieldNames.value);
            }, getOptionLabel: function (option) {
                return (0, getValueObject_1.default)(option, fieldNames.label);
            }, menuIsOpen: menuIsOpen, menuPlacement: "auto", isMulti: false, isSearchable: isSearchable, options: options, isDisabled: isDisabled || isReadOnly, placeholder: placeholder, onChange: handleChange, onInputChange: onInputChange, isClearable: isReadOnly ? false : isClearable, className: className, classNamePrefix: "react-select", captureMenuScroll: false }, leftProps)));
    };
    exports.default = SelectComponent;
});
