export interface DetailHistoryState {
  listPath: string;
  depth: number;
  origin: 'list' | 'direct';
}

export function detailHistoryState(value: unknown): DetailHistoryState | null {
  if (!value || typeof value !== 'object') return null;
  const candidate = (value as { mailhubDetail?: unknown }).mailhubDetail;
  if (!candidate || typeof candidate !== 'object') return null;
  const { listPath, depth, origin } = candidate as Partial<DetailHistoryState>;
  if (typeof listPath !== 'string' || !listPath.startsWith('/') || !Number.isInteger(depth) || Number(depth) < 1) return null;
  if (origin !== undefined && origin !== 'list' && origin !== 'direct') return null;
  return { listPath, depth: Number(depth), origin: origin || 'list' };
}

export function detailHistoryLocation(listPath: string, depth: number, origin: DetailHistoryState['origin'] = 'list') {
  return { mailhubDetail: { listPath, depth, origin } };
}
