import { defineStore } from 'pinia'
import { type Asset, type Object } from '@/types'
import config from '@/config'

export const useAssetStore = defineStore('assets', {
  state: () => ({
    assets: [] as Asset[],
    tiles: [] as string[],
    objects: [] as Object[]
  }),
  actions: {
    setAssets(assets: Asset[]) {
      this.assets = assets
    },
    addAsset(asset: Asset) {
      this.assets.push(asset)
    },
    setTiles(tiles: string[]) {
      this.tiles = tiles
    },
    setObjects(objects: Object[]) {
      this.objects = objects
    },
    async fetchAssets() {
      return fetch(config.server_endpoint + '/assets')
        .then((response) => response.json())
        .then((assets) => {
          this.setAssets(assets)
          return true
        })
        .catch((error) => {
          console.error('Error fetching assets:', error)
          return false
        })
    }
  }
})
