var jQueryWeb357LoginAsUserVirtueMart = jQuery.noConflict();
jQueryWeb357LoginAsUserVirtueMart(function ($) {
    /**
    VirtueMart's Users' page
    */
    // Login as user button
    $(
        ".admin.com_virtuemart.view-user form[action='/administrator/index.php?option=com_virtuemart&view=user'] table tbody, .admin.com_virtuemart.view-user .virtuemart-admin-area form[name='adminForm'] table tbody"
    )
        .find('tr')
        .each(function () {
            var username = $(this).find('td').eq(1).text();
            var name = $(this).find('td').eq(2).text();
            var email = $(this).find('td').eq(3).text();

            var build_login_as_user_url = login_as_user_url.replace(
                '{{email}}',
                email.trim()
            );

            $(this)
                .find('td')
                .eq(3)
                .after(
                    '<td><a target="_blank" class="btn" href="' +
                        build_login_as_user_url +
                        '">Login as <span class="loginasuser-email" style="font-weight:bold">' +
                        name +
                        ' (' +
                        username +
                        ')' +
                        '</span></td>'
                );
        });

    // Column
    $(
        ".admin.com_virtuemart.view-user form[action='/administrator/index.php?option=com_virtuemart&view=user'] table tbody, .admin.com_virtuemart.view-user .virtuemart-admin-area form[name='adminForm'] table thead"
    )
        .find('th')
        .eq(3)
        .after('<th>Login as User</th>');

    /**
    VirtueMart's Orders page
    */
    // Login as user button
    $(
        ".admin.com_virtuemart.view-orders form[action='index.php?option=com_virtuemart&view=orders'] table.adminlist tbody, .admin.com_virtuemart.view-orders form[action='index.php?option=com_virtuemart&view=orders'] table.uk-table tbody"
    )
        .find('tr')
        .each(function () {
            var name = $(this).find('td').eq(2).html();

            const getOnlyEmail = name
                .trim()
                .match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
            const email = getOnlyEmail[0].trim();

            var build_login_as_user_url = login_as_user_url.replace(
                '{{email}}',
                email
            );

            $(this)
                .find('td')
                .eq(2)
                .after(
                    '<td><a target="_blank" class="btn" href="' +
                        build_login_as_user_url +
                        '">Login as <span class="loginasuser-email" style="font-weight:bold">' +
                        email +
                        ' </span></td>'
                );
        });

    // Column
    $(
        ".admin.com_virtuemart.view-orders form[action='index.php?option=com_virtuemart&view=orders'] table.adminlist thead, .admin.com_virtuemart.view-orders form[action='index.php?option=com_virtuemart&view=orders'] table.uk-table thead"
    )
        .find('th')
        .eq(2)
        .after('<th>Login as User</th>');
});
