import { makeStyles, Typography } from '@material-ui/core';
import PropTypes from 'prop-types';
import React, { useState } from 'react';
import { Form, FormButtons, SubmitButton } from '../../utilities/form';
import { useVirtualActions } from '../../hooks/virtualActions';
const useStyles = makeStyles((theme) => ({
    actions: {
        marginTop: theme.spacing(2),
        marginBottom: theme.spacing(2),
        display: 'flex',
        flexDirection: 'row'
    },
    title: {
        flexGrow: 1
    },
    subject: {
        marginLeft: theme.spacing(1),
        display: 'flex',
        flexDirection: 'row',
        flexGrow: 1
    },
    switchRoot: {
        marginLeft: theme.spacing(1)
    },
    input: {
        height: 34,
        padding: 0
    },
    formTextInput: {
        fontSize: 13
    },
    labelRoot: {
        fontSize: 13
    }
}));
export const VirtualForm = (props) => {
    const { tableDescription, rowValue, closeSideSheet, onCreate, onUpdate } = props;
    const { renderInputByFieldType } = useVirtualActions();
    const [virtualTableFormData, setVirtualTableFormData] = useState(rowValue || {});
    const classes = useStyles();
    const handleSubmit = async (values) => {
        let newTableData = Object.assign(Object.assign({}, virtualTableFormData), values);
        const isNewRow = !rowValue;
        if (isNewRow) {
            onCreate && (await onCreate(newTableData));
        }
        else {
            onUpdate && (await onUpdate(newTableData));
        }
        closeSideSheet && closeSideSheet();
    };
    return (React.createElement(Form, { onSubmit: handleSubmit }, React.createElement("div", null, tableDescription === null || tableDescription === void 0 ? void 0 : tableDescription.map((formField, index) => {
        return (React.createElement(React.Fragment, { key: `${formField.name}-${index}` }, React.createElement(Typography, { variant: "subtitle2", style: { marginTop: 8 } }, formField.friendlyName), renderInputByFieldType(formField, index, virtualTableFormData, classes)));
    })), React.createElement(FormButtons, null, React.createElement(SubmitButton, { variant: "contained", color: "secondary", disablePristine: false, disabled: false, style: { marginTop: 15 } }, "Sav"))));
};
VirtualForm.propTypes = {
    /**
     * onClick event for closing sidesheet
     */
    closeSideSheet: PropTypes.func,
    /**
     * onClick event for creating
     */
    onCreate: PropTypes.func,
    /**
     * onClick event for updating
     */
    onUpdate: PropTypes.func
};
//# sourceMappingURL=VirtualForm.js.map