import { WishlistGetters as BaseWishlistGetters , AgnosticPrice , AgnosticTotals , AgnosticPagination , } from '@vue-storefront/core' ; import { Wishlist , WishlistQuery , } from '@vue-storefront/magento-api' ; export type WishlistProduct = WishlistQuery [ 'customer' ] [ 'wishlists' ] [ 0 ] [ 'items_v2' ] [ 'items' ] [ 0 ] & { variant : any ; } ; export const getItems = ( wishlist ) : WishlistProduct [ ] => wishlist . items_v2 . items ; export const getItemName = ( product ) : string => product ?. product ?. name || '' ; export const getItemImage = ( product ) : string => product ?. product ?. thumbnail . url || '' ; export const getItemPrice = ( product ) : AgnosticPrice => { let regular = 0 ; let special = null ; if ( product ?. product ?. price_range ) { regular = product ?. product ?. price_range . minimum_price . regular_price . value ; const final = product ?. product ?. price_range . minimum_price . final_price . value ; if ( final < regular ) { special = final ; } } return { regular , special , } ; } ; export const getItemQty = ( product ) : number => product . quantity ; export const getItemAttributes = ( product , filterByAttributeName ? ) => ( { '' : '' } ) ; export const getItemSku = ( product ) : string => product ?. product ?. sku || '' ; export const getTotals = ( wishlist ) : AgnosticTotals => { if ( Array . isArray ( wishlist ) ) { return wishlist [ 0 ] ?. items_v2 ?. items . reduce ( ( acc , curr ) => ( { total : acc . total + getItemPrice ( curr ) . special , subtotal : acc . subtotal + getItemPrice ( curr ) . regular , } ) , ( { total : 0 , subtotal : 0 } ) ) ; } return wishlist ?. items_v2 ?. items . reduce ( ( acc , curr ) => ( { total : acc . total + getItemPrice ( curr ) . special , subtotal : acc . subtotal + getItemPrice ( curr ) . regular , } ) , ( { total : 0 , subtotal : 0 } ) ) ; } ; export const getShippingPrice = ( wishlist ) : number => 0 ; export const getTotalItems = ( wishlist ) : number => ( Array . isArray ( wishlist ) ? wishlist [ 0 ] ?. items_count : ( wishlist ?. items_count || 0 ) ) ; export const getFormattedPrice = ( price ) : string => '' ; const getPagination = ( wishlistData ) : AgnosticPagination => ( { currentPage : wishlistData ?. items_v2 ?. page_info ?. current_page || 1 , totalPages : wishlistData ?. items_v2 ?. page_info ?. total_pages || 1 , totalItems : wishlistData ?. items_count || 0 , itemsPerPage : wishlistData ?. items_v2 ?. page_info ?. page_size || 10 , pageOptions : [ 10 , 50 , 100 ] , } ) ; const getProducts = ( wishlistData ) : { product : WishlistProduct ; quantity : number ; added_at : string ; } [ ] => { if ( ! wishlistData || ( Array . isArray ( wishlistData ) && wishlistData . length === 0 ) ) { return [ ] ; } const reducer = ( acc , curr ) => [ ... acc , ... curr . items_v2 . items . map ( ( item ) => ( { product : item . product , quantity : item . quantity , added_at : item . added_at , id : item . id , } ) ) ] ; const mapper = ( item ) => ( { product : item . product , quantity : item . quantity , added_at : item . added_at , id : item . id , } ) ; return Array . isArray ( wishlistData ) ? wishlistData . reduce ( ( acc , curr ) => reducer ( acc , curr ) , [ ] ) : wishlistData ?. items_v2 ?. items . map ( ( e ) => mapper ( e ) ) ; } ; export interface WishlistGetters extends BaseWishlistGetters < Wishlist , WishlistProduct > { getShippingPrice ( wishlist ) : number ; getItemQty ( product ) : number ; getPagination ( wishlistData ) : AgnosticPagination ; getProducts ( wishlistData ) : { product : WishlistProduct ; quantity : number ; added_at : string ; } [ ] ; } const wishlistGetters = { getTotals , getShippingPrice , getItems , getItemName , getItemImage , getItemPrice , getItemQty , getItemAttributes , getItemSku , getTotalItems , getFormattedPrice , getPagination , getProducts , } ; export default wishlistGetters ;