var jQueryWeb357LoginAsUserJ2Store = jQuery.noConflict();
jQueryWeb357LoginAsUserJ2Store(function ($) {
    /**
    J2Store Orders page
    */
    // Login as user button
    $(
        "body.com_j2store.view-orders form[action='index.php?option=com_j2store&view=orders'] table.table tbody"
    )
        .find('tr')
        .each(function () {
            var email = $(this).find('td').eq(5).find('small').text().trim();

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

            $(this)
                .find('td')
                .eq(5)
                .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
    $(
        "body.com_j2store.view-orders form[action='index.php?option=com_j2store&view=orders'] table.table thead"
    )
        .find('th')
        .eq(8)
        .after('<th>Login as User</th>');

    /**
    J2Store Order page
    */
    // Login as user button
    var billingAddress = $(this)
        .find(
            "body.com_j2store.view-order form[action='index.php'] table.table.addresses tbody tr td"
        )
        .eq(0);

    if (billingAddress.length) {
        const getOnlyEmail = billingAddress
            .text()
            .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
        );

        billingAddress.append(
            '<div style="margin: 15px 0"><a target="_blank" class="btn" href="' +
                build_login_as_user_url +
                '">Login as <span class="loginasuser-email" style="font-weight:bold">' +
                email +
                '</span></div>'
        );
    }

    /**
    J2Store Customers page
    */
    // Login as user button
    $(
        "body.com_j2store.view-customers form[action='index.php'] table.table tbody"
    )
        .find('tr')
        .each(function () {
            var email = $(this).find('td').eq(2).text().trim();

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

            $(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
    $(
        "body.com_j2store.view-customers form[action='index.php'] table.table thead"
    )
        .find('th')
        .eq(2)
        .after('<th>Login as User</th>');
});
