import { IMedia, ArrowSize } from '../types'; import HistoryService from './history'; export const Helpers = { createArray: (length: number): number[] => Array.from({ length }), // eslint-disable-next-line max-len chunkArray: (array: T[], chunkSize: number): T[][] => Array.from({ length: Math.ceil(array.length / chunkSize) }, (_, i) => array.slice(i * chunkSize, (i + 1) * chunkSize), ), getAbsoluteBackgroundColor: (element?: Element | null): string => { if (!element) { return 'white'; } const bg = window.getComputedStyle(element).getPropertyValue('background-color'); if (bg === 'transparent' || bg === 'rgba(0, 0, 0, 0)') { return element.parentElement ? Helpers.getAbsoluteBackgroundColor(element.parentElement) : 'white'; } return bg; }, isValidJson: (jsonString: string): boolean => { try { const parsed: unknown = JSON.parse(jsonString); return Boolean(parsed) && typeof parsed === 'object'; } catch { return false; } }, isMobileDevice: (): boolean => { if (typeof navigator === 'undefined') { return false; } const userAgent = navigator.userAgent.toLowerCase(); // Explicit mobile device patterns - more specific detection const mobilePatterns = [ /android.*mobile/i, // Android phones (not tablets) /iphone/i, /ipod/i, /blackberry/i, /windows phone/i, /windows ce/i, /palm/i, /symbian/i, /webos/i, /mobile/i, ]; // Explicit tablet patterns const tabletPatterns = [ /ipad/i, /android(?!.*mobile)/i, // Android tablets /tablet/i, /kindle/i, /playbook/i, /nexus.*7/i, ]; // Check for explicit mobile patterns first const isMobileUserAgent = mobilePatterns.some((pattern) => pattern.test(userAgent)); const isTabletUserAgent = tabletPatterns.some((pattern) => pattern.test(userAgent)); // If it's explicitly a desktop OS, return false regardless of touch if ( /windows nt|mac os x|linux.*x86/i.test(userAgent) && !isMobileUserAgent && !isTabletUserAgent ) { return false; } // Return true only for explicit mobile devices return isMobileUserAgent || isTabletUserAgent; }, pushPostToHistory: (post: IMedia): void => { HistoryService.push(post); }, replacePostInHistory: (post: IMedia): void => { HistoryService.replace(post); }, uniqId: (): string => `${Date.now()}${Math.floor(Math.random() * 1000)}`, randomId: (): string => Math.random().toString(36).slice(2, 10), calculateArrowSize: (containerWidth: number): ArrowSize => { if (containerWidth < 200) return 'nano'; if (containerWidth < 300) return 'micro'; if (containerWidth < 650) return 'small'; if (containerWidth < 800) return 'compact'; if (containerWidth < 1000) return 'medium'; return 'large'; }, };