import assert from 'node:assert/strict';
import test from 'node:test';
import { MINISTRY_TRANSLATIONS } from './index';
import { MINISTRY_PLENARY_LABELS } from './ministry-plenary-labels.seed';
import {
  ministryPlenaryDefaults,
  ministryPlenaryPage,
} from '../../../../frontend/features/ministry/plenary/plenary-labels';
import { reportTranslationKey } from '../../../../frontend/features/pswg/progress-report/progress-report-translations';

function flat(value: object, prefix = ''): Record<string, string> {
  return Object.assign(
    {},
    ...Object.entries(value).map(([key, child]) => {
      const path = prefix ? `${prefix}.${key}` : key;
      return typeof child === 'string'
        ? { [reportTranslationKey(path)]: child }
        : flat(child, path);
    }),
  );
}

test('both Ministry Plenary sub-pages own every bilingual live UI label', () => {
  const en = flat(ministryPlenaryDefaults('en'));
  const kh = flat(ministryPlenaryDefaults('kh'));
  assert.deepEqual(Object.keys(en), Object.keys(kh));
  assert.equal(Object.keys(en).length, MINISTRY_PLENARY_LABELS.length);
  for (const page of ['plenary_plenaries', 'plenary_rgc_decisions']) {
    const rows = MINISTRY_TRANSLATIONS.filter((row) => row.page === page);
    assert.equal(
      new Set(rows.map((row) => row.translationKey)).size,
      rows.length,
    );
    for (const key of Object.keys(en)) {
      const row = rows.find((item) => item.translationKey === key);
      assert.ok(row, `${page}/${key}`);
      assert.ok(row.englishText.trim());
      assert.ok(row.khmerText.trim());
    }
  }
});

test('route selects Plenaries and top-level RGC Decisions dictionaries independently', () => {
  assert.equal(
    ministryPlenaryPage('/ministry/plenary/plenaries'),
    'plenary_plenaries',
  );
  assert.equal(
    ministryPlenaryPage('/ministry/plenary/plenaries/7'),
    'plenary_plenaries',
  );
  assert.equal(
    ministryPlenaryPage('/ministry/plenary/plenaries/7/rgc-decisions/2'),
    'plenary_plenaries',
  );
  assert.equal(
    ministryPlenaryPage('/ministry/plenary/rgc-decisions'),
    'plenary_rgc_decisions',
  );
  assert.equal(
    ministryPlenaryPage('/ministry/plenary/rgc-decisions/2'),
    'plenary_rgc_decisions',
  );
});
