import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

export async function loadGameAssets(baseUrl = '/'): Promise<Record<string, string>> {
  const base = baseUrl.replace(/\/$/, '') + '/';
  const res = await fetch(base + 'gameAssets.json');
  if (!res.ok) throw new Error('Failed to fetch gameAssets.json');
  return res.json();
}

export async function loadTexture(url: string): Promise<THREE.Texture> {
  const loader = new THREE.TextureLoader();
  return new Promise((resolve, reject) => {
    loader.load(url, (tex) => {
      tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
      tex.colorSpace = THREE.LinearSRGBColorSpace;
      resolve(tex);
    }, undefined, reject);
  });
}

export async function loadGlb(url: string): Promise<THREE.Group> {
  const loader = new GLTFLoader();
  return new Promise((resolve, reject) => {
    loader.load(url, (gltf) => resolve(gltf.scene), undefined, reject);
  });
}
