import { createStorefrontApiClient } from '@shopify/storefront-api-client';
import { REACT_APP_ACCESS_TOKEN, REACT_APP_SHOP_NAME } from '../utils/httpCommon';

const client = createStorefrontApiClient({
  storeDomain: `https://${REACT_APP_SHOP_NAME}/api/2024-10/graphql.json`,
  apiVersion: '2024-10',
  publicAccessToken: REACT_APP_ACCESS_TOKEN,
});

// Create a cart
const createCustomer = async (input) => {
  const query = `
    mutation CreateCustomer($input: CustomerCreateInput!) {
      customerCreate(input: $input) {
        customer {
          id
          email
          firstName
          lastName
        }
        userErrors {
          field
          message
        }
      }
    }
  `;

  const {customerCreate} = await client.request(query,{
    variables: {
      input,
    }
  });

  if(!customerCreate) return 0
  if(customerCreate?.userErrors?.length == 0) return 1

};

// Export services
const customerService = {
  createCustomer
};

export default customerService;
