function normalizeSegment(value: string | number): string {
  return String(value ?? '').trim()
}

function joinPartId(prefix: string, ...segments: Array<string | number>): string {
  return [prefix, ...segments.map(normalizeSegment)].join(':')
}

export function createLocalInteractivePartId(
  kind: 'thinking' | 'todo' | 'plan',
  ownerId: string,
  blockOrdinal: number,
): string {
  return joinPartId(
    kind,
    'local',
    ownerId,
    Math.max(0, Math.floor(Number(blockOrdinal) || 0)),
  )
}

export function createThinkingPartIdFromReasoningItem(itemId: string): string {
  return joinPartId('thinking', 'item', itemId)
}

export function createThinkingPartIdFromStream(
  turnId: string,
  streamId: string,
): string {
  return joinPartId('thinking', 'stream', turnId, streamId)
}

export function createLocalThinkingPartId(
  ownerId: string,
  blockOrdinal: number,
): string {
  return createLocalInteractivePartId('thinking', ownerId, blockOrdinal)
}

export function createChecklistPartId(
  kind: 'todo' | 'plan',
  toolCallId: string,
): string {
  return joinPartId(kind, 'tool', toolCallId)
}
