"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractSvgDependencyUrls = void 0;
const xmldom_1 = require("@xmldom/xmldom");
const extract_css_dependency_urls_1 = require("./extract-css-dependency-urls");
const utils = __importStar(require("@applitools/utils"));
function extractSvgDependencyUrls(svg, { resourceUrl, sourceUrl }) {
    const urls = new Set();
    const doc = new xmldom_1.DOMParser().parseFromString(svg, 'image/svg+xml');
    Array.from(doc.getElementsByTagName('*')).forEach(element => {
        const tagName = element.tagName.toUpperCase();
        if (tagName === 'IMG') {
            if (element.hasAttribute('src')) {
                urls.add(sanitizeUrl(element.getAttribute('src'), { baseUrl: resourceUrl }));
            }
            if (element.hasAttribute('srcset')) {
                const sources = element.getAttribute('srcset').split(', ');
                sources.forEach(source => urls.add(sanitizeUrl(source.trim().split(/\s+/, 1)[0], { baseUrl: resourceUrl })));
            }
        }
        else if (tagName === 'IMAGE' ||
            tagName === 'USE' ||
            (tagName === 'LINK' && element.getAttribute('rel') === 'stylesheet')) {
            const href = (element.getAttribute('href') || element.getAttribute('xlink:href'));
            urls.add(sanitizeUrl(href, { baseUrl: resourceUrl }));
        }
        else if (tagName === 'OBJECT') {
            urls.add(sanitizeUrl(element.getAttribute('data'), { baseUrl: resourceUrl }));
        }
        else if (tagName === 'STYLE') {
            const cssUrls = element.textContent ? (0, extract_css_dependency_urls_1.extractCssDependencyUrls)(element.textContent, { resourceUrl, sourceUrl }) : [];
            cssUrls.forEach(url => urls.add(url));
        }
        if (element.hasAttribute('style')) {
            const matches = element.getAttribute('style').matchAll(/url\((?!['"]?:)['"]?([^'")]*)['"]?\)/g);
            Array.from(matches).forEach(([, url]) => urls.add(sanitizeUrl(url, { baseUrl: resourceUrl })));
        }
    });
    return [...urls];
}
exports.extractSvgDependencyUrls = extractSvgDependencyUrls;
function sanitizeUrl(url, { baseUrl }) {
    if (url.startsWith('#'))
        return baseUrl;
    return utils.general.absolutizeUrl(utils.general.toUnAnchoredUri(url), baseUrl);
}
