var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useEffect } from 'react';
import Typography from '@mui/material/Typography';
import { DropDownEditor } from '../DropDownEditor';
import { useStyles } from './styles';
export var SelectorWithOnlyOptionAutoSelect = function (_a) {
    var value = _a.value, onChange = _a.onChange, options = _a.options, _b = _a.classes, classes = _b === void 0 ? {} : _b, emptyLabel = _a.emptyLabel;
    var styles = useStyles();
    var firstOption = options[0], moreOptions = options.slice(1);
    var hasOnlyOneOptionToSelect = moreOptions.length === 0;
    useEffect(function () {
        if (hasOnlyOneOptionToSelect && value !== firstOption.value) {
            onChange(firstOption.value);
        }
    }, [firstOption.value]); // eslint-disable-line
    var displayEmpty = !!emptyLabel;
    return (React.createElement("div", { "data-reltio-id": "selector-with-only-option-auto-select", className: classes.root }, hasOnlyOneOptionToSelect ? (React.createElement(Typography, { className: classes.onlyValue || styles.onlyValue, variant: 'h6' }, firstOption.label)) : (React.createElement(DropDownEditor, { displayEmpty: displayEmpty, disableUnderline: true, classes: {
            select: classes.select || styles.select
        }, TextFieldProps: {
            classes: {
                root: classes.textField || styles.textField
            }
        }, MenuProps: {
            classes: {
                list: classes.selectMenu || styles.selectMenu
            }
        }, value: value || '', entries: displayEmpty ? __spreadArray([{ value: '', label: emptyLabel }], options, true) : options, onChange: function (value) { return onChange(value || null); } }))));
};
