"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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChangeSquasher = void 0;
exports.parse = parse;
const deepmerge_1 = __importDefault(require("deepmerge"));
const utils_1 = require("../utils");
const CycleCreator = __importStar(require("./CycleCreator"));
function parse(record) {
    const changes = CycleCreator.submodules.map((submodule) => submodule.parseRecord(record));
    const mergedChange = deepmerge_1.default.all(changes);
    return mergedChange;
}
class ChangeSquasher {
    constructor() {
        this.final = {
            added: [],
            removed: [],
            updated: [],
        };
        this.addedIds = new Set();
        this.removedIds = new Set();
        this.seenUpdates = new Map();
    }
    addChange(change) {
        for (const id of change.removed) {
            // Ignore if id is already removed
            if (this.removedIds.has(id))
                continue;
            // Mark this id as removed
            this.removedIds.add(id);
        }
        for (const update of change.updated) {
            // Ignore if update.id is already removed
            if (this.removedIds.has(update.id))
                continue;
            // Mark this id as updated
            this.seenUpdates.set(update.id, update);
        }
        for (const joinedConsensor of (0, utils_1.reversed)(change.added)) {
            // Ignore if it's already been added
            if (this.addedIds.has(joinedConsensor.id))
                continue;
            // Ignore if joinedConsensor.id is already removed
            if (this.removedIds.has(joinedConsensor.id)) {
                continue;
            }
            // Check if this id has updates
            const update = this.seenUpdates.get(joinedConsensor.id);
            if (update) {
                // If so, put them into final.updated
                this.final.updated.unshift(update);
                this.seenUpdates.delete(joinedConsensor.id);
            }
            // Add joinedConsensor to final.added
            this.final.added.unshift(joinedConsensor);
            // Mark this id as added
            this.addedIds.add(joinedConsensor.id);
        }
    }
}
exports.ChangeSquasher = ChangeSquasher;
//# sourceMappingURL=CycleParser.js.map