// Pure helpers — safe to import from client components.
// DB-backed helpers (generateNextStoryCode/PbiCode) live in lib/code-server.ts.

const VALID_CODE_RE = /^[A-Za-z0-9._-]+$/

export const MAX_CODE_LENGTH = 30

export function isValidCode(code: string): boolean {
  return code.length > 0 && code.length <= MAX_CODE_LENGTH && VALID_CODE_RE.test(code)
}

export function normalizeCode(input: string | null | undefined): string | null {
  if (input == null) return null
  const trimmed = input.trim()
  return trimmed === '' ? null : trimmed
}

export function deriveTaskCode(storyCode: string | null, indexOneBased: number): string | null {
  if (!storyCode) return null
  return `${storyCode}.${indexOneBased}`
}
