<div id="lc-container"></div>

<script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=default,es6,fetch,Array.prototype.includes,Object.keys,Symbol.iterator&flags=gated" type="text/javascript"></script>
<script>
    var LC_TOKEN = '{{ lcToken }}';
    var STORE_TOKEN = '{{ storeToken }}';
    var SITE_URL = '{{ siteUrl }}';
    var USER_EMAIL = '{{ userEmail }}';
    var WP_VER = '{{ wpVer }}';
    var MODULE_VER = '{{ moduleVer }}';
    var PARTNER_ID = '{{ partnerId }}';

    (function () {
        var interval = setInterval(function () {
            if (window.AppBridge === undefined) {
                return;
            }
            clearInterval(interval);

            function adminAjaxCall(action, data = {}, type = 'POST') {
                return jQuery.ajax({
                    url: ajaxurl, //ajaxurl is WP global variable
                    type,
                    data: {
                        ...data,
                        action,
                        security: ajax_nonce.value
                    }
                });
            }

            function userSignedInHandler(userToken, storeToken) {
                return adminAjaxCall('lc_connect', {
                    user_token: userToken,
                    store_token: storeToken
                });
            }

            function entityNotFound(entity) {
                return adminAjaxCall(`lc_${entity}_not_found`).done(() => {
                    window.location.reload();
                });
            }

            var eventsRegister = new AppBridge.EventsRegister();
            eventsRegister.register(AppBridge.UserSignedInEvent, function (data) {
                return userSignedInHandler(data.userToken, data.storeToken)
            });
            eventsRegister.register(AppBridge.ErrorOccurredEvent, function (data) {
                if (data.statusCode !== 404) {
                    return console.error(new Error(data.errorMessage));
                }

                if (data.errorMessage === 'Store was not found') {
                    return entityNotFound('store');
                }

                if (data.errorMessage === 'User was not found') {
                    return entityNotFound('user');
                }
            });

            AppBridge.AppBridgeParent.init('wordpress', eventsRegister, document.getElementById('lc-container'), '{{ appUrl }}').then(function (bridge) {
                document.getElementsByTagName('iframe')[0].classList.add('lc-iframe');

                if (LC_TOKEN) {
                    return bridge.call(new AppBridge.SignInCommand(LC_TOKEN, bridge.platform, WP_VER, MODULE_VER));
                }

                if (STORE_TOKEN) {
                    return bridge.call(new AppBridge.JoinCommand(STORE_TOKEN, USER_EMAIL, bridge.platform, WP_VER, MODULE_VER));
                }

                return bridge.call(new AppBridge.ConnectCommand(SITE_URL, USER_EMAIL, bridge.platform, WP_VER, MODULE_VER, PARTNER_ID));
            });
        }, 100);
    })();
</script>
