/**
 * Fractional indexing helpers for KB article ordering.
 *
 * Articles are ordered within their parent by a NUMERIC position field.
 * Fractional indexing allows O(1) reorder without renumbering siblings.
 *
 * Strategy: positions are positive decimals. Default gap = 1024.
 * - New item at end: prev + 1024 (or 1024 if no prev)
 * - Reorder between a and b: (a + b) / 2
 */

const DEFAULT_START = 1024

/**
 * Compute the position for a new item appended after `prev`.
 * If prev is null (no existing siblings), returns the default start value.
 */
export function fractionalIndexAfter(prev: number | null): number {
  if (prev === null) return DEFAULT_START
  return prev + DEFAULT_START
}

/**
 * Compute a position between `a` and `b`.
 * Assumes a < b. If values collapse too close, callers should eventually
 * renormalize — but in practice this is sufficient for thousands of reorders.
 */
export function fractionalIndexBetween(a: number, b: number): number {
  return (a + b) / 2
}
