import data from "./pages/api/data.json" ; import { IAllProducts , ICartProduct , ICategory , INotFound , IProduct , IProducutWithCategory , ITopCategories , } from "./types" ; export function getData ( ) { const { top_categories , top_products } = data ; return { top_categories , top_products } ; } const notFound = { message : "Not Found" } ; export function getProduct ( categoryId , productId ) { if ( ! categoryId && ! productId ) { return notFound ; } const foundCategory = data . all_products . find ( ( prod ) => prod . category_id === Number ( categoryId ) ) ; if ( ! foundCategory ) { return notFound ; } if ( Array . isArray ( foundCategory . products ) ) { const foundProduct = ( foundCategory . products as Array < IProduct > ) . find ( ( el ) => el . id === Number ( productId ) ) ; if ( ! foundProduct ) { return notFound ; } return { category_id : foundCategory . category_id , category_name : foundCategory . category_name , product : foundProduct , } ; } return notFound ; } interface StaticPropsReturn { params : { categoryId : string ; productId : string ; } ; } export function getTopProductsIds ( ) { const result = [ ] ; data . top_products . forEach ( ( topProduct ) => { topProduct . products . forEach ( ( product ) => { if ( product ) { result . push ( { params : { categoryId : topProduct . category_id . toString ( ) , productId : product . id . toString ( ) , } , } ) ; } } ) ; } ) ; return result ; } export function getCategory ( category_id ) { const foundCategory = data . top_products . find ( ( product ) => product . category_id === Number ( category_id ) ) ; if ( ! foundCategory ) { return undefined ; } return foundCategory ; } export function getCategoryList ( ) { return data . top_categories ; } export function getPercentageDecreased ( originalPrice , basePrice ) { return Math . round ( ( ( originalPrice - basePrice ) / originalPrice ) * 100 ) ; } /**
 * Find the products for a give query
 */ export function findProducts ( query ) { if ( ! query . trim ( ) . length ) { return [ ] ; } const foundProducts = [ ] ; data . all_products . forEach ( ( category ) => { category . products . forEach ( ( product ) => { if ( product . name . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ) { foundProducts . push ( product ) ; } } ) ; } ) ; return foundProducts ; } const breakpoints = { sm : "640px" , md : "768px" , lg : "1024px" , xl : "1280px" , "2xl" : "1536px" , } ; export const mediaQueries = ( key ) => { return ( style ) => `@media (min-width: ${ breakpoints [ key ] }) { ${ style } }` ; } ; export const getTotalItems = ( cart ) => { return cart . reduce ( ( acc , curr ) => { return acc + curr . count ; } , 0 ) ; } ;