import { createFeature, createReducer, on } from '@ngrx/store'; import { areaAttractions, type Area, type AreaDetails, type AreaProgress, type Attraction, type AttractionDetails, } from '../areas/area.models'; import { readAuthSession } from '../auth/auth-session'; import { GorodarActions } from './gorodar.actions'; import { RESEND_SECONDS, sessionToAuth, type AuthState, type ClaimState, type GorodarState } from './gorodar.models'; const initialClaim: ClaimState = { landmarkId: null, photo: null, stage: 'idle', step: 0, failure: null, confidence: null, alreadyVisited: false, }; const blankAuth: AuthState = { email: '', accessToken: null, tokenType: 'Bearer', authenticated: false, step: 'email', resendSeconds: 0, pending: false, error: '', sheetVisible: false, intent: 'collection', pendingLandmarkId: null, codeRequestId: 0, }; const restoredAuth: AuthState = { ...blankAuth, ...sessionToAuth(readAuthSession()), }; export const initialState: GorodarState = { photos: {}, collectedAt: {}, claim: initialClaim, auth: restoredAuth, profile: { displayName: 'Иван Иванович', firstName: 'Иван', lastName: 'Иванов', city: 'Нижний Новгород', email: '', phone: '+7 900 123-45-67', joinedLabel: 'Сентябрь 2026', }, areas: [], areasStatus: 'loading', areasError: '', areaDetails: {}, areaDetailStatus: {}, areaDetailError: {}, attractions: {}, attractionStatus: {}, attractionError: {}, }; const gorodarReducer = createReducer( initialState, on( GorodarActions.photoCaptured, (state, { landmarkId, photo }): GorodarState => ({ ...state, claim: { ...initialClaim, landmarkId, photo, stage: 'check', step: 1 }, }), ), on( GorodarActions.claimStepChanged, (state, { step }): GorodarState => ({ ...state, claim: { ...state.claim, step }, }), ), on( GorodarActions.claimCompleted, (state, { landmarkId, photo, collectedAt, alreadyVisited, progress }): GorodarState => ({ ...state, areas: markListVisited(state.areas, landmarkId, progress), photos: { ...state.photos, [landmarkId]: photo, }, collectedAt: { ...state.collectedAt, [landmarkId]: collectedAt, }, areaDetails: markAreaVisited(state.areaDetails, landmarkId, progress), attractions: markAttractionVisited(state.attractions, landmarkId), claim: { ...initialClaim, landmarkId, photo, stage: 'success', step: 3, alreadyVisited }, }), ), on( GorodarActions.claimFailed, (state, { failure, confidence }): GorodarState => ({ ...state, claim: { ...state.claim, stage: 'failed', failure, confidence }, }), ), on( GorodarActions.claimReset, (state): GorodarState => ({ ...state, claim: initialClaim, }), ), on(GorodarActions.authSheetOpened, (state, { intent, landmarkId }): GorodarState => { if (state.auth.authenticated) { return state; } return { ...state, auth: { ...state.auth, sheetVisible: true, intent, pendingLandmarkId: landmarkId ?? null, error: '', }, }; }), on( GorodarActions.authSheetDismissed, (state): GorodarState => ({ ...state, auth: { ...state.auth, sheetVisible: false, error: '', }, }), ), on( GorodarActions.authErrorCleared, (state): GorodarState => ({ ...state, auth: { ...state.auth, error: '', }, }), ), on( GorodarActions.authEmailChangeRequested, (state): GorodarState => ({ ...state, auth: { ...state.auth, step: 'email', error: '', resendSeconds: 0, }, }), ), on( GorodarActions.authCodeRequested, (state): GorodarState => ({ ...state, auth: { ...state.auth, pending: true, error: '', }, }), ), on( GorodarActions.authCodeRequestSucceeded, (state, { email }): GorodarState => ({ ...state, auth: { ...state.auth, email, step: 'code', pending: false, error: '', resendSeconds: RESEND_SECONDS, codeRequestId: state.auth.codeRequestId + 1, }, profile: { ...state.profile, email, }, }), ), on( GorodarActions.authCodeRequestFailed, (state, { error }): GorodarState => ({ ...state, auth: { ...state.auth, pending: false, error, }, }), ), on( GorodarActions.authCodeConfirmed, (state): GorodarState => ({ ...state, auth: { ...state.auth, pending: true, error: '', }, }), ), on( GorodarActions.authCodeConfirmSucceeded, (state, { email, accessToken, tokenType, refreshToken, expiresAt }): GorodarState => ({ ...state, auth: { ...state.auth, email, accessToken, tokenType, refreshToken, expiresAt, authenticated: true, step: 'email', pending: false, error: '', resendSeconds: 0, sheetVisible: false, pendingLandmarkId: null, }, profile: { ...state.profile, email, }, }), ), on( GorodarActions.authCodeConfirmFailed, (state, { error }): GorodarState => ({ ...state, auth: { ...state.auth, pending: false, error, }, }), ), on( GorodarActions.authResendTicked, (state, { seconds }): GorodarState => ({ ...state, auth: { ...state.auth, resendSeconds: seconds, }, }), ), on( GorodarActions.logout, (state): GorodarState => ({ ...state, photos: {}, collectedAt: {}, claim: initialClaim, auth: blankAuth, profile: { ...state.profile, email: '', }, areaDetails: {}, areaDetailStatus: {}, areaDetailError: {}, attractions: {}, attractionStatus: {}, attractionError: {}, }), ), on( GorodarActions.areasRequested, (state): GorodarState => ({ ...state, areasStatus: 'loading', areasError: '', }), ), on( GorodarActions.areasLoaded, (state, { areas }): GorodarState => ({ ...state, areas, areasStatus: 'ready', areasError: '', }), ), on( GorodarActions.areasFailed, (state, { error }): GorodarState => ({ ...state, areasStatus: 'error', areasError: error, }), ), on( GorodarActions.areaRequested, (state, { id }): GorodarState => ({ ...state, areaDetailStatus: { ...state.areaDetailStatus, [id]: 'loading', }, areaDetailError: { ...state.areaDetailError, [id]: '', }, }), ), on( GorodarActions.areaLoaded, (state, { area }): GorodarState => ({ ...state, areaDetails: { ...state.areaDetails, [area.id]: area, }, areaDetailStatus: { ...state.areaDetailStatus, [area.id]: 'ready', }, areaDetailError: { ...state.areaDetailError, [area.id]: '', }, }), ), on( GorodarActions.areaFailed, (state, { id, error }): GorodarState => ({ ...state, areaDetailStatus: { ...state.areaDetailStatus, [id]: 'error', }, areaDetailError: { ...state.areaDetailError, [id]: error, }, }), ), on( GorodarActions.attractionRequested, (state, { id }): GorodarState => ({ ...state, attractionStatus: { ...state.attractionStatus, [id]: 'loading', }, attractionError: { ...state.attractionError, [id]: '', }, }), ), on( GorodarActions.attractionLoaded, (state, { attraction }): GorodarState => ({ ...state, attractions: { ...state.attractions, [attraction.id]: attraction, }, attractionStatus: { ...state.attractionStatus, [attraction.id]: 'ready', }, attractionError: { ...state.attractionError, [attraction.id]: '', }, }), ), on( GorodarActions.attractionFailed, (state, { id, error }): GorodarState => ({ ...state, attractionStatus: { ...state.attractionStatus, [id]: 'error', }, attractionError: { ...state.attractionError, [id]: error, }, }), ), ); function markVisited(item: Attraction, id: string): Attraction { return item.id === id ? { ...item, visited: true } : item; } function markListVisited(areas: Area[], landmarkId: string, progress: AreaProgress): Area[] { return areas.map((area) => { const attractions = area.attractions ?? []; if (!attractions.some((item) => item.id === landmarkId)) { return area; } return { ...area, progress: progress ?? area.progress, attractions: attractions.map((item) => markVisited(item, landmarkId)), }; }); } function markAreaVisited( details: Record, landmarkId: string, progress: AreaProgress, ): Record { return Object.fromEntries( Object.entries(details).map(([id, area]) => { const contains = areaAttractions(area).some((item) => item.id === landmarkId); if (!contains) { return [id, area]; } return [ id, { ...area, progress: progress ?? area.progress, attractions: area.attractions.map((item) => markVisited(item, landmarkId)), children: area.children.map((child) => ({ ...child, attractions: child.attractions.map((item) => markVisited(item, landmarkId)), })), }, ]; }), ); } function markAttractionVisited( attractions: Record, landmarkId: string, ): Record { const current = attractions[landmarkId]; if (!current) { return attractions; } return { ...attractions, [landmarkId]: { ...current, visited: true, }, }; } export const gorodarFeature = createFeature({ name: 'gorodar', reducer: gorodarReducer, });