import LegacyNodePropertyKey from "./LegacyNodePropertyKey.js";
import LegacyPropertyValue from "./LegacyPropertyValue.js";
import NodeRecord from "./NodeRecord.js";
import WarpError from "../errors/WarpError.js";
/** Runtime-backed intent for writing one legacy-compatible node property. */
export default class NodePropertyWriteIntent {
    owner;
    key;
    value;
    constructor(fields) {
        const checkedFields = requireFields(fields);
        this.owner = requireOwner(checkedFields.owner);
        this.key = requireKey(checkedFields.key);
        this.value = requireValue(checkedFields.value);
        Object.freeze(this);
    }
    /** Builds a write intent from the current public node-property API fields. */
    static fromLegacyProperty(nodeId, key, value) {
        return new NodePropertyWriteIntent({
            owner: NodeRecord.fromLegacyNodeId(nodeId),
            key: new LegacyNodePropertyKey(key),
            value: new LegacyPropertyValue(value),
        });
    }
    /** Returns the current legacy node id target. */
    nodeId() {
        return this.owner.id.toString();
    }
    /** Returns the current legacy property key target. */
    propertyKey() {
        return this.key.toString();
    }
    /** Returns a defensive copy of the property value. */
    propertyValue() {
        return this.value.toPropValue();
    }
}
/** Validates the write-intent constructor envelope. */
function requireFields(fields) {
    if (fields === null || fields === undefined) {
        throw new WarpError('NodePropertyWriteIntent fields must be provided', 'E_VALIDATION');
    }
    return fields;
}
/** Requires a runtime-backed node owner. */
function requireOwner(owner) {
    if (!(owner instanceof NodeRecord)) {
        throw new WarpError('NodePropertyWriteIntent owner must be a NodeRecord', 'E_VALIDATION');
    }
    return owner;
}
/** Requires a runtime-backed node property key. */
function requireKey(key) {
    if (!(key instanceof LegacyNodePropertyKey)) {
        throw new WarpError('NodePropertyWriteIntent key must be a LegacyNodePropertyKey', 'E_VALIDATION');
    }
    return key;
}
/** Requires a runtime-backed property value. */
function requireValue(value) {
    if (!(value instanceof LegacyPropertyValue)) {
        throw new WarpError('NodePropertyWriteIntent value must be a LegacyPropertyValue', 'E_VALIDATION');
    }
    return value;
}
