/**
 * Server-side validation for dataset cell-anchored comments.
 *
 * The full anchor model + the (pure, client-side) resolver live in the web
 * app at `web/src/lib/utils/cell-anchor.ts` — resolution recomputes
 * `{row, col}` against the live grid on every render and never runs on the
 * server. The server's only job is to guard the write path: a cell comment
 * stores its anchor JSON in the existing `anchor_relpos` column, so we must
 * make sure (a) only datasets receive cell (JSON) anchors and (b) the JSON is
 * structurally well-formed, so a markdown comment's base64 relpos is never
 * confused for a cell anchor and a cell anchor is never fed to the text
 * resolver. The two copies are intentionally small and independent (the web
 * app does not import from `src/`).
 */
/** Cheap discriminator: a cell anchor is JSON (`{…}`); a markdown comment's
 *  anchor is a base64 relpos blob, which never starts with `{`. */
export function looksLikeCellAnchor(relpos) {
    return !!relpos && relpos[0] === '{';
}
export function isValidCellAnchor(value) {
    if (typeof value !== 'object' || value === null)
        return false;
    const a = value;
    if (a.kind !== 'cell')
        return false;
    if (typeof a.rowHash !== 'string' || a.rowHash.length === 0)
        return false;
    if (typeof a.columnIndex !== 'number' || !Number.isInteger(a.columnIndex) || a.columnIndex < 0)
        return false;
    if (typeof a.headerName !== 'string')
        return false;
    if (a.rowCells !== undefined) {
        if (!Array.isArray(a.rowCells))
            return false;
        if (!a.rowCells.every((c) => typeof c === 'string'))
            return false;
    }
    return true;
}
/** Parse + validate a stored anchor string as a cell anchor, or null. */
export function parseCellAnchor(relpos) {
    if (!looksLikeCellAnchor(relpos))
        return null;
    let parsed;
    try {
        parsed = JSON.parse(relpos);
    }
    catch {
        return null;
    }
    return isValidCellAnchor(parsed) ? parsed : null;
}
//# sourceMappingURL=cell-anchor.js.map