<script src="https://unpkg.com/@shopify/app-bridge@3"></script>
<script src="https://unpkg.com/@shopify/app-bridge-utils@3"></script>
<script type="text/javascript">
    (function (window) {
        function appBridgeRedirect(url) {
            var AppBridge = window['app-bridge'];
            var createApp = AppBridge.default;
            var Redirect = AppBridge.actions.Redirect;

            var app = createApp({
                apiKey: '{{ SHOPIFY_APP_API_KEY }}',
                host: '{{ host }}',
            });

            var normalizedLink = document.createElement('a');
            normalizedLink.href = url;

            Redirect.create(app).dispatch(Redirect.Action.REMOTE, normalizedLink.href);
        }

        window.appBridgeRedirect = appBridgeRedirect;
    })(window);

    (function () {
        function redirect() {
            var appBridgeUtils = window['app-bridge-utils'];
            var targetUrl = '{{ redirect_uri|safe }}';

            if (appBridgeUtils.isShopifyEmbedded()) {
                window.appBridgeRedirect(targetUrl);
            } else {
                window.top.location.href = targetUrl;
            }
        }

        document.addEventListener("DOMContentLoaded", redirect);

        // In the turbolinks context, neither DOMContentLoaded nor turbolinks:load
        // consistently fires. This ensures that we at least attempt to fire in the
        // turbolinks situation as well.
        redirect();
    })();
</script>