import { DataType, Shopify } from '@shopify/shopify-api';
import { StorefrontClient } from '@shopify/shopify-api/lib/clients/graphql/storefront_client';
import { ConfigShopifyMo } from './types';

export default class StorefrontApi {
    storefrontClient: StorefrontClient
    shopifyApi: Shopify

    constructor(shopifyApi: Shopify, config: ConfigShopifyMo) {
        this.shopifyApi = shopifyApi
        this.storefrontClient = new this.shopifyApi.clients.Storefront({
            domain: config.domain,
            storefrontAccessToken: config.storefrontAccessToken
        });
    }
    

    customerAccessTokenCreate = async (input: { email: string, password: string }) => {
        const data = await this.storefrontClient.query({
            data: `mutation customerAccessTokenCreate {
                    customerAccessTokenCreate(input: {email: "${input.email}", password: "${input.password}"}) {
                        customerAccessToken {
                            accessToken
                            expiresAt
                        }
                        customerUserErrors {
                            message
                        }
                    }
                }`,
        });
        return data
    }

    getCustomer = async (input: { customerAccessToken: any }) => {
        let token = input.customerAccessToken.body.data.customerAccessTokenCreate.customerAccessToken.accessToken
        const data = await this.storefrontClient.query({
            data: `query {
              customer(customerAccessToken: "${token}") {
                id
                firstName
                lastName
                acceptsMarketing
                email
                phone
              }
            }`,
        });
        return data
    }

    getProductAll = async () => {
        const products = await this.storefrontClient.query({
            data: `{
            products(first: 10) {
              edges {
                node {
                  id
                  createdAt
                  description
                  images(first: 10) {
                    edges {
                      node {
                        url
                        src
                        originalSrc
                        id
                        height
                        altText
                      }
                    }
                  }
                  title
                  updatedAt
                  productType
                  publishedAt
                  tags
                  totalInventory
                }
              }
            }
          }
          `,
        });

        return products
    }

    getProduct = async (product_id: string | number) => {
        const product = await this.storefrontClient.query({
            data: `{
                product(id: ${product_id}) {
                  createdAt
                  title
                  totalInventory
                  updatedAt
                }
              }`,
        });

        return product
    }

}