/** Registry de módulos Desk — Spec 015 */
const DeskModules = {
  list: [],
  loaded: false,

  async load() {
    const data = await api('/v1/modules');
    this.list = data.modules || [];
    this.loaded = true;
    return this.list;
  },

  isEnabled(moduleId) {
    const mod = this.list.find((m) => m.id === moduleId);
    if (!mod) return true;
    return !!mod.enabled;
  },

  isViewEnabled(view) {
    const btn = document.querySelector(`.nav button[data-view="${view}"]`);
    if (!btn || btn.hasAttribute('hidden')) return false;
    const modId = btn.dataset.module;
    if (!modId) return true;
    return this.isEnabled(modId);
  },

  applyVisibility() {
    document.querySelectorAll('[data-module]').forEach((el) => {
      const modId = el.dataset.module;
      if (this.isEnabled(modId)) el.removeAttribute('hidden');
      else el.setAttribute('hidden', '');
    });
  },
};

window.DeskModules = DeskModules;
