import * as React from 'react';
import { inject } from 'mobx-react';
import gql from 'graphql-tag';

import { IMeQueryProps, MeQuery } from './types';
import { userFragment } from '../fragments/user';

export const ME = gql`
  query me($accountInput: AccountInput) {
    me(input: $accountInput) {
      ...UserFragment
      userProfile {
        ssn
        email
        name
        postalCode
        streetAddress
        city
        isCompany
      }
      subscriptionLevel
    }
  }
  ${userFragment}
`;

export const ME_ADDRESS = gql`
  query meAddress($accountInput: AccountInput) {
    me(input: $accountInput) {
      ...UserFragment
      userProfile {
        postalCode
        streetAddress
        city
        name
      }
    }
  }
`;

@inject('authentication')
export default class Query extends React.Component<IMeQueryProps> {
  render() {
    const {
      authentication: { accountInput },
      variables,
      children,
      ...rest
    } = this.props;

    return (
      <MeQuery query={ME} variables={{ accountInput, ...variables }} {...rest}>
        {(result) => children(result)}
      </MeQuery>
    );
  }
}
