import { Divider, makeStyles } from '@material-ui/core';
import React, { useCallback, useState } from 'react';
import { UserFormFields } from './UserFormFields';
import { useUser } from '../../../../utilities/auth';
import { Form, FormButtons, SubmitButton } from '../../../../utilities/form';
const useStyles = makeStyles(theme => ({
    divider: {
        marginTop: theme.spacing(2),
        marginBottom: theme.spacing(2),
    },
    loadingBar: {
        bottom: theme.spacing(-2),
        marginLeft: theme.spacing(-2),
        marginRight: theme.spacing(-2),
    },
}));
export const UserForm = props => {
    const { editedUser, rolesData, showRolesField = true, submitUser, updateCreatedUser, formLoading, altFieldMapping, } = props;
    const classes = useStyles();
    const user = useUser();
    const [passwordType, setPasswordType] = useState('autoGeneratePassword');
    const [creationMethod, setCreationMethod] = useState('standard'); // 'standard' | 'invite'
    const handlePasswordTypeChange = useCallback(event => {
        setPasswordType(event.target.value);
    }, []);
    const handleCreationMethodChange = useCallback(event => {
        setCreationMethod(event.target.value);
    }, []);
    const handleSubmitUser = useCallback(values => {
        if (submitUser) {
            if (passwordType === 'autoGeneratePassword') {
                values.passwordProfile = null;
            }
            // eslint-disable-next-line @typescript-eslint/no-unused-expressions
            updateCreatedUser && editedUser
                ? updateCreatedUser(values)
                : submitUser(values);
        }
    }, [submitUser, passwordType, updateCreatedUser, editedUser]);
    return (React.createElement(Form, { onSubmit: handleSubmitUser }, React.createElement(UserFormFields, { rolesData: rolesData, editedUser: editedUser, showRolesField: showRolesField, handlePasswordTypeChange: handlePasswordTypeChange, passwordType: passwordType, handleCreationMethodChange: handleCreationMethodChange, creationMethod: creationMethod, altFieldMapping: altFieldMapping }), user.can.CAN_MANAGE_USERS && (React.createElement(React.Fragment, null, React.createElement("div", { className: classes.divider }, React.createElement(Divider, null)), React.createElement(FormButtons, null, React.createElement(SubmitButton, { variant: "contained", color: "primary", disablePristine: false, disabled: formLoading }, editedUser ? 'Update' : 'Create'))))));
};
//# sourceMappingURL=UserForm.js.map