// components/updater/index.js — App Updater feature module.
//
// Per-app version tracking + CVE/security scanning + disaster-recovery
// (snapshot-before-update and rollback). Built in the same shape as the backup
// feature: a self-registering module whose mount() lazy-loads the controller,
// renders a fragment, and news the page object; unmount() releases its
// task-refresh registration. All state-changing actions go through the task
// system (libreportal updater …), never a new mutating API.
LP.features.register({
  id: 'updater',
  routes: ['/updater', '/updater*'],
  scripts: ['/components/updater/js/updater-page.js'],

  async mount(ctx) {
    await ctx.loadScripts(this.scripts);
    const html = await ctx.loadFragment('/components/updater/html/updater-content.html');
    ctx.setContent(html, 'Updates');
    if (typeof UpdaterPage === 'undefined') {
      throw new Error('UpdaterPage controller failed to load');
    }
    window.updaterPage = new UpdaterPage(ctx.services);
    await window.updaterPage.init();
  },

  async unmount(ctx) {
    // Drop the task-refresh registration so a finished update/rollback task
    // doesn't repaint a torn-down page. The page self-guards via
    // (window.updaterPage === this); nulling it neutralises any pending work.
    try { ctx.services.tasks.refresh && ctx.services.tasks.refresh.unregister('updater'); } catch (_) {}
    // Release the auto-refresh timer that keeps the page in step with the
    // host-side auto-scan.
    try { window.updaterPage && window.updaterPage.dispose && window.updaterPage.dispose(); } catch (_) {}
    window.updaterPage = null;
  },
});
