import { defineStore } from 'pinia'
import type { Character } from '@/types'

export const useZoneStore = defineStore('zone', {
  state: () => ({
    loaded: false,
    tiles: undefined,
    characters: [] as Character[]
  }),
  getters: {
    isLoaded: (state) => state.loaded,
    getTiles: (state) => state.tiles,
    getCharacters: (state) => state.characters
  },
  actions: {
    loadTiles(tiles: any) {
      this.tiles = tiles
      this.loaded = true
    },
    setCharacters(characters: Character[]) {
      this.characters = characters
    },
    addCharacter(character: Character) {
      this.characters.push(character)
    },
    removeCharacter(character: Character) {
      this.characters = this.characters.filter((c: Character) => c.id !== character.id)
    },
    updateCharacter(character: Character) {
      const index = this.characters.findIndex((c) => c.id === character.id)
      if (index !== -1) {
        this.characters[index] = character
      } else {
        console.error(`Character with id ${character.id} not found`)
      }
    }
  }
})
