import { isDevMode, Pipe, PipeTransform, inject } from '@angular/core';

import { DevLogService } from '../devtools/dev-log.service';
import { SESSION_STATUS_LABELS } from './session-status-labels.config';

@Pipe({
  name: 'sessionStatus',
  standalone: true,
})
export class SessionStatusPipe implements PipeTransform {
  private readonly devLog = inject(DevLogService);
  private readonly warnedUnknown = new Set<string>();

  transform(value: string | null | undefined): string {
    if (value == null || value === '') {
      return '—';
    }
    const key = value.trim();
    const mapped = SESSION_STATUS_LABELS[key.toLowerCase()];
    if (mapped !== undefined) {
      return mapped;
    }
    if (isDevMode() && !this.warnedUnknown.has(key)) {
      this.warnedUnknown.add(key);
      this.devLog.add({
        level: 'warn',
        source: 'system',
        message: `Неизвестный статус сессии (нет подписи в session-status-labels.config): ${key}`,
      });
    }
    return key;
  }
}
