import EdgeRecord from "./EdgeRecord.js";
import NodeRecord from "./NodeRecord.js";
import VisibleEdgePropertyRecord from "./VisibleEdgePropertyRecord.js";
import VisibleNodePropertyRecord from "./VisibleNodePropertyRecord.js";
import WarpError from "../errors/WarpError.js";
/** Runtime-backed collection of visible legacy property compatibility records. */
export default class LegacyPropertyProjection {
    nodeProperties;
    edgeProperties;
    constructor(fields) {
        const checkedFields = requireFields(fields);
        this.nodeProperties = requireNodeProperties(checkedFields.nodeProperties);
        this.edgeProperties = requireEdgeProperties(checkedFields.edgeProperties);
        Object.freeze(this);
    }
    /** Returns visible property records for a node owner. */
    propertiesForNode(owner) {
        const checkedOwner = requireNodeOwner(owner);
        return Object.freeze(this.nodeProperties.filter((record) => record.owner.equals(checkedOwner)));
    }
    /** Returns visible property records for an edge owner. */
    propertiesForEdge(owner) {
        const checkedOwner = requireEdgeOwner(owner);
        return Object.freeze(this.edgeProperties.filter((record) => record.owner.equals(checkedOwner)));
    }
}
/** Validates the projection constructor envelope. */
function requireFields(fields) {
    if (fields === null || fields === undefined) {
        throw new WarpError('LegacyPropertyProjection fields must be provided', 'E_VALIDATION');
    }
    return fields;
}
/** Requires a node owner for projection lookup. */
function requireNodeOwner(owner) {
    if (!(owner instanceof NodeRecord)) {
        throw new WarpError('LegacyPropertyProjection node owner must be a NodeRecord', 'E_VALIDATION');
    }
    return owner;
}
/** Requires an edge owner for projection lookup. */
function requireEdgeOwner(owner) {
    if (!(owner instanceof EdgeRecord)) {
        throw new WarpError('LegacyPropertyProjection edge owner must be an EdgeRecord', 'E_VALIDATION');
    }
    return owner;
}
/** Requires immutable node property records. */
function requireNodeProperties(records) {
    if (!Array.isArray(records)) {
        throw new WarpError('LegacyPropertyProjection nodeProperties must be an array', 'E_VALIDATION');
    }
    return Object.freeze(records.map((record) => requireNodeProperty(record)));
}
/** Requires immutable edge property records. */
function requireEdgeProperties(records) {
    if (!Array.isArray(records)) {
        throw new WarpError('LegacyPropertyProjection edgeProperties must be an array', 'E_VALIDATION');
    }
    return Object.freeze(records.map((record) => requireEdgeProperty(record)));
}
/** Requires a runtime-backed visible node property record. */
function requireNodeProperty(record) {
    if (!(record instanceof VisibleNodePropertyRecord)) {
        throw new WarpError('LegacyPropertyProjection nodeProperties entries must be VisibleNodePropertyRecord values', 'E_VALIDATION');
    }
    return record;
}
/** Requires a runtime-backed visible edge property record. */
function requireEdgeProperty(record) {
    if (!(record instanceof VisibleEdgePropertyRecord)) {
        throw new WarpError('LegacyPropertyProjection edgeProperties entries must be VisibleEdgePropertyRecord values', 'E_VALIDATION');
    }
    return record;
}
