import type { ZoneId } from '../../../entities/zone/model/meta';
import { ZONE_META } from '../../../entities/zone/model/meta';

function toTextValue(value: unknown): string {
  if (value == null) return '';
  return String(value).trim();
}

export function getZoneIdFromProps(props: Record<string, unknown> | null | undefined): string {
  const safeProps = props || {};
  const candidates = [
    'zoneId',
    'zone_id',
    'zoneIdNo',
    'zoneKey',
    'zoneCode',
    'ZONE_ID',
    'ZONECODE',
    'id',
  ];

  for (const key of candidates) {
    const value = toTextValue(safeProps[key]);
    if (value) return value;
  }

  return '';
}

export function getZoneDisplayNameFromProps(props: Record<string, unknown> | null | undefined): string {
  const safeProps = props || {};
  const nameCandidates = ['zoneName', 'zoneLabel', 'NAME', 'name', 'ZONE_NM', 'label'];
  for (const key of nameCandidates) {
    const name = toTextValue(safeProps[key]);
    if (name) return name;
  }
  const zoneId = getZoneIdFromProps(safeProps);
  if (!zoneId) return '수역';
  return ZONE_META[(zoneId as ZoneId)]?.name || `수역 ${zoneId}`;
}
