import debounce from 'debounce';
import security from './security';
import settings from './settings';

const SELECTOR = '#chat-lines a.externallink';
const DOMAINS = new Set([
    'youtube.com',
    'youtu.be',
    'bit.ly',
    'goo.gl',
    'ow.ly',
    't.co',
    'twitter.com',
    'tinyurl.com',
    'tr.im',
    'twitch.tv'
]);

let linkData = new Map();

function onData(url, data) {
    linkData.set(url, data);

    if (!data || !data.tooltip) {
        return;
    }

    let elem = $('<div>').append(data.tooltip);
    security.sanitize(elem);
    data.tooltip = elem.get(0).outerHTML;

    $(SELECTOR).filter(`[href="${url}"]`).tooltip({
        html: true,
        placement: 'auto top',
        title: data.tooltip,
        trigger: 'manual'
    }).each(function() {
        let $this = $(this);
        if ($this.is(':hover')) {
            $this.tooltip('show');
        }
    });
}

function resolveLink(url) {
    if (linkData.has(url)) {
        return linkData.get(url);
    }

    // Call from content script for now
    // postMessage('bdgg_get_linkinfo', url).then(data => { onData(url, data); });

    let encodedURL = encodeURIComponent(url);
    $.getJSON('https://api.betterttv.net/2/link_resolver/' + encodedURL)
        .then(data => { onData(url, data); });
}

let hoverLink = debounce(function() {
    let domain = this.hostname.split('.').slice(-2).join('.');
    if (!DOMAINS.has(domain)) {
        return;
    }

    let $this = $(this);
    let url = $this.attr('href');

    let data = resolveLink(url);
    if (!data || !data.tooltip) {
        return;
    }

    if ($this.is(':hover')) {
        $this.tooltip('show');
    }
}, 250);

let linkinfo = {
    init() {
        this.enabled(settings.get('bdgg_show_linkinfo'));
        settings.on('bdgg_show_linkinfo', value => { this.enabled(value); });
    },
    enabled(value) {
        if (value) {
            $('body').on('mouseover', SELECTOR, hoverLink);
            $('body').on('mouseout', SELECTOR, function() {
                $(this).tooltip('hide');
            });
        } else {
            $('body').off('mouseover', SELECTOR, hoverLink);
        }
    }
};

export default linkinfo;

@@ -26,16 +26,60 @@
 ounce';%0A
+import %7B postMessage %7D from '../messaging';%0A
 import s
@@ -1044,51 +1044,8 @@
 %0A   
- // Call from content script for now%0A    //
  pos
@@ -1078,16 +1078,25 @@
 o', url)
+%0A        
 .then(da
@@ -1128,173 +1128,31 @@
 ; %7D)
-;%0A
 %0A    
-let encodedURL = encodeURIComponent(url);%0A    $.getJSON('https://api.betterttv.net/2/link_resolver/' + encodedURL)%0A        .then(data =%3E %7B onData(url, data); 
+    .catch(() =%3E %7B
 %7D);%0A

move linkinfo xhr to background page