"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const components_1 = require("@bluebase/components");
const FilledInput_1 = tslib_1.__importDefault(require("@material-ui/core/FilledInput"));
const FormControl_1 = tslib_1.__importDefault(require("@material-ui/core/FormControl"));
const FormHelperText_1 = tslib_1.__importDefault(require("@material-ui/core/FormHelperText"));
const Input_1 = tslib_1.__importDefault(require("@material-ui/core/Input"));
const InputLabel_1 = tslib_1.__importDefault(require("@material-ui/core/InputLabel"));
const OutlinedInput_1 = tslib_1.__importDefault(require("@material-ui/core/OutlinedInput"));
const react_1 = tslib_1.__importDefault(require("react"));
const Select_1 = tslib_1.__importDefault(require("@material-ui/core/Select"));
const component_mapper_1 = require("@bluebase/component-mapper");
const fieldMap = {
    displayEmpty: ({ placeholder }) => !!placeholder,
    native: ({ mode }) => mode !== 'default' ? false : true,
    value: 'selectedValue',
    onChange: ({ onChange, onValueChange }) => (event, value) => {
        if (onChange) {
            onChange(event, value);
        }
        if (onValueChange) {
            onValueChange(event.target.value, event.target.selectedIndex);
        }
    }
};
exports.Picker = (props) => {
    const newProps = component_mapper_1.objectMapper(props, fieldMap, {
        ignore: ['onValueChange', 'selectedValue'],
        rest: true,
    });
    const { children, disabled, displayEmpty, error, helperText, id, label, name, native, onChange, readOnly, value, placeholder, required, variant, PickerItem } = newProps, rest = tslib_1.__rest(newProps, ["children", "disabled", "displayEmpty", "error", "helperText", "id", "label", "name", "native", "onChange", "readOnly", "value", "placeholder", "required", "variant", "PickerItem"]);
    let InputComponent = Input_1.default;
    if (variant === 'filled') {
        InputComponent = FilledInput_1.default;
    }
    if (variant === 'outlined') {
        InputComponent = OutlinedInput_1.default;
    }
    const formControlProps = Object.assign({ disabled,
        error,
        required,
        variant }, rest);
    const selectProps = {
        displayEmpty,
        input: react_1.default.createElement(InputComponent, Object.assign({}, { id, name, })),
        // inputProps: { id, name, },
        native,
        onChange,
        readOnly,
        value,
    };
    return (react_1.default.createElement(FormControl_1.default, Object.assign({}, formControlProps),
        label ? react_1.default.createElement(InputLabel_1.default, { htmlFor: id }, label) : null,
        react_1.default.createElement(Select_1.default, Object.assign({}, selectProps),
            placeholder ? react_1.default.createElement(PickerItem, { value: "", label: placeholder, disabled: true }) : null,
            children),
        helperText ? react_1.default.createElement(FormHelperText_1.default, null, helperText) : null));
};
exports.Picker.defaultProps = Object.assign({}, components_1.PickerDefaultProps, { PickerItem: components_1.PickerItem });
