// panes/mac.js — macOS app distribution.
import { registerPane } from '../app.js';
import { jget } from '../lib/api.js';
import { fmtJSON } from '../lib/ui.js';

const TPL = `
  <div class="pane-head">
    <div><div class="title">macOS</div><div class="sub">Native app distribution.</div></div>
    <div class="grow"></div>
    <button class="btn btn-secondary" id="mac-refresh">Refresh</button>
  </div>
  <div class="card">
    <div class="card-title"><h2>Build</h2><a class="btn btn-primary" id="mac-download" style="display:none">Download</a></div>
    <pre id="mac-info">…</pre>
  </div>
`;

async function load(host) {
  try {
    const r = await jget('/api/mac/info');
    host.querySelector('#mac-info').textContent = fmtJSON(r);
    const a = host.querySelector('#mac-download');
    if (r.download_url) {
      a.href = r.download_url;
      a.textContent = 'Download ' + (r.build?.name || 'app');
      a.style.display = '';
    } else {
      a.style.display = 'none';
    }
  } catch (e) {
    host.querySelector('#mac-info').textContent = e.message;
  }
}

registerPane('mac', {
  label: 'macOS',
  init(host) {
    host.innerHTML = TPL;
    host.querySelector('#mac-refresh').addEventListener('click', () => load(host));
    load(host);
  },
  refresh: load,
});
