import { canonicalStringify } from "../../utils/canonicalStringify.js";
const PENDING_IDENTITY = 'checkpoint-tail-query:pending';
export function exactIdOnlyQueryNodeId(request) {
    if (!canUseExactIdOnlyProvider(request)) {
        return null;
    }
    const { nodeRequest } = request;
    if (!isExactIdPattern(nodeRequest.pattern) || !isIdOnlySelect(nodeRequest.select)) {
        return null;
    }
    return nodeRequest.pattern;
}
function canUseExactIdOnlyProvider(request) {
    return request !== undefined
        && !request.aggregate
        && request.operations.length === 0;
}
function isExactIdPattern(pattern) {
    return typeof pattern === 'string' && !pattern.includes('*');
}
function isIdOnlySelect(select) {
    return select?.length === 1 && select[0] === 'id';
}
export default class CheckpointTailExactIdQueryReadModel {
    _nodeId;
    _optic;
    _readIdentity;
    constructor(options) {
        this._nodeId = options.nodeId;
        this._optic = options.optic;
        this._readIdentity = `${PENDING_IDENTITY}:${options.nodeId}`;
    }
    get stateHash() {
        return this._readIdentity;
    }
    async *nodes(request) {
        if (!this._matchesRequest(request)) {
            return;
        }
        const node = await this._optic.node(this._nodeId).read();
        this._readIdentity = readIdentityStateHash(node.readIdentity);
        if (!node.alive) {
            return;
        }
        yield Object.freeze({
            id: this._nodeId,
            props: emptyProps(),
            edgesOut: Object.freeze([]),
            edgesIn: Object.freeze([]),
        });
    }
    async *neighbors(_nodeId, _options) { }
    nodeProps(_nodeId) {
        return Promise.resolve(null);
    }
    _matchesRequest(request) {
        return request.pattern === this._nodeId
            && request.select?.length === 1
            && request.select[0] === 'id';
    }
}
function emptyProps() {
    return Object.freeze({});
}
function readIdentityStateHash(readIdentity) {
    return `checkpoint-tail-query:${canonicalStringify(readIdentity)}`;
}
