/**
 * Deterministic JSON (must match Android StableJson.kt and broadcast_store.js).
 */
export function stableStringify(value) {
  if (value === null || typeof value !== "object") {
    return JSON.stringify(value);
  }
  if (Array.isArray(value)) {
    return "[" + value.map((v) => stableStringify(v)).join(",") + "]";
  }
  const keys = Object.keys(value).sort();
  return "{" + keys.map((k) => JSON.stringify(k) + ":" + stableStringify(value[k])).join(",") + "}";
}

export function canonicalBytes(obj) {
  return new TextEncoder().encode(stableStringify(obj));
}
