import { jsonClone } from '../../hub/types/json.js';
function cloneValue(value) {
    return jsonClone(value);
}
function entriesFromInput(input) {
    if (!input) {
        return undefined;
    }
    if (input instanceof ConversionMetaBag) {
        return input.entries();
    }
    if (typeof input[Symbol.iterator] === 'function') {
        return input;
    }
    return Object.entries(input);
}
export class ConversionMetaBag {
    store = new Map();
    constructor(initial) {
        if (initial) {
            this.ingest(initial);
        }
    }
    static from(input) {
        return new ConversionMetaBag(input);
    }
    keys() {
        return Array.from(this.store.keys());
    }
    has(key) {
        return this.store.has(key);
    }
    get(key) {
        const value = this.store.get(key);
        return value;
    }
    set(key, value) {
        if (value === undefined) {
            this.store.delete(key);
            return;
        }
        this.store.set(key, cloneValue(value));
    }
    consume(key) {
        const value = this.get(key);
        if (value !== undefined) {
            this.store.delete(key);
        }
        return value;
    }
    ingest(input) {
        const incoming = entriesFromInput(input);
        if (!incoming) {
            return;
        }
        for (const [key, value] of incoming) {
            if (value === undefined) {
                this.store.delete(key);
            }
            else {
                this.store.set(key, cloneValue(value));
            }
        }
    }
    entries() {
        return this.store.entries();
    }
    snapshot() {
        const out = {};
        for (const [key, value] of this.store.entries()) {
            out[key] = cloneValue(value);
        }
        return out;
    }
    toJSON() {
        return this.snapshot();
    }
    clone() {
        return new ConversionMetaBag(this.entries());
    }
}
