import { useCallback } from 'react'; import { usePersSDK } from '../providers/PersSDKProvider'; import type { BusinessDTO, BusinessTypeDTO, BusinessUpdateRequestDTO, BusinessToggleActiveRequestDTO, PaginatedResponseDTO, BusinessFilterOptions } from '@explorins/pers-sdk'; // Re-export for consumers export type { BusinessFilterOptions } from '@explorins/pers-sdk'; /** * React hook for business operations in the PERS SDK * * Provides methods for fetching, creating, and managing businesses within the platform. * Includes both public business queries and administrative operations. * * @returns Business hook with methods for business management * * @example * ```typescript * function BusinessesComponent() { * const { * getActiveBusinesses, * getBusinessById, * createBusiness * } = useBusiness(); * * const loadBusinesses = async () => { * try { * const businesses = await getActiveBusinesses(); * console.log('Active businesses:', businesses); * } catch (error) { * console.error('Failed to load businesses:', error); * } * }; * * return ( *
* *
* ); * } * ``` */ export const useBusiness = () => { const { sdk, isInitialized } = usePersSDK(); /** * Retrieves all currently active businesses in the system * * @returns Promise resolving to array of active businesses * @throws Error if SDK is not initialized * * @example * ```typescript * const { getActiveBusinesses } = useBusiness(); * const businesses = await getActiveBusinesses(); * console.log('Active businesses:', businesses); * ``` */ const getActiveBusinesses = useCallback(async (): Promise> => { if (!isInitialized || !sdk) { throw new Error('SDK not initialized. Call initialize() first.'); } try { const result = await sdk.businesses.getActiveBusinesses(); return result; } catch (error) { console.error('Failed to fetch active businesses:', error); throw error; } }, [sdk, isInitialized]); /** * Retrieves all available business types/categories * * @returns Promise resolving to array of business types * @throws Error if SDK is not initialized * * @example * ```typescript * const { getBusinessTypes } = useBusiness(); * const types = await getBusinessTypes(); * console.log('Business types:', types); * ``` */ const getBusinessTypes = useCallback(async (): Promise> => { if (!isInitialized || !sdk) { throw new Error('SDK not initialized. Call initialize() first.'); } try { const result = await sdk.businesses.getBusinessTypes(); return result; } catch (error) { console.error('Failed to fetch business types:', error); throw error; } }, [sdk, isInitialized]); const getBusinessById = useCallback(async (businessId: string): Promise => { if (!isInitialized || !sdk) { throw new Error('SDK not initialized. Call initialize() first.'); } try { const result = await sdk.businesses.getBusinessById(businessId); return result; } catch (error) { console.error('Failed to fetch business:', error); throw error; } }, [sdk, isInitialized]); /** * Admin: Get all businesses with optional filtering * * @param options - Filter and pagination options * @returns Promise resolving to paginated businesses * * @example * ```typescript * // Get all businesses * const { data } = await getBusinesses(); * * // Filter by tag * const tagged = await getBusinesses({ tag: 'restaurant' }); * * // Multiple tags (OR match) * const multiTag = await getBusinesses({ tag: ['restaurant', 'cafe'] }); * * // With search and pagination * const filtered = await getBusinesses({ * search: 'coffee', * active: true, * page: 1, * limit: 20 * }); * ``` */ const getBusinesses = useCallback(async (options?: BusinessFilterOptions): Promise> => { if (!isInitialized || !sdk) { throw new Error('SDK not initialized. Call initialize() first.'); } try { const result = await sdk.businesses.getBusinesses(options); return result; } catch (error) { console.error('Failed to fetch all businesses:', error); throw error; } }, [sdk, isInitialized]); const getBusinessByAccount = useCallback(async (accountAddress: string): Promise => { if (!isInitialized || !sdk) { throw new Error('SDK not initialized. Call initialize() first.'); } try { const result = await sdk.businesses.getBusinessByAccount(accountAddress); return result; } catch (error) { console.error('Failed to fetch business by account:', error); throw error; } }, [sdk, isInitialized]); const getBusinessesByType = useCallback(async (typeId: string): Promise> => { if (!isInitialized || !sdk) { throw new Error('SDK not initialized. Call initialize() first.'); } try { const result = await sdk.businesses.getBusinessesByType(typeId); return result; } catch (error) { console.error('Failed to fetch businesses by type:', error); throw error; } }, [sdk, isInitialized]); // Admin methods /** * Creates a new business (Admin operation) * * @param displayName - The display name for the new business * @returns Promise resolving to the created business data * @throws Error if SDK is not initialized or user lacks permissions * * @example * ```typescript * const { createBusiness } = useBusiness(); * const business = await createBusiness('My Coffee Shop'); * console.log('Business created:', business); * ``` */ const createBusiness = useCallback(async (displayName: string): Promise => { if (!isInitialized || !sdk) { throw new Error('SDK not initialized. Call initialize() first.'); } try { const result = await sdk.businesses.createBusiness(displayName); return result; } catch (error) { console.error('Failed to create business:', error); throw error; } }, [sdk, isInitialized]); const updateBusiness = useCallback(async (businessId: string, businessData: BusinessUpdateRequestDTO): Promise => { if (!isInitialized || !sdk) { throw new Error('SDK not initialized. Call initialize() first.'); } try { const result = await sdk.businesses.updateBusiness(businessId, businessData); return result; } catch (error) { console.error('Failed to update business:', error); throw error; } }, [sdk, isInitialized]); const toggleBusinessStatus = useCallback(async (businessId: string, toggleData: BusinessToggleActiveRequestDTO): Promise => { if (!isInitialized || !sdk) { throw new Error('SDK not initialized. Call initialize() first.'); } try { const result = await sdk.businesses.toggleBusinessStatus(businessId, toggleData); return result; } catch (error) { console.error('Failed to toggle business status:', error); throw error; } }, [sdk, isInitialized]); return { getActiveBusinesses, getBusinessTypes, getBusinesses, getBusinessById, getBusinessByAccount, getBusinessesByType, createBusiness, updateBusiness, toggleBusinessStatus, isAvailable: isInitialized && !!sdk?.businesses, }; }; export type BusinessHook = ReturnType;