<commit_before>const meta = {
    loaded: false,
};

function change_display_setting(data, status_element, success_msg, sticky) {
    const $status_el = $(status_element);
    const status_is_sticky = $status_el.data('is_sticky');
    const display_message = status_is_sticky ? $status_el.data('sticky_msg') : success_msg;
    const opts = {
        success_msg: display_message,
        sticky: status_is_sticky || sticky,
    };

    if (sticky) {
        $status_el.data('is_sticky', true);
        $status_el.data('sticky_msg', success_msg);
    }
    settings_ui.do_settings_change(channel.patch, '/json/settings/display', data, status_element, opts);
}

exports.demote_inactive_streams_values = {
    automatic: {
        code: 1,
        description: i18n.t("Automatic"),
    },
    always: {
        code: 2,
        description: i18n.t("Always"),
    },
    never: {
        code: 3,
        description: i18n.t("Never"),
    },
};

exports.twenty_four_hour_time_values = {
    twenty_four_hour_clock: {
        value: true,
        description: i18n.t("24-hour clock (17:00)"),
    },
    twelve_hour_clock: {
        value: false,
        description: i18n.t("12-hour clock (5:00 PM)"),
    },
};

exports.all_display_settings = {
    settings: {
        user_display_settings: [
            "dense_mode",
            "night_mode",
            "high_contrast_mode",
            "left_side_userlist",
            "starred_message_counts",
            "fluid_layout_width",
        ],
    },
    render_only: {
        high_contrast_mode: page_params.development_environment,
        dense_mode: page_params.development_environment,
    },
};

exports.set_up = function () {
    meta.loaded = true;
    $("#display-settings-status").hide();

    $("#user_timezone").val(page_params.timezone);

    $("#demote_inactive_streams").val(page_params.demote_inactive_streams);

    $("#twenty_four_hour_time").val(JSON.stringify(page_params.twenty_four_hour_time));

    $(".emojiset_choice[value=" + page_params.emojiset + "]").prop("checked", true);

    $("#default_language_modal [data-dismiss]").click(function () {
        overlays.close_modal('default_language_modal');
    });

    _.each(exports.all_display_settings.settings.user_display_settings, function (setting) {
        $("#" + setting).change(function () {
            const data = {};
            data[setting] = JSON.stringify($(this).prop('checked'));

            if (["left_side_userlist"].indexOf(setting) > -1) {
                change_display_setting(
                    data,
                    "#display-settings-status",
                    i18n.t("Saved. Please <a class='reload_link'>reload</a> for the change to take effect."), true);
            } else {
                change_display_setting(data, "#display-settings-status");
            }
        });
    });

    $("#default_language_modal .language").click(function (e) {
        e.preventDefault();
        e.stopPropagation();
        overlays.close_modal('default_language_modal');

        const $link = $(e.target).closest("a[data-code]");
        const setting_value = $link.attr('data-code');
        const data = {default_language: JSON.stringify(setting_value)};

        const new_language = $link.attr('data-name');
        $('#default_language_name').text(new_language);

        change_display_setting(data, '#language-settings-status',
                               i18n.t("Saved. Please <a class='reload_link'>reload</a> for the change to take effect."), true);

    });

    $('#default_language').on('click', function (e) {
        e.preventDefault();
        e.stopPropagation();
        overlays.open_modal('default_language_modal');
    });

    $('#demote_inactive_streams').change(function () {
        const data = {demote_inactive_streams: this.value};
        change_display_setting(data, '#display-settings-status');
    });

    $('body').on('click', '.reload_link', function () {
        window.location.reload();
    });


    $("#twenty_four_hour_time").change(function () {
        const data = {twenty_four_hour_time: this.value};
        change_display_setting(data, '#time-settings-status');
    });

    $("#user_timezone").change(function () {
        const data = {timezone: JSON.stringify(this.value)};
        change_display_setting(data, '#time-settings-status');
    });
    $(".emojiset_choice").click(function () {
        const data = {emojiset: JSON.stringify($(this).val())};
        const spinner = $("#emoji-settings-status").expectOne();
        loading.make_indicator(spinner, {text: settings_ui.strings.saving });

        channel.patch({
            url: '/json/settings/display',<commit_msg>settings: Fix buggy emoji format loading spinner.<commit_after>const meta = {
    loaded: false,
};

function change_display_setting(data, status_element, success_msg, sticky) {
    const $status_el = $(status_element);
    const status_is_sticky = $status_el.data('is_sticky');
    const display_message = status_is_sticky ? $status_el.data('sticky_msg') : success_msg;
    const opts = {
        success_msg: display_message,
        sticky: status_is_sticky || sticky,
    };

    if (sticky) {
        $status_el.data('is_sticky', true);
        $status_el.data('sticky_msg', success_msg);
    }
    settings_ui.do_settings_change(channel.patch, '/json/settings/display', data, status_element, opts);
}

exports.demote_inactive_streams_values = {
    automatic: {
        code: 1,
        description: i18n.t("Automatic"),
    },
    always: {
        code: 2,
        description: i18n.t("Always"),
    },
    never: {
        code: 3,
        description: i18n.t("Never"),
    },
};

exports.twenty_four_hour_time_values = {
    twenty_four_hour_clock: {
        value: true,
        description: i18n.t("24-hour clock (17:00)"),
    },
    twelve_hour_clock: {
        value: false,
        description: i18n.t("12-hour clock (5:00 PM)"),
    },
};

exports.all_display_settings = {
    settings: {
        user_display_settings: [
            "dense_mode",
            "night_mode",
            "high_contrast_mode",
            "left_side_userlist",
            "starred_message_counts",
            "fluid_layout_width",
        ],
    },
    render_only: {
        high_contrast_mode: page_params.development_environment,
        dense_mode: page_params.development_environment,
    },
};

exports.set_up = function () {
    meta.loaded = true;
    $("#display-settings-status").hide();

    $("#user_timezone").val(page_params.timezone);

    $("#demote_inactive_streams").val(page_params.demote_inactive_streams);

    $("#twenty_four_hour_time").val(JSON.stringify(page_params.twenty_four_hour_time));

    $(".emojiset_choice[value=" + page_params.emojiset + "]").prop("checked", true);

    $("#default_language_modal [data-dismiss]").click(function () {
        overlays.close_modal('default_language_modal');
    });

    _.each(exports.all_display_settings.settings.user_display_settings, function (setting) {
        $("#" + setting).change(function () {
            const data = {};
            data[setting] = JSON.stringify($(this).prop('checked'));

            if (["left_side_userlist"].indexOf(setting) > -1) {
                change_display_setting(
                    data,
                    "#display-settings-status",
                    i18n.t("Saved. Please <a class='reload_link'>reload</a> for the change to take effect."), true);
            } else {
                change_display_setting(data, "#display-settings-status");
            }
        });
    });

    $("#default_language_modal .language").click(function (e) {
        e.preventDefault();
        e.stopPropagation();
        overlays.close_modal('default_language_modal');

        const $link = $(e.target).closest("a[data-code]");
        const setting_value = $link.attr('data-code');
        const data = {default_language: JSON.stringify(setting_value)};

        const new_language = $link.attr('data-name');
        $('#default_language_name').text(new_language);

        change_display_setting(data, '#language-settings-status',
                               i18n.t("Saved. Please <a class='reload_link'>reload</a> for the change to take effect."), true);

    });

    $('#default_language').on('click', function (e) {
        e.preventDefault();
        e.stopPropagation();
        overlays.open_modal('default_language_modal');
    });

    $('#demote_inactive_streams').change(function () {
        const data = {demote_inactive_streams: this.value};
        change_display_setting(data, '#display-settings-status');
    });

    $('body').on('click', '.reload_link', function () {
        window.location.reload();
    });


    $("#twenty_four_hour_time").change(function () {
        const data = {twenty_four_hour_time: this.value};
        change_display_setting(data, '#time-settings-status');
    });

    $("#user_timezone").change(function () {
        const data = {timezone: JSON.stringify(this.value)};
        change_display_setting(data, '#time-settings-status');
    });
    $(".emojiset_choice").click(function () {
        const data = {emojiset: JSON.stringify($(this).val())};
        const current_emojiset = JSON.stringify(page_params.emojiset);
        if (current_emojiset === data.emojiset) {
            return;
        }
        const spinner = $("#emoji-settings-status").expectOne();
        loading.make_indicator(spinner, {text: settings_ui.strings.saving });

        channel.patch({
            url: '/json/settings/display',
{'fraction_non_alphanumeric': 0.15110321028965806, 'fraction_numerical': 0.00606504235072676, 'mean_word_length': 2.7802371541501976, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '944999', 'n_tokens_mistral': 2972, 'n_tokens_neox': 2904, 'n_words': 422}