import {appendLoader, hideLoader, showLoader} from "./utils/loader";

var Wick = window.Wick || {};

Wick.SmartBanner = {
    STORAGE_KEY: 'smartBannerClosedAt',
    BANNER_SELECTOR: '.smart-banner',
    CLOSE_BUTTON_SELECTOR: '.smart-banner__action-close',
    INSTALL_BUTTON_SELECTOR: '.install-app',
    OPEN_BUTTON_SELECTOR: '.open-app',
    LOADER_SELECTOR: '.loader-wrapper',
    LOADER_PAYLOAD: {
        wrapper: '.smart-banner',
        hidden: true,
        titleLoader: 'Opening the app... If it doesn’t open, tap \'Install\'',
        modifier: 'smart-banner-loader',
    },

    init: function () {
        appendLoader(Wick.SmartBanner.LOADER_PAYLOAD);
        $(Wick.SmartBanner.CLOSE_BUTTON_SELECTOR).on('click', Wick.SmartBanner.closeBanner);
        $(Wick.SmartBanner.OPEN_BUTTON_SELECTOR).on('click', Wick.SmartBanner.handleOpenClick);
        $(Wick.SmartBanner.INSTALL_BUTTON_SELECTOR).on('click', Wick.SmartBanner.handleInstallClick);
        if ('scrollRestoration' in history) {
            history.scrollRestoration = 'manual';
        }
    },

    hideBanner: function () {
        $(Wick.SmartBanner.BANNER_SELECTOR).addClass('hide');
    },

    closeBanner: function (e) {
        e.preventDefault();
        Wick.SmartBanner.hideBanner();
    },

    handleOpenClick: function (e) {
        e.preventDefault();
        showLoader()
        setTimeout(() => {
            hideLoader()
        }, 5000);
    },

    handleInstallClick: function (e) {
        e.preventDefault();
    },
};

$(function () {
    Wick.SmartBanner.init();
});
