import type { Context , Options } from '../../index' ; import { fetchAPI } from '../fetch' ; import type { Brand } from './types/Brand' ; import type { CategoryTree } from './types/CategoryTree' ; import type { OrderForm , OrderFormInputItem } from './types/OrderForm' ; import type { PortalPagetype } from './types/Portal' ; import type { Simulation , SimulationArgs , SimulationOptions , } from './types/Simulation' ; const BASE_INIT = { method : 'POST' , headers : { 'content-type' : 'application/json' } } ; export const VtexCommerce = ( { account , environment } , ctx ) => { const base = `http://${ account }.${ environment }.com.br` ; return { catalog : { brand : { list : ( ) : Promise < Brand [ ] > => fetchAPI ( `${ base }/api/catalog_system/pub/brand/list` ) } , category : { tree : ( depth = 3 ) : Promise < CategoryTree [ ] > => fetchAPI ( `${ base }/api/catalog_system/pub/category/tree/${ depth }` ) } , portal : { pagetype : ( slug ) : Promise < PortalPagetype > => fetchAPI ( `${ base }/api/catalog_system/pub/portal/pagetype/${ slug }` ) } } , checkout : { simulation : ( args , { salesChannel } = { salesChannel : ctx . storage . channel } ) : Promise < Simulation > => { const params = new URLSearchParams ( { sc : salesChannel } ) ; return fetchAPI ( `${ base }/api/checkout/pub/orderForms/simulation?${ params . toString ( ) }` , { ... BASE_INIT , body : JSON . stringify ( args ) } ) ; } , orderForm : ( { id , refreshOutdatedData = true , salesChannel = ctx . storage . channel , } ) : Promise < OrderForm > => { const params = new URLSearchParams ( { refreshOutdatedData : refreshOutdatedData . toString ( ) , sc : salesChannel } ) ; return fetchAPI ( `${ base }/api/checkout/pub/orderForm/${ id }?${ params . toString ( ) }` , BASE_INIT ) ; } , updateOrderFormItems : ( { id , orderItems , allowOutdatedData = 'paymentData' , salesChannel = ctx . storage . channel , } ) : Promise < OrderForm > => { const params = new URLSearchParams ( { allowOutdatedData , sc : salesChannel } ) ; return fetchAPI ( `${ base }/api/checkout/pub/orderForm/${ id }/items?${ params }` , { ... BASE_INIT , body : JSON . stringify ( { orderItems } ) , method : 'PATCH' } ) ; } } } ; } ;