/**
 * Derive time fields from a now() provider — record-store pattern (spec §3).
 * createdAt ISO is RE-DERIVED from createdAtMs (never the raw now() echo),
 * so it is collation-safe and identical across all adapters.
 */

export function deriveCreatedAt(now: () => string): { createdAtMs: number; createdAt: string } {
  const createdAtMs = Date.parse(now())
  if (Number.isNaN(createdAtMs)) {
    throw new RangeError('deriveCreatedAt: now() returned an unparseable timestamp')
  }
  return {
    createdAtMs,
    createdAt: new Date(createdAtMs).toISOString(),
  }
}
