import AttachmentKey from "./AttachmentKey.js";
import AttachmentSchemaVersion from "./AttachmentSchemaVersion.js";
import EdgeRecord from "./EdgeRecord.js";
import NodeRecord from "./NodeRecord.js";
import WarpError from "../errors/WarpError.js";
import { isPropValue } from "../types/PropValue.js";
/** Runtime-backed graph attachment slot and payload record. */
export default class AttachmentRecord {
    owner;
    key;
    value;
    schemaVersion;
    constructor(fields) {
        const checkedFields = requireFields(fields);
        this.owner = requireOwner(checkedFields.owner);
        this.key = requireKey(checkedFields.key);
        this.value = requireValue(checkedFields.value);
        this.schemaVersion = requireSchemaVersion(checkedFields.schemaVersion);
        Object.freeze(this);
    }
    /** Returns true when this attachment is owned by a node record. */
    isNodeAttachment() {
        return this.owner instanceof NodeRecord;
    }
    /** Returns true when this attachment is owned by an edge record. */
    isEdgeAttachment() {
        return this.owner instanceof EdgeRecord;
    }
}
/** Validates the attachment-record constructor envelope. */
function requireFields(fields) {
    if (fields === null || fields === undefined) {
        throw new WarpError('AttachmentRecord fields must be provided', 'E_VALIDATION');
    }
    return fields;
}
/** Requires a runtime-backed attachment owner. */
function requireOwner(value) {
    if (value instanceof NodeRecord || value instanceof EdgeRecord) {
        return value;
    }
    throw new WarpError('AttachmentRecord owner must be a NodeRecord or EdgeRecord', 'E_VALIDATION');
}
/** Requires a runtime-backed attachment key. */
function requireKey(value) {
    if (!(value instanceof AttachmentKey)) {
        throw new WarpError('AttachmentRecord key must be an AttachmentKey', 'E_VALIDATION');
    }
    return value;
}
/** Requires a property-compatible attachment value. */
function requireValue(value) {
    if (!isPropValue(value)) {
        throw new WarpError('AttachmentRecord value must be a PropValue', 'E_VALIDATION');
    }
    return value;
}
/** Requires a runtime-backed attachment schema version. */
function requireSchemaVersion(value) {
    if (!(value instanceof AttachmentSchemaVersion)) {
        throw new WarpError('AttachmentRecord schemaVersion must be an AttachmentSchemaVersion', 'E_VALIDATION');
    }
    return value;
}
