import OpticReadFailureSchemaError from "./OpticReadFailureSchemaError.js";
export default class OpticReadTarget {
    opticKind;
    nodeId;
    propertyKey;
    constructor(options) {
        assertNonEmpty(options.nodeId, 'nodeId');
        if (options.opticKind === 'node-property') {
            assertNonEmpty(options.propertyKey ?? '', 'propertyKey');
        }
        this.opticKind = options.opticKind;
        this.nodeId = options.nodeId;
        this.propertyKey = options.propertyKey ?? null;
        Object.freeze(this);
    }
    static node(nodeId) {
        return new OpticReadTarget({ opticKind: 'node', nodeId });
    }
    static nodeProperty(nodeId, propertyKey) {
        return new OpticReadTarget({ opticKind: 'node-property', nodeId, propertyKey });
    }
    static neighborhood(nodeId) {
        return new OpticReadTarget({ opticKind: 'neighborhood', nodeId });
    }
    static traversal(nodeId) {
        return new OpticReadTarget({ opticKind: 'traversal', nodeId });
    }
    toContextValue() {
        if (this.propertyKey === null) {
            return Object.freeze({ nodeId: this.nodeId });
        }
        return Object.freeze({ nodeId: this.nodeId, propertyKey: this.propertyKey });
    }
}
function assertNonEmpty(value, field) {
    if (value.length === 0) {
        throw new OpticReadFailureSchemaError('optic read target requires non-empty fields', { field });
    }
}
