"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const R = require("ramda");
const xstream_1 = require("xstream");
exports.propertyByDependencyType = {
    dev: 'devDependencies',
    optional: 'optionalDependencies',
    prod: 'dependencies',
};
exports.default = (log$, deprecationLog$) => {
    const rootLog$ = log$.filter((log) => log.name === 'pnpm:root');
    const deprecationSet$ = deprecationLog$
        .fold((acc, log) => {
        acc.add(log.pkgId);
        return acc;
    }, new Set());
    const pkgsDiff$ = xstream_1.default.combine(rootLog$, deprecationSet$)
        .fold((pkgsDiff, args) => {
        const rootLog = args[0];
        const deprecationSet = args[1];
        if (rootLog['added']) {
            pkgsDiff[rootLog['added'].dependencyType][`+${rootLog['added'].name}`] = {
                added: true,
                deprecated: deprecationSet.has(rootLog['added'].id),
                latest: rootLog['added'].latest,
                name: rootLog['added'].name,
                realName: rootLog['added'].realName,
                version: rootLog['added'].version,
            };
            return pkgsDiff;
        }
        if (rootLog['removed']) {
            pkgsDiff[rootLog['removed'].dependencyType][`-${rootLog['removed'].name}`] = {
                added: false,
                name: rootLog['removed'].name,
                version: rootLog['removed'].version,
            };
            return pkgsDiff;
        }
        if (rootLog['linked']) {
            pkgsDiff[rootLog['linked'].dependencyType][`>${rootLog['linked'].name}`] = {
                added: false,
                from: rootLog['linked'].from,
                linked: true,
                name: rootLog['linked'].name,
            };
            return pkgsDiff;
        }
        return pkgsDiff;
    }, {
        dev: {},
        optional: {},
        prod: {},
    });
    const packageJson$ = log$
        .filter((log) => log.name === 'pnpm:package-json')
        .take(2)
        .fold(R.merge, {})
        .last();
    return xstream_1.default.combine(pkgsDiff$, packageJson$)
        .map(R.apply((pkgsDiff, packageJsons) => {
        const initialPackageJson = packageJsons['initial'];
        const updatedPackageJson = packageJsons['updated'];
        if (!initialPackageJson || !updatedPackageJson)
            return pkgsDiff;
        for (const depType of ['prod', 'optional', 'dev']) {
            const prop = exports.propertyByDependencyType[depType];
            const initialDeps = R.keys(initialPackageJson[prop]);
            const updatedDeps = R.keys(updatedPackageJson[prop]);
            const removedDeps = R.difference(initialDeps, updatedDeps);
            for (const removedDep of removedDeps) {
                if (!pkgsDiff[depType][`-${removedDep}`]) {
                    pkgsDiff[depType][`-${removedDep}`] = {
                        added: false,
                        name: removedDep,
                        version: initialPackageJson[prop][removedDep],
                    };
                }
            }
            const addedDeps = R.difference(updatedDeps, initialDeps);
            for (const addedDep of addedDeps) {
                if (!pkgsDiff[depType][`+${addedDep}`]) {
                    pkgsDiff[depType][`+${addedDep}`] = {
                        added: true,
                        name: addedDep,
                        version: updatedPackageJson[prop][addedDep],
                    };
                }
            }
        }
        return pkgsDiff;
    }));
};
//# sourceMappingURL=pkgsDiff.js.map