import {
  ProductTypeName,
  ProductVariantTypeName
} from '../../models/shopify';
import {
  CountryCode,
  ProductVariantInventoryPolicy,
  WeightUnit
} from '../../types/ShopifyGraphQLAdminAPI';
import * as AirtableFields from '../airtable/AirtableFieldDefaultConfigs';
import { ProductVariantAirtableFields as pvaf } from '../airtable/ShopifyObjectAirtableFields';

import {
  getShopifyObjectAirtableFields
} from './getCreateTableProps';
import { getRecordLinkAirtableField } from './getCreateTablePropsUtil';

const {
  id,
  barcode,
  url,
  singleLineText,
  datetime,
  checkbox,
  singleSelect,
  multipleRecordLinks,
  float,
  currency,
  json,
  integer,
} = AirtableFields;

const mapStringsToSingleSelectChoices = (strings:string[]) => strings.map(policy => ({ name: policy }));

export const getProductVariantsAirtableFields:getShopifyObjectAirtableFields<ProductVariantTypeName> = ({
  airtableTableLinks,
  baseSyncConfig,
}) => {
  const ProductVariantAirtableFields:pvaf = {
    type: ProductVariantTypeName,
    fields: {
      title: singleLineText,
      sku: singleLineText,
      product: getRecordLinkAirtableField({
        shopifyObjectType: ProductTypeName,
        airtableTableLinks,
        baseSyncConfig,
      }),
      id,
      createdAt: datetime,
      updatedAt: datetime,
      availableForSale: checkbox,
      price: currency,
      compareAtPrice: currency,
      'unitCost.amount': float,
      'unitCost.currencyCode': singleLineText,
      image: url,
      inventoryPolicy: {
        ...singleSelect,
        options: {
          choices: mapStringsToSingleSelectChoices(
            Object.values(ProductVariantInventoryPolicy)
          )
        }
      },
      tracked: checkbox,
      inventoryQuantity: integer,
      'selectedOptions.1': singleSelect,
      'selectedOptions.2': singleSelect,
      'selectedOptions.3': singleSelect,
      storefrontId: singleLineText,
      taxable: checkbox,
      'fulfillmentService.id': singleLineText,
      fulfillmentServiceEditable: json,
      weight: float,
      weightUnit: {
        ...singleSelect,
        options: {
          choices: Object.values(WeightUnit)
            .map(wu => ({ name: wu }))
        }
      },
      deliveryProfile: singleLineText,
      requiresShipping: checkbox,
      taxCode: singleLineText,
      barcode,
      displayName: singleLineText,
      legacyResourceId: singleLineText,
      position: integer,

      'inventoryItem.id': singleLineText,
      'inventoryItem.createdAt': datetime,
      'inventoryItem.updatedAt': datetime,
      inventoryHistoryUrl: url,
      'inventoryItem.legacyResourceId': singleLineText,
      locationsCount: integer,
      duplicateSkuCount: integer,
      harmonizedSystemCode: singleLineText,
      countryCodeOfOrigin: {
        ...singleSelect,
        options: {
          choices: mapStringsToSingleSelectChoices(
            Object.values(CountryCode)
          )
        }
      },
      provinceCodeOfOrigin: singleLineText,
      trackedEditable: json,
      'product.id': singleLineText,
    }
  } as const;

  return ProductVariantAirtableFields;
};
