import React from 'react';
import PropTypes from 'prop-types';
import { ApolloClient, HttpLink, InMemoryCache, ApolloProvider } from '@apollo/client';

// eslint-disable-next-line no-unused-vars
const withGraphQL = (WrappedComponent, endpoint) => {
    const GraphQLClient = props => {
        // eslint-disable-next-line no-unused-vars
        const { data: { adTenant, baseUrl = '' } } = props;
        const httpLink = new HttpLink({
            uri: `${baseUrl}/${endpoint}`,
            headers: {
                'AD-Tenant': adTenant
            }
        });

        const client = new ApolloClient({
            cache: new InMemoryCache(),
            link: httpLink
        });

        return (
            <ApolloProvider client={client}>
                <WrappedComponent
                    {...props}
                />
            </ApolloProvider>
        );
    };

    GraphQLClient.propTypes = {
        data: PropTypes.shape({
            adTenant: PropTypes.string.isRequired,
            baseUrl: PropTypes.string.isRequired,
            productId: PropTypes.string.isRequired
        }).isRequired
    };

    GraphQLClient.schema = WrappedComponent.schema;
    return GraphQLClient;
};

export default withGraphQL;
