var Wick = Wick || {};

Wick.CookieBar = (function () {
    var init = function () {
        $.cookieBar({
            message: 'We use cookies to help deliver our services and give you the best experience on\n' +
                '            our website. If you continue without changing your settings, we’ll assume that you are happy to receive all\n' +
                '            cookies on the Wickes website. However, you can <a class="cookies-policy__link" href="/cookies">read our cookie policy here</a>', //Message displayed on bar
            message1: '<a class="cookies-policy__link" href="/cookies">Wickes cookie policy</a>', //Message displayed on bar
            acceptText: 'I Understand', //Text on accept/enable button
            acceptButton: true, //Set to true to show accept/enable button
            acceptButtonMobile: true, //Set to true to show accept/enable button
            policyButton: false, //Set to true to show Privacy Policy button
            domain: getDomain(String(window.location.hostname))
        });

        function getDomain(host = '') {
            const wickesSubDomain = 'wickes.co.uk',
                  isEnv = window.location.hostname.includes(wickesSubDomain),
                  subDomain =  host.split('.')
                .map((element, index) => (index === 0 ? '' : element))
                .join('.').slice(1);

            return isEnv ? subDomain : '';
        };

        let getDuplicateCookie = document.cookie.split('; ').filter((i) => i.split('=')[0] === 'cb-enabled')

        if (getDuplicateCookie.length > 1 && window.location.hostname.includes('dev')) {
            document.cookie = 'cb-enabled=; path=/; domain=.wickes.co.uk; expires=' + new Date(0).toUTCString();
        }
    };

    if (!navigator.userAgent.match(/TradeProMobile_iOS|TradeProMobile_Android|DIYMobile_iOS|DIYMobile_Android|DIYApp_Guest_Mobile_iOS|DIYApp_Guest_Mobile_Android|AGENT_PLACEHOLDER/i)) {
        init();
    }
})();
