/** * 데이터 유틸리티 함수 * @description gameData, pageData 처리에 필요한 유틸리티 함수를 제공합니다. */ import type { PageDataValue, PageDataComponent, } from '#layers/types/api/pageData' /** * 페이지 데이터를 기반으로 레이아웃 타입을 결정합니다. * @param pageData 페이지 데이터 * @returns 레이아웃 타입 ('default' | 'promotion') */ export const getLayoutType = ( pageData: PageDataValue | null ): 'default' | 'promotion' => { return pageData?.page_type === 1 ? 'default' : 'promotion' } /** * 그룹의 첫 번째 데이터를 반환합니다. * @param source props.components 또는 group 객체 * @returns 첫 번째 그룹 데이터 또는 null */ export const getFirstGroup = (source: any) => { if (!source) return null return source.groups?.[0] || null } /** * 컴포넌트 그룹에 데이터가 존재하는지 확인합니다. * @param source props.components 또는 group 객체 * @param componentName 컴포넌트 이름 * @returns 데이터 존재 여부 */ export const hasComponentGroup = ( source: any, componentName: string ): boolean => { if (!source) return false const component = source[componentName] as PageDataComponent return component?.groups && component.groups.length > 0 } /** * 컴포넌트 그룹의 첫 번째 데이터를 반환합니다. * @param source props.components 또는 group 객체 * @param componentName 컴포넌트 이름 * @returns 첫 번째 그룹 데이터 또는 null */ export const getComponentGroup = (source: any, componentName: string) => { if (!source) return null return getFirstGroup(source[componentName]) } /** * 컴포넌트 그룹의 모든 데이터를 반환합니다. * @param source props.components 또는 group 객체 * @param componentName 컴포넌트 이름 * @returns 그룹 배열 데이터 */ export const getComponentGroupAry = (source: any, componentName: string) => { if (!source) return [] return source[componentName]?.groups || [] } /** * 현재 시간의 타임스탬프를 반환합니다. * @param unit 단위 ('ms' | 's') - 밀리초 또는 초 * @returns 타임스탬프 */ export const getCurrentTimestamp = (unit: 'ms' | 's' = 'ms'): number => { const now = Date.now() return unit === 's' ? Math.floor(now / 1000) : now }