import type { OperationContext, OperationOptions, } from '@commerce/api/operations' import { GetAllProductPathsOperation } from '../../types/product' import { GetAllProductPathsQuery, GetAllProductPathsQueryVariables, } from '../../schema' import type { WooCommerceConfig, Provider } from '..' import getAllProductsQuery from '../../wp/queries/get-all-products-paths-query' export default function getAllProductPathsOperation({ commerce, }: OperationContext) { async function getAllProductPaths< T extends GetAllProductPathsOperation >(opts?: { variables?: T['variables'] config?: WooCommerceConfig }): Promise async function getAllProductPaths( opts: { variables?: T['variables'] config?: WooCommerceConfig } & OperationOptions ): Promise async function getAllProductPaths({ query = getAllProductsQuery, config, variables, }: { query?: string config?: WooCommerceConfig variables?: T['variables'] } = {}): Promise { config = commerce.getConfig(config) const { data } = await config.fetch< GetAllProductPathsQuery, GetAllProductPathsQueryVariables >(query, { variables }) const products = data?.products?.edges ? data.products.edges.map((edge) => ({ path: `/${edge?.node?.slug}`, })) : [] return { products, } } return getAllProductPaths }