import React from "react";
import { isMultiple } from "../../../util/multiple";
import { LimeElementsWidgetAdapter } from "../adapters";
export class Select extends React.Component {
    constructor(props) {
        super(props);
        this.props = props;
        this.state = {
            modified: false,
        };
        this.handleChange = this.handleChange.bind(this);
    }
    render() {
        var _a, _b;
        const props = this.props;
        const enumOptions = props.options.enumOptions;
        const options = enumOptions.map(createOption);
        let value;
        if (props.multiple) {
            value = findValues(props.value, options);
        }
        else {
            value = findValue(props.value, options);
        }
        const additionalProps = ((_b = (_a = props.schema.lime) === null || _a === void 0 ? void 0 : _a.component) === null || _b === void 0 ? void 0 : _b.props) || {};
        return React.createElement(LimeElementsWidgetAdapter, {
            name: 'limel-select',
            value: value,
            events: {
                change: this.handleChange,
            },
            widgetProps: props,
            extraProps: Object.assign({ multiple: props.multiple, options: options }, additionalProps),
        });
    }
    handleChange(event) {
        const props = this.props;
        event.stopPropagation();
        if (!props.onChange) {
            return;
        }
        if (isMultiple(event.detail)) {
            const value = event.detail.map((option) => option.value);
            props.onChange(value);
            return;
        }
        props.onChange(event.detail.value);
    }
}
function createOption(item) {
    return {
        text: item.label,
        value: item.value,
        disabled: !!item.schema.readOnly,
    };
}
function findValue(value, options) {
    return options.find((option) => option.value === value);
}
function findValues(value, options) {
    return options.filter((option) => value.includes(option.value));
}
