import type { SubstanceToxicity } from './dispersionTypes';

/**
 * 주요 HNS 물질 독성 및 물리 데이터
 * 출처: EPA AEGL, NIOSH IDLH, ALOHA Chemical Library
 */
const SUBSTANCE_DATA: Record<string, SubstanceToxicity> = {
  '톨루엔 (Toluene)': {
    nameKr: '톨루엔',
    nameEn: 'Toluene',
    aegl1: 37,
    aegl2: 150,
    aegl3: 500,
    idlh: 500,
    mw: 92.14,
    densityGas: 3.18,
    vaporPressure: 28.4,
    poolRadius: 5.0,
    Q: 15.0,
    QTotal: 8000.0,
  },
  '벤젠 (Benzene)': {
    nameKr: '벤젠',
    nameEn: 'Benzene',
    aegl1: 52,
    aegl2: 800,
    aegl3: 4000,
    idlh: 500,
    mw: 78.11,
    densityGas: 2.77,
    vaporPressure: 95.2,
    poolRadius: 5.0,
    Q: 20.0,
    QTotal: 10000.0,
  },
  '자일렌 (Xylene)': {
    nameKr: '자일렌',
    nameEn: 'Xylene',
    aegl1: 130,
    aegl2: 920,
    aegl3: 2500,
    idlh: 900,
    mw: 106.16,
    densityGas: 3.66,
    vaporPressure: 8.8,
    poolRadius: 5.0,
    Q: 10.0,
    QTotal: 6000.0,
  },
  '스티렌 (Styrene)': {
    nameKr: '스티렌',
    nameEn: 'Styrene',
    aegl1: 20,
    aegl2: 130,
    aegl3: 1100,
    idlh: 700,
    mw: 104.15,
    densityGas: 3.6,
    vaporPressure: 6.4,
    poolRadius: 5.0,
    Q: 8.0,
    QTotal: 5000.0,
  },
  '메탄올 (Methanol)': {
    nameKr: '메탄올',
    nameEn: 'Methanol',
    aegl1: 530,
    aegl2: 2100,
    aegl3: 14000,
    idlh: 6000,
    mw: 32.04,
    densityGas: 1.11,
    vaporPressure: 128.0,
    poolRadius: 5.0,
    Q: 25.0,
    QTotal: 15000.0,
  },
  '아세톤 (Acetone)': {
    nameKr: '아세톤',
    nameEn: 'Acetone',
    aegl1: 200,
    aegl2: 3200,
    aegl3: 12000,
    idlh: 2500,
    mw: 58.08,
    densityGas: 2.0,
    vaporPressure: 231.0,
    poolRadius: 5.0,
    Q: 30.0,
    QTotal: 20000.0,
  },
  '염소 (Chlorine)': {
    nameKr: '염소',
    nameEn: 'Chlorine',
    aegl1: 0.5,
    aegl2: 2.8,
    aegl3: 50,
    idlh: 10,
    mw: 70.91,
    densityGas: 3.17,
    vaporPressure: 5168.0,
    poolRadius: 3.0,
    Q: 20.0,
    QTotal: 10000.0,
  },
  '암모니아 (Ammonia)': {
    nameKr: '암모니아',
    nameEn: 'Ammonia',
    aegl1: 30,
    aegl2: 160,
    aegl3: 1100,
    idlh: 300,
    mw: 17.03,
    densityGas: 0.73,
    vaporPressure: 7510.0,
    poolRadius: 3.0,
    Q: 25.0,
    QTotal: 12000.0,
  },
  '염화수소 (HCl)': {
    nameKr: '염화수소',
    nameEn: 'HCl',
    aegl1: 1.8,
    aegl2: 22,
    aegl3: 100,
    idlh: 50,
    mw: 36.46,
    densityGas: 1.49,
    vaporPressure: 42080.0,
    poolRadius: 3.0,
    Q: 15.0,
    QTotal: 8000.0,
  },
  '황화수소 (H2S)': {
    nameKr: '황화수소',
    nameEn: 'H2S',
    aegl1: 0.51,
    aegl2: 17,
    aegl3: 50,
    idlh: 50,
    mw: 34.08,
    densityGas: 1.36,
    vaporPressure: 15600.0,
    poolRadius: 3.0,
    Q: 10.0,
    QTotal: 5000.0,
  },
};

/**
 * 물질명으로 독성 데이터 조회 (한/영 퍼지 매칭)
 */
export function getSubstanceToxicity(name: string): SubstanceToxicity {
  // 정확한 키 매칭
  if (SUBSTANCE_DATA[name]) return SUBSTANCE_DATA[name];

  // 부분 문자열 매칭 (한글명 또는 영문명)
  const lower = name.toLowerCase();
  for (const [, tox] of Object.entries(SUBSTANCE_DATA)) {
    if (
      tox.nameKr === name ||
      tox.nameEn.toLowerCase() === lower ||
      lower.includes(tox.nameEn.toLowerCase()) ||
      name.includes(tox.nameKr)
    ) {
      return tox;
    }
  }

  // 기본값: 톨루엔
  return SUBSTANCE_DATA['톨루엔 (Toluene)'];
}

/**
 * 등록된 물질 목록 반환
 */
export function getSubstanceList(): string[] {
  return Object.keys(SUBSTANCE_DATA);
}
