<commit_before>const IntDict = require("./int_dict").IntDict;

const render_settings_custom_user_profile_field = require("../templates/settings/custom_user_profile_field.hbs");
const render_settings_dev_env_email_access = require('../templates/settings/dev_env_email_access.hbs');
const render_settings_api_key_modal = require('../templates/settings/api_key_modal.hbs');

exports.update_email = function (new_email) {
    const email_input = $('#email_value');

    if (email_input) {
        email_input.text(new_email);
    }
};

exports.update_full_name = function (new_full_name) {
    const full_name_field = $("#change_full_name button #full_name_value");
    if (full_name_field) {
        full_name_field.text(new_full_name);
    }

    // Arguably, this should work more like how the `update_email`
    // flow works, where we update the name in the modal on open,
    // rather than updating it here, but this works.
    const full_name_input = $(".full_name_change_container input[name='full_name']");
    if (full_name_input) {
        full_name_input.val(new_full_name);
    }
};

exports.user_can_change_name = function () {
    if (page_params.is_admin) {
        return true;
    }
    if (page_params.realm_name_changes_disabled || page_params.server_name_changes_disabled) {
        return false;
    }
    return true;
};

exports.user_can_change_avatar = function () {
    if (page_params.is_admin) {
        return true;
    }
    if (page_params.realm_avatar_changes_disabled || page_params.server_avatar_changes_disabled) {
        return false;
    }
    return true;
};

exports.update_name_change_display = function () {
    if (!exports.user_can_change_name()) {
        $('#full_name').attr('disabled', 'disabled');
        $(".change_name_tooltip").show();
    } else {
        $('#full_name').attr('disabled', false);
        $(".change_name_tooltip").hide();
    }
};

exports.update_email_change_display = function () {
    if (page_params.realm_email_changes_disabled && !page_params.is_admin) {
        $('#change_email .button').attr('disabled', 'disabled');
        $(".change_email_tooltip").show();
    } else {
        $('#change_email .button').attr('disabled', false);
        $(".change_email_tooltip").hide();
    }
};

exports.update_avatar_change_display = function () {
    if (!exports.user_can_change_avatar()) {
        $('#user_avatar_upload_button .button').attr('disabled', 'disabled');
        $('#user_avatar_delete_button .button').attr('disabled', 'disabled');
    } else {
        $('#user_avatar_upload_button .button').attr('disabled', false);
        $('#user_avatar_delete_button .button').attr('disabled', false);
    }
};

function settings_change_error(message, xhr) {
    ui_report.error(message, xhr, $('#account-settings-status').expectOne());
}

function update_custom_profile_field(field, method) {
    let field_id;
    if (method === channel.del) {
        field_id = field;
    } else {
        field_id = field.id;
    }

    const spinner = $('.custom_user_field[data-field-id="' + field_id +
        '"] .custom-field-status').expectOne();
    loading.make_indicator(spinner, {text: 'Saving ...'});
    settings_ui.do_settings_change(method, "/json/users/me/profile_data",
                                   {data: JSON.stringify([field])}, spinner);
}

function update_user_custom_profile_fields(fields, method) {
    if (method === undefined) {
        blueslip.error("Undefined method in update_user_custom_profile_fields");
    }

    for (const field of fields) {
        update_custom_profile_field(field, method);
    }
}

exports.append_custom_profile_fields = function (element_id, user_id) {
    const person = people.get_by_user_id(user_id);
    if (person.is_bot) {
        return;
    }
    const all_custom_fields = page_params.custom_profile_fields;
    const all_field_types = page_params.custom_profile_field_types;

    const all_field_template_types = {};
    all_field_template_types[all_field_types.LONG_TEXT.id] = "text";
    all_field_template_types[all_field_types.SHORT_TEXT.id] = "text";
    all_field_template_types[all_field_types.CHOICE.id] = "choice";
    all_field_template_types[all_field_types.USER.id] = "user";
    all_field_template_types[all_field_types.DATE.id] = "date";
    all_field_template_types[all_field_types.EXTERNAL_ACCOUNT.id] = "text";
    all_field_template_types[all_field_types.URL.id] = "url";

    all_custom_fields.forEach(function (field) {
        let field_value = people.get_custom_profile_data(user_id, field.id);
        const is_choice_field = field.type === all_field_types.CHOICE.id;
        const field_choices = [];

        if (field_value === undefined || field_value === null) {
            field_value = {value: "", rendered_value: ""};
        }
        if (is_choice_field) {
            const field_choice_dict = JSON.parse(field.field_data);
            for (const choice in field_choice_dict) {
                if (choice) {
                    field_choices[field_choice_dict[choice].order] = {
                        value: choice,
                        text: field_choice_dict[choice].text,
                        selected: choice === field_value.value,
                    };
                }
            }
        }

        const html = render_settings_custom_user_profile_field({
            field: field,
            field_type: all_field_template_types[field.type],
            field_value: field_value,
            is_long_text_field: field.type === all_field_types.LONG_TEXT.id,
            is_user_field: field.type === all_field_types.USER.id,
            is_date_field: field.type === all_field_types.DATE.id,
            is_choice_field: is_choice_field,
            field_choices: field_choices,
        });
        $(element_id).append(html);
    });
};

exports.initialize_custom_date_type_fields = function (element_id) {
    $(element_id).find(".custom_user_field .datepicker").flatpickr({
        altInput: true,<commit_msg>settings_account: Convert all_field_template_types from object to Map.<commit_after>const IntDict = require("./int_dict").IntDict;

const render_settings_custom_user_profile_field = require("../templates/settings/custom_user_profile_field.hbs");
const render_settings_dev_env_email_access = require('../templates/settings/dev_env_email_access.hbs');
const render_settings_api_key_modal = require('../templates/settings/api_key_modal.hbs');

exports.update_email = function (new_email) {
    const email_input = $('#email_value');

    if (email_input) {
        email_input.text(new_email);
    }
};

exports.update_full_name = function (new_full_name) {
    const full_name_field = $("#change_full_name button #full_name_value");
    if (full_name_field) {
        full_name_field.text(new_full_name);
    }

    // Arguably, this should work more like how the `update_email`
    // flow works, where we update the name in the modal on open,
    // rather than updating it here, but this works.
    const full_name_input = $(".full_name_change_container input[name='full_name']");
    if (full_name_input) {
        full_name_input.val(new_full_name);
    }
};

exports.user_can_change_name = function () {
    if (page_params.is_admin) {
        return true;
    }
    if (page_params.realm_name_changes_disabled || page_params.server_name_changes_disabled) {
        return false;
    }
    return true;
};

exports.user_can_change_avatar = function () {
    if (page_params.is_admin) {
        return true;
    }
    if (page_params.realm_avatar_changes_disabled || page_params.server_avatar_changes_disabled) {
        return false;
    }
    return true;
};

exports.update_name_change_display = function () {
    if (!exports.user_can_change_name()) {
        $('#full_name').attr('disabled', 'disabled');
        $(".change_name_tooltip").show();
    } else {
        $('#full_name').attr('disabled', false);
        $(".change_name_tooltip").hide();
    }
};

exports.update_email_change_display = function () {
    if (page_params.realm_email_changes_disabled && !page_params.is_admin) {
        $('#change_email .button').attr('disabled', 'disabled');
        $(".change_email_tooltip").show();
    } else {
        $('#change_email .button').attr('disabled', false);
        $(".change_email_tooltip").hide();
    }
};

exports.update_avatar_change_display = function () {
    if (!exports.user_can_change_avatar()) {
        $('#user_avatar_upload_button .button').attr('disabled', 'disabled');
        $('#user_avatar_delete_button .button').attr('disabled', 'disabled');
    } else {
        $('#user_avatar_upload_button .button').attr('disabled', false);
        $('#user_avatar_delete_button .button').attr('disabled', false);
    }
};

function settings_change_error(message, xhr) {
    ui_report.error(message, xhr, $('#account-settings-status').expectOne());
}

function update_custom_profile_field(field, method) {
    let field_id;
    if (method === channel.del) {
        field_id = field;
    } else {
        field_id = field.id;
    }

    const spinner = $('.custom_user_field[data-field-id="' + field_id +
        '"] .custom-field-status').expectOne();
    loading.make_indicator(spinner, {text: 'Saving ...'});
    settings_ui.do_settings_change(method, "/json/users/me/profile_data",
                                   {data: JSON.stringify([field])}, spinner);
}

function update_user_custom_profile_fields(fields, method) {
    if (method === undefined) {
        blueslip.error("Undefined method in update_user_custom_profile_fields");
    }

    for (const field of fields) {
        update_custom_profile_field(field, method);
    }
}

exports.append_custom_profile_fields = function (element_id, user_id) {
    const person = people.get_by_user_id(user_id);
    if (person.is_bot) {
        return;
    }
    const all_custom_fields = page_params.custom_profile_fields;
    const all_field_types = page_params.custom_profile_field_types;

    const all_field_template_types = new Map([
        [all_field_types.LONG_TEXT.id, "text"],
        [all_field_types.SHORT_TEXT.id, "text"],
        [all_field_types.CHOICE.id, "choice"],
        [all_field_types.USER.id, "user"],
        [all_field_types.DATE.id, "date"],
        [all_field_types.EXTERNAL_ACCOUNT.id, "text"],
        [all_field_types.URL.id, "url"],
    ]);

    all_custom_fields.forEach(function (field) {
        let field_value = people.get_custom_profile_data(user_id, field.id);
        const is_choice_field = field.type === all_field_types.CHOICE.id;
        const field_choices = [];

        if (field_value === undefined || field_value === null) {
            field_value = {value: "", rendered_value: ""};
        }
        if (is_choice_field) {
            const field_choice_dict = JSON.parse(field.field_data);
            for (const choice in field_choice_dict) {
                if (choice) {
                    field_choices[field_choice_dict[choice].order] = {
                        value: choice,
                        text: field_choice_dict[choice].text,
                        selected: choice === field_value.value,
                    };
                }
            }
        }

        const html = render_settings_custom_user_profile_field({
            field: field,
            field_type: all_field_template_types.get(field.type),
            field_value: field_value,
            is_long_text_field: field.type === all_field_types.LONG_TEXT.id,
            is_user_field: field.type === all_field_types.USER.id,
            is_date_field: field.type === all_field_types.DATE.id,
            is_choice_field: is_choice_field,
            field_choices: field_choices,
        });
        $(element_id).append(html);
    });
};

exports.initialize_custom_date_type_fields = function (element_id) {
    $(element_id).find(".custom_user_field .datepicker").flatpickr({
        altInput: true,
{'fraction_non_alphanumeric': 0.12749371332774517, 'fraction_numerical': 0.0, 'mean_word_length': 3.111302549965541, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4202279', 'n_tokens_mistral': 3701, 'n_tokens_neox': 3674, 'n_words': 581}