"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.prototype.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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Select = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_select_1 = __importStar(require("react-select"));
const styled_components_1 = require("styled-components");
const theme_1 = require("../../theme");
const Icon_1 = require("../Icon");
const Flex_1 = require("../Flex");
const Text_1 = require("../Text");
const Tooltip_1 = require("../Tooltip");
const DropdownIndicator = (props) => {
    return (react_select_1.components.DropdownIndicator && ((0, jsx_runtime_1.jsx)(react_select_1.components.DropdownIndicator, Object.assign({}, props, { children: (0, jsx_runtime_1.jsx)(Icon_1.Icon, { variant: "basic", icon: theme_1.polyIcons.ChevronDown, size: "24px", color: "gray3" }, void 0) }), void 0)));
};
const Option = (_a) => {
    var { children } = _a, props = __rest(_a, ["children"]);
    return ((0, jsx_runtime_1.jsxs)(react_select_1.components.Option, Object.assign({}, props, { isDisabled: true }, { children: [!props.selectProps.noIcon && ((0, jsx_runtime_1.jsx)(Icon_1.Icon, { variant: "basic", icon: theme_1.polyIcons.Image, size: "22px", color: "gray3", margin: "0 s -5px l" }, void 0)), children] }), void 0));
};
const Select = (_a) => {
    var { margin, id, label, tooltip, inputRef, onChange, noIcon } = _a, props = __rest(_a, ["margin", "id", "label", "tooltip", "inputRef", "onChange", "noIcon"]);
    const currentTheme = (0, react_1.useContext)(styled_components_1.ThemeContext);
    // @TODO: properly type this function
    const handleChange = (e) => {
        if (onChange) {
            if (!e) {
                onChange(null);
            }
            else {
                const result = Array.isArray(e)
                    ? e.map((option) => option.value)
                    : e.value;
                onChange(result);
            }
        }
    };
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Text_1.Text, Object.assign({ variant: "label", display: "block", margin: margin }, { children: label && tooltip && ((0, jsx_runtime_1.jsxs)(Flex_1.Flex, Object.assign({ variant: "raw", justify: tooltip ? 'spaced' : 'start' }, { children: [(0, jsx_runtime_1.jsx)(Text_1.Text, Object.assign({ variant: "span", format: "b2m" }, { children: label }), void 0), tooltip && (0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, { variant: "icon", content: tooltip }, void 0)] }), void 0)) }), void 0), (0, jsx_runtime_1.jsx)(react_select_1.default, Object.assign({ inputId: id, ref: inputRef, inputRef: inputRef, innerRef: inputRef, styles: currentTheme.SELECT, components: {
                    Option,
                    DropdownIndicator,
                    IndicatorSeparator: null,
                    ClearIndicator: null,
                }, onChange: handleChange, noIcon: noIcon, menuPlacement: "auto", menuPortalTarget: document.body, backspaceRemovesValue: false }, props), void 0)] }, void 0));
};
exports.Select = Select;
//# sourceMappingURL=Select.js.map