#!/usr/bin/env python3

# VALUES_I is holding final values (weights), VALUES_C is holding original
# Unicode codepoints for collision detection

import sys
import mph


def usage():
	print(f'usage: {sys.argv[0]} [TAG] [BMP_ONLY]')
	print('')
	print('  [TAG]      - tag to use to prefix hashtables')
	print('  [BMP_ONLY] - flag to indicate if set is BMP-only, 0 or 1 (false or true)')


if __name__ == '__main__':
	if len(sys.argv) < 3:
		usage()
		sys.exit(1)

	TAG = sys.argv[1]
	BMP_ONLY = bool(int(sys.argv[2]))

	d: mph.Table = {}

	for i, line in enumerate(sys.stdin):
		assert isinstance(line, str)

		codepoint, replacement = (x for x in line.strip().split(' ') if x)

		if not replacement:
			continue

		replacement = int(replacement.strip(), base=16)
		d[codepoint] = (codepoint, replacement)

	(G, V) = mph.create_minimal_perfect_hash(d)

	assert len(G) == len(V)

	mph.gen_header(TAG, G, "")
	mph.gen_includes()
	mph.gen_G(TAG, G)
	mph.gen_values(TAG, V, BMP_ONLY)
