import { UnreachableCaseError } from '../../types';

import { CustomerTypeName } from './Customer';
import { InventoryLevelTypeName } from './InventoryLevel';
import { LineItemTypeName } from './LineItem';
import { LocationTypeName } from './Location';
import { OrderTypeName } from './Order';
import { ProductTypeName } from './Product';
import { ProductVariantTypeName } from './ProductVariant';

import { ShopifyObjectType } from '.';

export function isProductType(sot: any): sot is ProductTypeName {
  if (sot !== ProductVariantTypeName) {
    throw `Not ${ProductTypeName}`;
  }

  return sot;
}

export function getRestId(graphqlId:string):number {
  const idParts = graphqlId.split('/');

  return parseInt(idParts[idParts.length - 1]);
}

export function getGraphqlId(restId: number, shopifyObjectType: ShopifyObjectType):string {
  const gidPrefix = 'gid://shopify/';

  switch(shopifyObjectType) {
    case OrderTypeName: {
      return `${gidPrefix}Order/${restId}`;
    }
    case ProductTypeName: {
      return `${gidPrefix}Product/${restId}`;
    }
    case ProductVariantTypeName: {
      return `${gidPrefix}ProductVariant/${restId}`;
    }
    case LocationTypeName: {
      return `${gidPrefix}Location/${restId}`;
    }
    case InventoryLevelTypeName: {
      return `${gidPrefix}InventoryLevel/${restId}`;
    }
    case LineItemTypeName: {
      return `${gidPrefix}LineItem/${restId}`;
    }

    case CustomerTypeName: {
      return `${gidPrefix}Customer/${restId}`;
    }

    default: {
      throw new UnreachableCaseError(shopifyObjectType);
    }
  }
}