import { goodsDescriptionOwner, ProductNeedingCustomsInfo, } from './customsRules' export declare interface EnteredCustomsInformation { hsCodes: Record skus: Record goodsDescriptions: Record hsCodeForAll?: string } export declare interface CustomsInformationToSave { hsCodes: Array<{ productId: string; hsCode: string }> skus: Array<{ productId: string; sku: string }> goodsDescriptions: Array<{ productId: string; description: string }> } const entered = (value?: string): Array => value?.trim() ? [value.trim()] : [] export const customsInformationToSave = ( products: Array, { hsCodes, skus, goodsDescriptions, hsCodeForAll }: EnteredCustomsInformation ): CustomsInformationToSave => ({ hsCodes: products .filter(product => product.missingHsCode) .flatMap(({ item }) => entered(hsCodeForAll ?? hsCodes[item.product_id]).map(hsCode => ({ productId: item.product_id, hsCode, })) ), skus: products .filter(product => product.missingSku) .flatMap(({ item }) => entered(skus[item.product_id]).map(sku => ({ productId: item.product_id, sku, })) ), goodsDescriptions: products .filter(product => product.missingGoodsDescription) .map(({ item }) => goodsDescriptionOwner(item)) .flatMap(productId => entered(goodsDescriptions[productId]).map(description => ({ productId, description, })) ), })