/* Version 4.0 – JSON data loader with file:// fallback */
(function(){
  const fallback=globalThis.FM_DATA||{};
  globalThis.FMDataStore={
    source: location.protocol==='file:'?'embedded':'json',
    leagues:fallback.leagues||globalThis.COUNTRIES||[],
    names:fallback.names||{firstNames:globalThis.FIRST_NAMES||[],lastNames:globalThis.LAST_NAMES||[]},
    stadiums:fallback.stadiums||globalThis.STADIUMS||{},
    attributes:null,
    async load(){
      if(location.protocol==='file:')return this;
      try{
        const [leagues,names,stadiums,attributes]=await Promise.all([
          fetch('data/leagues.json').then(r=>r.json()),fetch('data/names.json').then(r=>r.json()),
          fetch('data/stadiums.json').then(r=>r.json()),fetch('data/player-attributes.json').then(r=>r.json())
        ]);
        Object.assign(this,{leagues,names,stadiums,attributes,source:'json'});
      }catch(err){console.warn('JSON-Daten konnten nicht geladen werden; eingebettete Offline-Daten werden verwendet.',err);}
      return this;
    }
  };
})();
