'use strict';

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

function getExtension(downloadUrl) {
    return downloadUrl.match(/\.(png|jpg|ico)/)[0];
}

function getSiteDomain(siteUrl) {
    return url.parse(siteUrl).hostname;
}

function downloadIcon(iconUrl) {
    var iconData = new Promise(function (resolve, reject) {
        axios.get(iconUrl, { responseType: 'arraybuffer' }).then(function (response) {
            resolve(response.data);
        }).catch(function (error) {
            if (error.status === 404) {
                resolve();
                return;
            }
            reject(error);
        });
    });

    return iconData.then(function (iconData) {
        if (!iconData) {
            return new Promise(function (resolve) {
                return resolve();
            });
        }
        return {
            source: getSiteDomain(iconUrl),
            type: getExtension(iconUrl),
            data: iconData
        };
    });
}

function downloadIcons(iconUrls) {
    var promises = iconUrls.map(downloadIcon);
    return Promise.all(promises).then(function (iconPaths) {
        return iconPaths.filter(function (element) {
            return !!element;
        });
    });
}

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