'use strict';

const REPLAYABLE_PART_TYPES = new Set([
  'message',
  'user',
  'assistant',
  'text',
  'reasoning',
  'tool',
  'diagnostic',
  'compaction',
]);

const DURABLE_NON_REPLAY_PART_TYPES = new Set([
  'artifact',
  'prompt_manifest',
  'snapshot',
  'step_start',
  'step_finish',
  'runtime_thread',
  'runtime_turn',
  'runtime_item',
  'runtime_event',
  'lane_event',
]);

function isReplayableRuntimePart(partType) {
  return REPLAYABLE_PART_TYPES.has(String(partType || ''));
}

function isDurableRuntimePart(partType) {
  const type = String(partType || '');
  return REPLAYABLE_PART_TYPES.has(type) || DURABLE_NON_REPLAY_PART_TYPES.has(type);
}

function runtimeProjectionFlags(partType) {
  const type = String(partType || '');
  return {
    partType: type,
    durable: isDurableRuntimePart(type),
    replayable: isReplayableRuntimePart(type),
    ctmProjectable: type !== 'progress',
  };
}

function attachProjectionFlags(record = {}, partType = record.partType || record.type) {
  return {
    ...record,
    runtimeProjection: runtimeProjectionFlags(partType),
  };
}

module.exports = {
  DURABLE_NON_REPLAY_PART_TYPES,
  REPLAYABLE_PART_TYPES,
  attachProjectionFlags,
  isDurableRuntimePart,
  isReplayableRuntimePart,
  runtimeProjectionFlags,
};
