"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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 __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));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectorWithOnlyOptionAutoSelect = void 0;
var react_1 = __importStar(require("react"));
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var DropDownEditor_1 = require("../DropDownEditor");
var styles_1 = require("./styles");
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 = (0, styles_1.useStyles)();
    var firstOption = options[0], moreOptions = options.slice(1);
    var hasOnlyOneOptionToSelect = moreOptions.length === 0;
    (0, react_1.useEffect)(function () {
        if (hasOnlyOneOptionToSelect && value !== firstOption.value) {
            onChange(firstOption.value);
        }
    }, [firstOption.value]); // eslint-disable-line
    var displayEmpty = !!emptyLabel;
    return (react_1.default.createElement("div", { "data-reltio-id": "selector-with-only-option-auto-select", className: classes.root }, hasOnlyOneOptionToSelect ? (react_1.default.createElement(Typography_1.default, { className: classes.onlyValue || styles.onlyValue, variant: 'h6' }, firstOption.label)) : (react_1.default.createElement(DropDownEditor_1.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); } }))));
};
exports.SelectorWithOnlyOptionAutoSelect = SelectorWithOnlyOptionAutoSelect;
