'use strict';

var cheerio = require('cheerio');
var url = require('url');

function hrefIsIcon(href) {
    return (/((icon.*\.(png|jpg))|(\w+\.ico))/.test(href)
    );
}

function getDomainUrl(someUrl) {
    var parsedUrl = url.parse(someUrl);
    parsedUrl.pathname = null;
    return parsedUrl.format();
}

function linkTagLinks($) {
    var links = [];
    $('link').each(function (index, element) {
        var href = $(element).attr('href');
        if (!hrefIsIcon(href)) {
            return;
        }
        links.push(href);
    });
    return links;
}

function metaTagLinks($) {
    var links = [];
    $('meta').each(function (index, element) {
        var property = $(element).attr('property');
        if (property !== 'og:image') {
            return;
        }

        var graphImageUrl = $(element).attr('content');
        links.push(graphImageUrl);
    });

    return links;
}

function getIconLinks(rootUrl, dom) {
    var $ = cheerio.load(dom);
    var iconLinks = [];

    iconLinks = iconLinks.concat(linkTagLinks($));
    iconLinks = iconLinks.concat(metaTagLinks($));

    iconLinks = iconLinks.map(function (iconLink) {
        return url.resolve(rootUrl, iconLink);
    });

    iconLinks.push(url.resolve(getDomainUrl(rootUrl), 'apple-touch-icon.png'));
    return iconLinks;
}

module.exports = getIconLinks;
//# sourceMappingURL=getIconLinks.js.map