type key_val = {
    [key: string]: any;
};

/* This utility function looks for any form data with the prefixed name passed and returns a new object.
 * This function is used heavily! Be very careful making changes!!!
 * If rm_empty_id then it will remove/ignore fields matching. This helps with the API and new records/objects
 * If rm_empty then it will remove/ignore fields matching. Sometimes this is needed.
 * If trim_values then it will trim string values.
 * If bool_tf_str then it will convert string values of true/false (case insensitive) to boolean values.
 * REMINDER: An unchecked checkbox will not be sent in the form data. This is a browser thing.
 * Updated 2023-12-22
 */
export const extract_prefixed_form_data = function extract_prefixed_form_data({
    prefix = null,
    form_data = {},
    rm_empty_id = true,
    rm_empty = false,
    trim_values = false,
    bool_tf_str = false,
    log_lvl = 0
}: {
    prefix: string | null;
    form_data: any;
    rm_empty_id?: boolean;
    rm_empty?: boolean;
    trim_values?: boolean;
    bool_tf_str?: boolean;
    log_lvl?: number;
}) {
    if (log_lvl) {
        console.log('*** extract_prefixed_form_data() ***');
        if (prefix) {
            console.log(
                `Looking for prefixed fields: ${prefix}; Removing emptry ID fields: ${rm_empty_id}; Removing empty fields: ${rm_empty}; Trim string values: ${trim_values}; Convert true/false string values to boolean: ${bool_tf_str}`
            );
        } else {
            console.log(
                `No prefix set. Looking at all fields. Removing emptry ID fields: ${rm_empty_id}; Removing empty fields: ${rm_empty}; Trim string values: ${trim_values}; Convert true/false string values to boolean: ${bool_tf_str}`
            );
        }
    }
    if (log_lvl > 1) {
        console.log('Form Data:');
        console.log(form_data);
    }

    // const data_obj: any = {}; // future TS
    const data_obj: key_val = {};
    for (const field of form_data) {
        let [obj_prop_name, obj_prop_value] = field;
        if (log_lvl > 1) {
            console.log(
                `${obj_prop_name}: ${obj_prop_value} type=${typeof obj_prop_value}`
            );
        }

        // Trim string values if needed
        if (trim_values && typeof obj_prop_value === 'string') {
            if (log_lvl && obj_prop_value.trim() != obj_prop_value) {
                console.log('Trimming string value!');
                obj_prop_value = obj_prop_value.trim();
            }
        }

        // Convert string to boolean if needed
        if (bool_tf_str && typeof obj_prop_value === 'string') {
            // console.log('Flag set for converting true/false string values to boolean!');

            if (obj_prop_value.toLowerCase() === 'true') {
                if (log_lvl) {
                    console.log('Converting string to boolean value: true');
                }
                obj_prop_value = true;
            } else if (obj_prop_value.toLowerCase() === 'false') {
                if (log_lvl) {
                    console.log('Converting string to boolean value: false');
                }
                obj_prop_value = false;
            }
        }

        if (prefix && obj_prop_name.startsWith(prefix)) {
            // Prefix set
            // if (obj_prop_name.startsWith(prefix)) {
            obj_prop_name = obj_prop_name.replace(prefix, '');
            if (log_lvl) {
                console.log(
                    `Checking: (${prefix})${obj_prop_name} value=${obj_prop_value}`
                );
            }
            if (
                rm_empty_id &&
                obj_prop_name.endsWith('id_random') &&
                !obj_prop_value
            ) {
                if (log_lvl) {
                    console.log(
                        `Match but empty *_id_random. Ignoring/removing: ${obj_prop_name}`
                    );
                }
            } else if (rm_empty && !obj_prop_value) {
                if (log_lvl) {
                    console.log(
                        `Match but empty. Ignoring/removing: ${obj_prop_name}`
                    );
                }
            } else {
                if (log_lvl) {
                    console.log(
                        `Match: ${prefix})${obj_prop_name} value=${obj_prop_value}`
                    );
                }
                data_obj[obj_prop_name] = obj_prop_value;
            }
        } else if (prefix && !obj_prop_name.startsWith(prefix)) {
            // Prefix set
            if (log_lvl > 1) {
                console.log('Did not start with prefix. Ignoring');
            }
        } else {
            // No prefix set
            if (log_lvl) {
                console.log(
                    `Checking: ${obj_prop_name} value=${obj_prop_value}`
                );
            }
            if (
                rm_empty_id &&
                obj_prop_name.endsWith('id_random') &&
                !obj_prop_value
            ) {
                if (log_lvl > 1) {
                    console.log(
                        `Match but empty *_id_random. Ignoring/removing: ${obj_prop_name}`
                    );
                }
            } else if (rm_empty && !obj_prop_value) {
                if (log_lvl > 1) {
                    console.log(
                        `Match but empty. Ignoring/removing: ${obj_prop_name}`
                    );
                }
            } else {
                if (log_lvl > 1) {
                    console.log(
                        `Match: ${obj_prop_name} value=${obj_prop_value}`
                    );
                }
                data_obj[obj_prop_name] = obj_prop_value;
            }
        }
    }
    if (log_lvl > 1) {
        console.log(data_obj);
    }
    return data_obj;
};
