import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import useModal from '../vm-hooks/useModal';
import { getUserInfo } from './api';
import UserFJMModal from './userFJMModal';
import UserJMModal from './userJMModal';
import './style';
const Modal = ({ id, ...resetProps }) => {
    if (!resetProps.visible) {
        return null;
    }
    const [info, setInfo] = useState({});
    useEffect(() => {
        if (id) {
            getUserInfo(id).then(data => {
                setInfo(data);
            });
        }
    }, [id]);
    const props = {
        info,
        ...resetProps,
    };
    if (info?.type === 'FJM') {
        return <UserFJMModal {...props} />;
    } else if (info?.type === 'JM') {
        return <UserJMModal {...props} />;
    }
    return null;
};

const VtxGasUserModal = props => {
    const { label, id } = props;
    const { showModal, ...modalProps } = useModal();

    return (
        <>
            {label ? (
                <a type="link" onClick={showModal}>
                    {label}
                </a>
            ) : (
                <span>-</span>
            )}
            {modalProps.visible && <Modal id={id} {...modalProps} />}
        </>
    );
};

Modal.propTypes = {
    id: PropTypes.string.isRequired,
};

VtxGasUserModal.propTypes = {
    id: PropTypes.string.isRequired,
    label: PropTypes.string.isRequired,
};

VtxGasUserModal.Modal = Modal;

export default VtxGasUserModal;
