"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.extractCssDependencyUrls = void 0;
const csstree = __importStar(require("@applitools/css-tree"));
const utils = __importStar(require("@applitools/utils"));
function extractCssDependencyUrls(css, { resourceUrl, sourceUrl }) {
    const urls = new Set();
    const ast = csstree.parse(css, {
        parseRulePrelude: false,
        parseAtrulePrelude: true,
        parseCustomProperty: true,
        parseValue: true,
    });
    csstree.walk(ast, node => {
        var _a;
        if (node.type === 'Atrule' && node.name === 'import' && ((_a = node.prelude) === null || _a === void 0 ? void 0 : _a.type) === 'AtrulePrelude') {
            return processImportPrelude(node.prelude, { baseUrl: resourceUrl });
        }
        else if (node.type === 'Declaration' && node.property.startsWith('--')) {
            return processCustomPropertyValue(node.value, { baseUrl: sourceUrl !== null && sourceUrl !== void 0 ? sourceUrl : resourceUrl });
        }
        else {
            return processCssNode(node, { baseUrl: resourceUrl });
        }
    });
    return [...urls];
    function processCssNode(node, { baseUrl }) {
        if (node.type === 'Url') {
            urls.add(sanitizeUrl(node.value, { baseUrl }));
            return csstree.walk.skip;
        }
        else if (node.type === 'Function' && node.name.includes('image-set')) {
            node.children.forEach(imageNode => {
                if (imageNode.type === 'Url' || imageNode.type === 'String')
                    urls.add(sanitizeUrl(imageNode.value, { baseUrl }));
            });
            return csstree.walk.skip;
        }
    }
    function processImportPrelude(node, { baseUrl }) {
        var _a, _b;
        if (((_a = node.children.first) === null || _a === void 0 ? void 0 : _a.type) === 'Url' || ((_b = node.children.first) === null || _b === void 0 ? void 0 : _b.type) === 'String') {
            return urls.add(sanitizeUrl(node.children.first.value, { baseUrl }));
        }
        return csstree.walk.skip;
    }
    function processCustomPropertyValue(node, { baseUrl }) {
        csstree.walk(node, node => processCssNode(node, { baseUrl }));
        return csstree.walk.skip;
    }
}
exports.extractCssDependencyUrls = extractCssDependencyUrls;
function sanitizeUrl(url, { baseUrl }) {
    return utils.general.absolutizeUrl(utils.general.toUnAnchoredUri(utils.general.toUriEncoding(url)), baseUrl);
}
