"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 });
exports.Select = void 0;
const react_1 = __importStar(require("react"));
const react_select_1 = __importDefault(require("react-select"));
const styles_1 = require("../../styles");
const utils_1 = require("../../styles/utils");
const Option_1 = require("./Option");
const DropdownIndicator_1 = require("./DropdownIndicator");
const getStyles = (theme, variant) => {
    const inputsStyles = utils_1.inputs({ theme });
    return {
        primary: {
            container: (styles) => (Object.assign(Object.assign({}, styles), { minHeight: inputsStyles.height, minWidth: '7rem' })),
            control: (styles, state) => {
                return Object.assign(Object.assign(Object.assign({}, styles), { borderRadius: theme.radii[1], boxShadow: 'none', borderColor: theme.colors.placeholder }), (state.isFocused &&
                    {
                        borderColor: theme.colors.brandMain,
                        '&:hover': {
                            borderColor: theme.colors.brandMain,
                        },
                    }));
            },
            valueContainer: (styles) => (Object.assign(Object.assign({}, styles), { paddingLeft: '14px', fontSize: theme.fontSizes[1], minHeight: inputsStyles.height })),
            dropdownIndicator: (styles) => (Object.assign(Object.assign({}, styles), { paddingRight: '16px', color: theme.colors.placeholder })),
            menu: (styles) => (Object.assign(Object.assign({}, styles), { minWidth: '20rem' })),
            menuPortal: (styles) => (Object.assign(Object.assign({}, styles), { zIndex: 1000 })),
        },
        ghost: {
            container: (styles) => (Object.assign(Object.assign({}, styles), { minWidth: '2rem' })),
            control: (styles) => {
                return Object.assign(Object.assign({}, styles), { boxShadow: 'none', border: 'none', backgroundColor: '' });
            },
            valueContainer: (styles) => (Object.assign(Object.assign({}, styles), { paddingRight: 0, paddingLeft: 0, fontSize: theme.fontSizes[1] })),
            dropdownIndicator: (styles) => (Object.assign(Object.assign({}, styles), { paddingRight: 0, color: theme.colors.placeholder, '&:hover': {
                    color: theme.colors.brandMain,
                } })),
            menu: (styles) => (Object.assign(Object.assign({}, styles), { minWidth: '20rem' })),
            menuPortal: (styles) => (Object.assign(Object.assign({}, styles), { zIndex: 1000 })),
        },
    }[variant];
};
class SelectPrimitiveBase extends react_1.Component {
    constructor() {
        super(...arguments);
        this.handleChange = (value) => {
            const { onChange, onBlur } = this.props;
            if (onChange) {
                if (!value) {
                    onChange(value);
                }
                else {
                    const result = Array.isArray(value)
                        ? value.map((option) => option.value)
                        : value.value;
                    onChange(result);
                }
            }
            if (onBlur) {
                onBlur();
            }
        };
    }
    render() {
        const _a = this.props, { theme, variant, name, 
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        onChange, value, options, components, styles, inputRef, placeholder, noIcon } = _a, otherProps = __rest(_a, ["theme", "variant", "name", "onChange", "value", "options", "components", "styles", "inputRef", "placeholder", "noIcon"]);
        return (react_1.default.createElement(react_select_1.default, Object.assign({ inputId: name, ref: inputRef, inputRef: inputRef, innerRef: inputRef, name: name, styles: theme && variant
                ? Object.assign(Object.assign({}, getStyles(theme, variant)), styles) : {}, components: Object.assign({ IndicatorSeparator: null, ClearIndicator: null, DropdownIndicator: DropdownIndicator_1.DropdownIndicator, Option: (optionProps) => Option_1.Option(Object.assign(Object.assign({}, optionProps), { noIcon })) }, components), backspaceRemovesValue: false, onChange: this.handleChange, menuPlacement: "auto", menuPortalTarget: document.body, options: options, placeholder: placeholder, value: value === null || (value && Array.isArray(value))
                ? value
                : options === null || options === void 0 ? void 0 : options.find((option) => option.value === value) }, otherProps)));
    }
}
SelectPrimitiveBase.defaultProps = {
    isClearable: false,
    isSearchable: false,
    variant: 'primary',
    onBlur: () => { },
    placeholder: 'Select...',
};
const ThemedSelectPrimitive = styles_1.withTheme(SelectPrimitiveBase);
exports.Select = Object.assign(ThemedSelectPrimitive, {
    defaultProps: SelectPrimitiveBase.defaultProps,
    name: 'Select',
});
//# sourceMappingURL=Select.js.map