import {
  ProductVariantModelFragment
} from '../../types/ShopifyGraphQLAdminAPI';

import { ShopifyObjectModel } from './ShopifyObject';

export const OmittedProductVariantKeys = ['__typename', 'selectedOptions', 'inventoryItem', 'fulfillmentService'] as const;
export type OmittedProductVariantKey = typeof OmittedProductVariantKeys[number];

type RemainingProductVariantKey = Exclude<keyof ProductVariantModelFragment, OmittedProductVariantKey>;
const RemainingProductVariantKeysRecord : Record<RemainingProductVariantKey, undefined> = {
  'availableForSale': undefined,
  'barcode': undefined,
  'compareAtPrice': undefined,
  'createdAt': undefined,
  'displayName': undefined,
  'id': undefined,
  'inventoryPolicy': undefined,
  'inventoryQuantity': undefined,
  'legacyResourceId': undefined,
  'position': undefined,
  'price': undefined,
  'requiresShipping': undefined,
  'sku': undefined,
  'storefrontId': undefined,
  'taxCode': undefined,
  'taxable': undefined,
  'title': undefined,
  'updatedAt': undefined,
  'weight': undefined,
  'weightUnit': undefined,
  'deliveryProfile': undefined,
  'fulfillmentServiceEditable': undefined,
  'image': undefined,
  'product': undefined,
};
const RemainingProductVariantKeys = Object.keys(RemainingProductVariantKeysRecord) as RemainingProductVariantKey[];

const NewProductVariantKeys = [
  'selectedOptions.1',
  'selectedOptions.2',
  'selectedOptions.3',
  'fulfillmentService.id',
  'product.id',

  /**
   * InventoryItem props
   * We don't need to prefix all the InventoryItem properties with 'inventoryItem' for a couple reasons:
   * (1) Many of these properties are visible on the product variant page so merchants already associate them with product variant
   * (2) The origins from a code standpoint are clear.
   */
  'inventoryItem.id',
  'inventoryItem.createdAt',
  'inventoryItem.updatedAt',
  'inventoryItem.legacyResourceId',
  'duplicateSkuCount',
  'inventoryHistoryUrl',
  'locationsCount',
  'harmonizedSystemCode',
  'countryCodeOfOrigin',
  'provinceCodeOfOrigin',
  'tracked',
  'trackedEditable',
  'unitCost.amount',
  'unitCost.currencyCode',
] as const;
type NewProductVariantKey = typeof NewProductVariantKeys[number];

export type ProductVariantProps = Omit<ProductVariantModelFragment, OmittedProductVariantKey> & {
  'selectedOptions.1': ProductVariantModelFragment['selectedOptions'][0]['value'];
  'selectedOptions.2': ProductVariantModelFragment['selectedOptions'][1]['value'];
  'selectedOptions.3': ProductVariantModelFragment['selectedOptions'][2]['value'];
  'fulfillmentService.id': NonNullable<ProductVariantModelFragment['fulfillmentService']>['id'] | undefined;
  'product.id': ProductVariantModelFragment['product']['id'];
  'inventoryItem.id': ProductVariantModelFragment['inventoryItem']['id'];
  'inventoryItem.createdAt': ProductVariantModelFragment['inventoryItem']['createdAt'];
  'inventoryItem.updatedAt': ProductVariantModelFragment['inventoryItem']['updatedAt'];
  'inventoryItem.legacyResourceId': ProductVariantModelFragment['inventoryItem']['legacyResourceId'];
  'duplicateSkuCount': ProductVariantModelFragment['inventoryItem']['duplicateSkuCount'];
  'inventoryHistoryUrl': ProductVariantModelFragment['inventoryItem']['inventoryHistoryUrl'];
  'locationsCount': ProductVariantModelFragment['inventoryItem']['locationsCount'];
  'harmonizedSystemCode': ProductVariantModelFragment['inventoryItem']['harmonizedSystemCode'];
  'countryCodeOfOrigin': ProductVariantModelFragment['inventoryItem']['countryCodeOfOrigin'];
  'provinceCodeOfOrigin': ProductVariantModelFragment['inventoryItem']['provinceCodeOfOrigin'];
  'tracked': ProductVariantModelFragment['inventoryItem']['tracked'];
  'trackedEditable': ProductVariantModelFragment['inventoryItem']['trackedEditable'];
  'unitCost.amount': NonNullable<ProductVariantModelFragment['inventoryItem']['unitCost']>['amount'] | undefined;
  'unitCost.currencyCode': NonNullable<ProductVariantModelFragment['inventoryItem']['unitCost']>['currencyCode'] | undefined;
};

export const ProductVariantKeys = [
  ...RemainingProductVariantKeys,
  ...NewProductVariantKeys,
] as const;

export const ProductVariantTypeName = 'product_variants';
export type ProductVariantTypeName = typeof ProductVariantTypeName;

export interface ProductVariant extends ShopifyObjectModel {
  type: ProductVariantTypeName;
  props: ProductVariantProps;
}

export type ProductVariantKey = keyof ProductVariantProps;