/**
 * Zeq Chrome — shared config + canonical constants.
 *
 * One source of truth for: the live fork origin (configurable, NOT a dead
 * localhost), the framework constants (charter-exact), and the operator
 * SURFACE strings (never a precise count — P21.R13).
 *
 * Works in the service worker (importScripts), content scripts, and the
 * side panel — it attaches to `self`, which exists in every context.
 *
 * @version 1.287.6
 * @license CC BY 4.0 — Zeq. H / Zeq 1.287 Hz Limited
 */
(function (global) {
  'use strict';

  // The live fork the extension binds to by default. User-overridable via
  // chrome.storage.local 'zeq_api_origin' (set in panel settings). This
  // replaces the old hard-coded http://localhost stack entirely.
  const DEFAULT_ORIGIN = 'https://zeqstate.com';

  const C = {
    // ── Charter-exact constants ──────────────────────────────────────────
    HZ: 1.287, // HulyaPulse system clock (f = 1/tau)
    TAU_S: 0.777000777, // 1 Zeqond = 777,000,777 ns exactly (display 0.777 s)
    ALPHA_KO42: 0.001, // KO42 modulation alpha = 1e-3 EXACTLY.
    // NOTE: the economy's alpha_econ = 1.29e-3 is a SEPARATE tokenomics
    // parameter — never conflate it with the KO42 modulation constant.

    // ── Operator SURFACE strings (never a precise count — P21.R13) ───────
    OPERATORS_HERO: '42+ Kinematic operators',
    OPERATORS_CATALOGUE: '1,500+ catalogued operators',

    DEFAULT_ORIGIN,

    // ── Honest time arithmetic (computed, never "synced") ────────────────
    phase(now) { now = (now == null ? Date.now() / 1000 : now); return (now % C.TAU_S) / C.TAU_S; },
    zeqond(now) { now = (now == null ? Date.now() / 1000 : now); return now / C.TAU_S; },
    ko42(now) { now = (now == null ? Date.now() / 1000 : now); return C.ALPHA_KO42 * Math.sin(2 * Math.PI * C.HZ * now); },

    // ── Live origin resolution (async — reads user override, else default) ─
    async origin() {
      try {
        if (global.chrome && chrome.storage && chrome.storage.local) {
          const r = await chrome.storage.local.get('zeq_api_origin');
          if (r && r.zeq_api_origin) return String(r.zeq_api_origin).replace(/\/+$/, '');
        }
      } catch (_) { /* fall through to default */ }
      return DEFAULT_ORIGIN;
    },
    async api() { return (await C.origin()) + '/api'; },

    // Canonical live endpoints (relative to /api), so callers never guess.
    endpoints: {
      registerV3: '/zeq-auth/register-v3',
      loginV3: '/zeq-auth/login-v3',
      saltV3: '/zeq-auth/salt/', // + :zid
      verifyV3: '/zeq-auth/verify-v3',
      logoutV3: '/zeq-auth/logout-v3',
      compute: '/zeq/wizard/compute',
      authBootstrap: '/zeq/wizard/auth-bootstrap',
      operators: '/operators',
      operatorsCategories: '/operators/categories',
      chainRecent: '/chain/recent',
      chainMachines: '/chain/state-machines',
      chainState: '/chain/', // + :slug + /state
      hiteEncrypt: '/hite/encrypt',
      hiteDecrypt: '/hite/decrypt',
      tescSend: '/tesc/send',
      sslStatus: '/ssl/verify',
      health: '/health',
      appsTrack: '/apps/track',
    },
  };

  global.ZeqConfig = C;
})(typeof self !== 'undefined' ? self : this);
