"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBuildDeps = void 0;
const ramda_1 = require("ramda");
const upath_1 = require("@ewizardjs/upath");
const url_1 = require("url");
const urlJoin = require("url-join");
const encodeSpecialChars_1 = require("./encodeSpecialChars");
const fileProtocol = 'file:///';
function getBuildDeps(presentationPath, appCompilation) {
    const unixPresentationPath = (0, upath_1.toUnix)(presentationPath);
    const modules = appCompilation.reduce((acc, compilation) => {
        acc.push(...compilation.modules);
        return acc;
    }, []).filter((module) => module.resource);
    const indirectDeps = modules
        .filter((module) => {
        const url = getFileUrl(fileProtocol, module.resource);
        const { pathname, searchParams } = new URL(url);
        return decodeURI(pathname).endsWith('.scss') || searchParams.get('lang') === 'scss';
    })
        .reduce((scssDeps, scssModule) => { var _a; return scssDeps.concat(Array.from(((_a = scssModule.buildInfo) === null || _a === void 0 ? void 0 : _a.fileDependencies) || [])); }, []);
    const directDeps = modules.map((module) => module.resource);
    const allDeps = (0, ramda_1.uniq)(directDeps.concat(indirectDeps)).reduce((deps, resource) => {
        const url = getFileUrl(fileProtocol, resource);
        const path = (0, upath_1.toUnix)((0, url_1.fileURLToPath)(url));
        return path.startsWith(unixPresentationPath)
            ? [...deps, (0, upath_1.relative)(unixPresentationPath, path)]
            : deps;
    }, []);
    return allDeps;
}
exports.getBuildDeps = getBuildDeps;
function getFileUrl(fileProtocol, resource) {
    const url = urlJoin(fileProtocol, resource);
    return (0, encodeSpecialChars_1.encodeSpecialChars)(url);
}
//# sourceMappingURL=getBuildDeps.js.map