import { to_title_case } from './ae_utils__to_title_case';

// Updated 2026-01-22
export function set_obj_prop_display_name({
    prop_name,
    obj_type = null,
    prefix_w_obj_type = true,
    prefix_all_w_obj_type = false,
    replace_underscores = true,
    title_case = true,
    override = null
}: {
    prop_name: string;
    obj_type?: string | null;
    prefix_w_obj_type?: boolean;
    prefix_all_w_obj_type?: boolean;
    replace_underscores?: boolean;
    title_case?: boolean;
    override?: string | null;
}) {
    console.log('*** set_obj_prop_display_name() ***');

    if (override) {
        return override;
    }

    const known_obj_type_li = [
        'account',
        'address',
        'contact',
        'event_badge',
        'event_exhibit',
        'event_file',
        'event_location',
        'event_person',
        'event_presentation',
        'event_presenter',
        'event_registration',
        'event_session',
        'event',
        'hosted_file',
        'order_line',
        'order',
        'person',
        'user'
    ];

    const known_obj_type_li_dict = [
        { name: 'account', display: 'Account' },
        { name: 'address', display: 'Address' },
        { name: 'contact', display: 'Contact' },
        { name: 'event_badge', display: 'Event Badge' },
        { name: 'event_exhibit', display: 'Event Exhibit' },
        { name: 'event_file', display: 'Event File' },
        { name: 'event_location', display: 'Event Location' },
        { name: 'event_person', display: 'Event Person' },
        { name: 'event_presentation', display: 'Event Presentation' },
        { name: 'event_presenter', display: 'Event Presenter' },
        { name: 'event_registration', display: 'Event Registration' },
        { name: 'event_session', display: 'Event Session' },
        { name: 'event', display: 'Event' },
        { name: 'hosted_file', display: 'Hosted File' },
        { name: 'order_line', display: 'Order Line' },
        { name: 'order', display: 'Order' },
        { name: 'person', display: 'Person' },
        { name: 'user', display: 'User' }
    ];

    let prop_display_name = prop_name;

    if (!prefix_w_obj_type) {
        if (obj_type) {
            prop_display_name = prop_name.replace(obj_type, '');
        } else {
            for (let i = 0; i < known_obj_type_li_dict.length; i++) {
                // console.log(known_obj_type_li_dict[i]);
                if (prop_name.startsWith(known_obj_type_li_dict[i].name)) {
                    // console.log(`Found ${known_obj_type_li_dict[i].name}`);
                    prop_display_name = prop_name.replace(
                        known_obj_type_li_dict[i].name,
                        ''
                    );
                    break;
                }
            }

            // for (let i = 0; i < known_obj_type_li.length; i++) {
            //     // console.log(known_obj_type_li[i]);
            //     if (prop_name.startsWith(known_obj_type_li[i])) {
            //         console.log(`Found ${known_obj_type_li[i]}`);
            //         prop_display_name = prop_name.replace(known_obj_type_li[i], '');
            //         break;
            //     }
            // }
        }
    } else {
        if (!prefix_all_w_obj_type) {
            for (let i = 0; i < known_obj_type_li.length; i++) {
                // console.log(known_obj_type_li[i]);
                let found_obj_type = null;

                if (prop_name.startsWith(known_obj_type_li_dict[i].name)) {
                    // if (prop_name.startsWith(known_obj_type_li[i])) {
                    // console.log(`Found ${known_obj_type_li_dict[i].name}`);
                    found_obj_type = known_obj_type_li_dict[i].name;
                    if (found_obj_type == obj_type) {
                        prop_display_name = prop_name.replace(
                            known_obj_type_li_dict[i].name,
                            ''
                        );
                    }

                    break;
                }
            }

            // if (obj_type) {
            //     prop_display_name = prop_name.replace(obj_type, '');
            // } else {
        }
    }

    // console.log(prop_display_name);
    if (prop_display_name.search('ID')) {
    }
    prop_display_name = prop_display_name.replace('id_random', 'ID');

    if (replace_underscores) {
        prop_display_name = prop_display_name.replaceAll('_', ' ');
    }

    if (title_case) {
        prop_display_name = to_title_case(prop_display_name);
    }

    // console.log(prop_display_name);
    return prop_display_name;
}
