/** The local web store cart is saved with the profile so checkout can clear it with the purchase. */ import { ensureBlock, findBlock, type BlkBlock } from '@wto/blk'; const STORE_CART = 'wtoStoreCart'; export function storeCartIds(profile: BlkBlock): readonly string[] { return findBlock(profile, STORE_CART)?.params.flatMap((param) => param.name === 'productId' && param.value.type === 'string' ? [param.value.value] : [], ) ?? []; } export function setStoreCart(profile: BlkBlock, productIds: readonly string[]): void { if (productIds.length === 0) { clearStoreCart(profile); return; } ensureBlock(profile, STORE_CART).params = productIds.map((id) => ({ name: 'productId', value: { type: 'string', value: id }, })); } export function clearStoreCart(profile: BlkBlock): void { const index = profile.blocks.findIndex((block) => block.name === STORE_CART); if (index !== -1) profile.blocks.splice(index, 1); }