"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.diffAttributes = exports.removeErasures = exports.getAttributes = void 0;
const isEmpty_1 = require("@jsonjoy.com/util/lib/isEmpty");
const PersistedSlice_1 = require("../peritext/slice/PersistedSlice");
const constants_1 = require("../peritext/slice/constants");
const getAttributes = (overlayPoint) => {
    const layers = overlayPoint.layers;
    const layerLength = layers.length;
    if (!layerLength)
        return;
    const attributes = {};
    for (let i = 0; i < layerLength; i++) {
        const slice = layers[i];
        if (!(slice instanceof PersistedSlice_1.PersistedSlice))
            continue;
        switch (slice.stacking) {
            case constants_1.SliceStacking.One: {
                const tag = slice.type();
                if (tag)
                    attributes[tag] = slice.data();
                break;
            }
            case constants_1.SliceStacking.Erase: {
                const tag = slice.type();
                if (tag)
                    delete attributes[tag];
                break;
            }
        }
    }
    if ((0, isEmpty_1.isEmpty)(attributes))
        return undefined;
    return attributes;
};
exports.getAttributes = getAttributes;
const eraseAttributes = (attr) => {
    if (!attr)
        return;
    const keys = Object.keys(attr);
    const length = keys.length;
    if (!length)
        return;
    const erased = {};
    for (let i = 0; i < length; i++)
        erased[keys[i]] = null;
    return erased;
};
const removeErasures = (attr) => {
    if (!attr)
        return;
    const keys = Object.keys(attr);
    const length = keys.length;
    if (!length)
        return;
    const cleaned = {};
    for (let i = 0; i < length; i++) {
        const key = keys[i];
        const value = attr[key];
        if (value !== null)
            cleaned[key] = value;
    }
    return (0, isEmpty_1.isEmpty)(cleaned) ? undefined : cleaned;
};
exports.removeErasures = removeErasures;
const diffAttributes = (oldAttributes, newAttributes) => {
    if (!oldAttributes)
        return (0, exports.removeErasures)(newAttributes);
    if (!newAttributes)
        return eraseAttributes(oldAttributes);
    const diff = {};
    const keys = Object.keys(newAttributes);
    const length = keys.length;
    for (let i = 0; i < length; i++) {
        const key = keys[i];
        const newValue = newAttributes[key];
        const oldValue = oldAttributes[key];
        if (newValue === oldValue)
            continue;
        diff[key] = newValue;
    }
    const oldKeys = Object.keys(oldAttributes);
    const oldLength = oldKeys.length;
    for (let i = 0; i < oldLength; i++) {
        const key = oldKeys[i];
        // tslint:disable-next-line:triple-equals
        if (newAttributes[key] !== undefined)
            continue;
        diff[key] = null;
    }
    if ((0, isEmpty_1.isEmpty)(diff))
        return undefined;
    return diff;
};
exports.diffAttributes = diffAttributes;
//# sourceMappingURL=util.js.map