"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const components_1 = require("@bluebase/components");
const react_1 = tslib_1.__importStar(require("react"));
const FormControl_1 = tslib_1.__importDefault(require("@material-ui/core/FormControl"));
const FormHelperText_1 = tslib_1.__importDefault(require("@material-ui/core/FormHelperText"));
const InputLabel_1 = tslib_1.__importDefault(require("@material-ui/core/InputLabel"));
const Select_1 = tslib_1.__importDefault(require("@material-ui/core/Select"));
const react_native_1 = require("react-native");
const component_mapper_1 = require("@bluebase/component-mapper");
Select_1.default.displayName = 'Select';
FormControl_1.default.displayName = 'FormControl';
FormHelperText_1.default.displayName = 'FormHelperText';
InputLabel_1.default.displayName = 'InputLabel';
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);
        }
    },
    PickerItem: 'PickerItem',
    children: 'children',
    classes: 'classes',
    disabled: 'disabled',
    error: 'error',
    helperText: 'helperText',
    id: 'id',
    label: 'label',
    mode: 'mode',
    name: 'name',
    placeholder: 'placeholder',
    readOnly: 'readOnly',
    required: 'required',
    style: ({ style }) => react_native_1.StyleSheet.flatten(style),
    variant: 'variant',
};
exports.Picker = (props) => {
    const newProps = component_mapper_1.objectMapper(props, fieldMap);
    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"]);
    const items = react_1.default.Children.map(children, (child) => {
        if (!react_1.isValidElement(child)) {
            return null;
        }
        const childProps = child.props;
        return react_1.default.cloneElement(child, Object.assign(Object.assign({}, childProps), { children: childProps.children || childProps.label }));
    });
    return (react_1.default.createElement(FormControl_1.default, Object.assign({}, Object.assign({ disabled,
        error,
        required,
        variant }, rest)),
        react_1.default.createElement(InputLabel_1.default, { id: `${id}-label` }, label),
        react_1.default.createElement(Select_1.default, Object.assign({}, {
            displayEmpty: false,
            id,
            labelId: `${id}-label`,
            name,
            onChange,
            readOnly,
            value,
        }),
            placeholder ? react_1.default.createElement(PickerItem, { value: "", label: react_1.default.createElement("em", null, placeholder), disabled: true }) : null,
            items),
        helperText ? react_1.default.createElement(FormHelperText_1.default, null, helperText) : null));
};
exports.Picker.defaultProps = Object.assign(Object.assign({}, components_1.PickerDefaultProps), { PickerItem: components_1.PickerItem });
