import type { key_val } from './ae_utils';

/* This utility function processes specific data string.
 * MECARD
 * OBJ = OBJ:ot:example,oi:asdf1234
 * ot = Aether object type; oi = Aether object ID random
 * KV = KV:"key":"value"
 * JS = {"id":123,"name":"example name"}
 * http or https?
 * Common short keys used:
 * bdg: Badge ID Random
 * reg: Registration ID Random
 * exid: External ID
 * gn: Given First Name
 * fn: Family Last Name
 * em: Email Address
 */
// Updated 2022-02-11

export const process_data_string = function process_data_string(
    data_string: string
) {
    console.log('*** process_data_string() ***');
    // console.log(data_string);
    if (!data_string || data_string.length < 1) {
        console.log('No data string found.');
        return false;
    }

    const obj: key_val = {};

    const colon_index = data_string.indexOf(':');
    if (colon_index) {
        const data_string_type = data_string.slice(0, colon_index);
        console.log(data_string_type);

        obj['qr_type'] = data_string_type;

        if (data_string_type == 'MECARD') {
            const mecard_str = data_string.slice(colon_index + 1);
            console.log(mecard_str);

            obj['str'] = mecard_str;
        } else if (data_string_type == 'OBJ') {
            const key_value_str = data_string.slice(colon_index + 1);
            console.log(key_value_str);
            const key_value_array = key_value_str.split(',');
            // console.log(key_value_array);
            const ot_colon_index = key_value_array[0].indexOf(':');
            const obj_type = key_value_array[0].slice(ot_colon_index + 1);
            // console.log(obj_type);
            const oi_colon_index = key_value_array[1].indexOf(':');
            const obj_id = key_value_array[1].slice(oi_colon_index + 1);
            // console.log(obj_id);
            obj['type'] = obj_type;
            obj['id'] = obj_id;
        } else if (data_string_type == 'JSON') {
            const partial_json_str = data_string.slice(colon_index + 1);
            console.log(partial_json_str);
            const json_str = `{${partial_json_str}}`;
            console.log(json_str);

            obj['json'] = JSON.parse(json_str);
        } else if (data_string_type == 'STR') {
            const str = data_string.slice(colon_index + 1);
            console.log(str);

            obj['str'] = str;
        } else if (data_string_type == 'http' || data_string_type == 'https') {
            console.log(`http or https: ${data_string}`);

            obj['type'] = 'url';
            obj['url'] = data_string;
        } else {
            console.log(
                'The unknown data string type was found. Returning the string part.'
            );
            const unknown_str = data_string.slice(colon_index + 1);
            console.log(unknown_str);

            obj['str'] = unknown_str;
        }
    } else {
        console.log(
            'The data string type was not found. Returning the entire string.'
        );
        console.log(data_string);

        obj['qr_type'] = 'UNKNOWN';
        obj['str'] = data_string;
        // return false;
    }

    console.log(obj);
    return obj; // Returns an object
};
