import type {GameAction} from './actions'
import {useGameStore} from "../store/gameStore.ts";

export function dispatchGameAction(action: GameAction) {
    const store = useGameStore()

    switch (action.type) {
        case 'GAME_INIT':
            store.initGame(action.payload)
            break

        case 'GAME_START':
            store.phase = 'playing'
            store.dealerIndex = action.payload.dealerIndex
            break

        case 'DRAW_TILE':
            store.onDrawTile(action.payload)
            break

        case 'PLAY_TILE':
            store.onPlayTile(action.payload)
            break

        case 'PENDING_CLAIM':
            store.onPendingClaim(action.payload)
            break

        case 'CLAIM_RESOLVED':
            store.clearPendingClaim()
            break

        case 'ROOM_PLAYER_UPDATE':
            store.onRoomPlayerUpdate(action.payload)
            break


        default:
            throw new Error('Invalid game action')
    }
}
