/*
  Copyright:        Copyright (C) 2022 SIL International.
  Authors:          srl295
  This tool generates a .h version of the keyboardprocessor_ldml.ts file
*/

import { constants } from './keyboardprocessor_ldml';

const keys = Object.keys(constants);
keys.sort();
console.log(`
/*
  Copyright:        Copyright (C) 2022 SIL International.
  Authors:          srl295
  This file provides constants for the KMX Plus (LDML support) binary format,
  to be shared between TypeScript and C++ via the generator (below)
*/

//
// Generated File - do not edit
//
// This file is generated by core/tools/ldml-const-builder/build.sh
// based on core/include/ldml/keyboardprocessor_ldml.ts
//

#pragma once
`);

for (const key of keys) {
    const value = constants[key];
    const upkey = key.toUpperCase();
    const type = typeof value;
    if (type === 'number') {
        console.log(`#define LDML_${upkey} 0x${value.toString(16).toUpperCase()}`);
    } else if (type === 'string') {
        console.log(`#define LDML_${upkey} "${value}"`);
    } else if (key === 'section') {
        // handle section table
        const subkeys = Object.keys(value);
        subkeys.sort();
        for (const subkey of subkeys) {
            const upsubkey = subkey.toUpperCase();
            const subvalue = subkeys[subkey];
            const asnum = constants.hex_section_id(subkey);
            console.log(`#define LDML_${upkey}ID_${upsubkey} 0x${asnum.toString(16).toUpperCase()} /* "${subkey}" */`);
            console.log(`#define LDML_${upkey}NAME_${upsubkey}             "${subkey}"`);
        }
    } else if (type !== 'function') {
        console.error(`Unrecognized key ${key}`);
    }
}
