// ============================================
// ARROWS GAME - Levels Loader
// ============================================

const Levels = {
  _data: null,
  _loadingPromise: null,

  async load() {
    if (this._data) return this._data;
    if (this._loadingPromise) return this._loadingPromise;

    this._loadingPromise = fetch('levels.json')
      .then(res => {
        if (!res.ok) throw new Error('Failed to load levels.json');
        return res.json();
      })
      .then(json => {
        this._data = json.levels;
        return this._data;
      })
      .catch(err => {
        console.error('[Levels] load error:', err);
        this._data = [];
        return this._data;
      });

    return this._loadingPromise;
  },

  get(levelNum) {
    if (!this._data) return null;
    return this._data.find(l => l.id === levelNum) || null;
  },

  total() {
    return this._data ? this._data.length : GAME_CONFIG.totalLevels;
  },
};
