import WarpError from "../errors/WarpError.js";
import { isPropValue } from "../types/PropValue.js";
/** Runtime-backed value for a legacy property compatibility record. */
export default class LegacyPropertyValue {
    value;
    constructor(value) {
        this.value = clonePropValue(requirePropValue(value));
        Object.freeze(this);
    }
    /** Returns a defensive copy of the property-compatible value. */
    toPropValue() {
        return clonePropValue(this.value);
    }
}
/** Requires a value that can live in the property register. */
function requirePropValue(value) {
    if (!isPropValue(value)) {
        throw new WarpError('LegacyPropertyValue must wrap a PropValue', 'E_VALIDATION');
    }
    return value;
}
/** Copies recursive property values so source carriers stay outside the noun. */
function clonePropValue(value) {
    if (value instanceof Uint8Array) {
        return new Uint8Array(value);
    }
    if (Array.isArray(value)) {
        return value.map((entry) => clonePropValue(entry));
    }
    if (isPropValueObject(value)) {
        const copy = {};
        for (const [key, entry] of Object.entries(value)) {
            copy[key] = clonePropValue(entry);
        }
        return copy;
    }
    return value;
}
/** Narrows recursive property objects. */
function isPropValueObject(value) {
    return value !== null
        && typeof value === 'object'
        && !(value instanceof Uint8Array)
        && !Array.isArray(value);
}
