(function () {
    /**
     * Format the patron response from a Koha RESTful API request.
     * @param  {Object}  patron  The patron json object as returned from the Koha RESTful API
     * @param  {Object}  config  A configuration object
     *                           Valid keys are: `invert_name`, `display_cardnumber', `url`
     *                           and `showDiffFirstname`
     * @return {string}          The formatted HTML string
     */
    window.$patron_to_html = function (patron, config) {
        if (patron == null) {
            return ""; // empty string for no patron
        }

        var title = null;
        if (patron.title != null && patron.title != "") {
            title =
                '<span class="patron-title">' +
                escape_str(patron.title) +
                "</span>";
        }

        let name;
        var firstname = escape_str(patron.firstname);
        var preferred_name = escape_str(patron.preferred_name);
        var surname = escape_str(patron.surname);

        if (patron.middle_name != null && patron.middle_name != "") {
            firstname += " " + escape_str(patron.middle_name);
            preferred_name += " " + escape_str(patron.middle_name);
        }

        if (patron.other_name != null && patron.other_name != "") {
            firstname += " (" + escape_str(patron.other_name) + ")";
            preferred_name += " (" + escape_str(patron.other_name) + ")";
        }

        if (
            config &&
            config.showDiffFirstname &&
            patron.preferred_name != patron.firstname
        ) {
            preferred_name +=
                " <em>[" + escape_str(patron.firstname) + "]</em>";
        }

        const nameParts = [];
        if (config?.invert_name) {
            // Invert: "Surname, Preferred"
            if (surname) nameParts.push(surname);
            if (preferred_name)
                nameParts.push(
                    surname ? ", " + preferred_name : preferred_name
                );
            name = nameParts.join("");
        } else {
            // Normal: "Preferred Surname"
            if (preferred_name) nameParts.push(preferred_name);
            if (surname) nameParts.push(surname);
            name = nameParts.join(" ");
        }

        if (name.replace(" ", "").length == 0) {
            if (patron.library) {
                return __("A patron from %s").format(patron.library.name);
            } else {
                return __("A patron from another library");
            }
        }

        if (config?.hide_patron_name) {
            name = "";
        }

        if (config?.display_cardnumber) {
            if (name.length > 0) {
                name = name + " (" + escape_str(patron.cardnumber) + ")";
            } else {
                name = escape_str(patron.cardnumber);
            }
        }

        if (config?.url) {
            if (config.url === "circulation_reserves") {
                name =
                    '<a href="/cgi-bin/koha/circ/circulation.pl?borrowernumber=' +
                    encodeURIComponent(patron.patron_id) +
                    '#reserves">' +
                    name +
                    "</a>";
            } else {
                name =
                    '<a href="/cgi-bin/koha/members/moremember.pl?borrowernumber=' +
                    encodeURIComponent(patron.patron_id) +
                    '">' +
                    name +
                    "</a>";
            }
        }

        return name;
    };
})();
