"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.elementRefToTarget = void 0;
/**
 * Map a comment's ElementRef to a single `propose_variants` target.
 *
 * Rules (first non-empty wins, no concatenation):
 *  1. `rivetId` → `{ type: 'element', ref: '[data-rivet-id="<id>"]' }`
 *  2. `id` (no whitespace) → `{ type: 'element', ref: '#<id>' }`
 *  3. `xpath` → `{ type: 'element', ref: '<xpath>' }`
 *  4. `className` (single dominant token) → `{ type: 'element', ref: '.<token>' }`
 *
 * If `element.filePaths.length === 1`, prefer `{ type: 'file', ref: filePaths[0] }`
 * for refinement scope. The original ElementRef selector should still appear
 * verbatim in the LLM brief prompt — callers are responsible for that.
 *
 * Returns null when none of the four rules produce a usable ref. Callers should
 * surface a structured error to the user rather than calling propose_variants
 * with a fabricated target.
 */
const elementRefToTarget = (ref) => {
    if (ref.filePaths.length === 1 && ref.filePaths[0]) {
        return { type: 'file', ref: ref.filePaths[0] };
    }
    const rivetId = ref.rivetId?.trim();
    if (rivetId) {
        return { type: 'element', ref: `[data-rivet-id="${rivetId}"]` };
    }
    const id = ref.id?.trim();
    if (id && !/\s/.test(id)) {
        return { type: 'element', ref: `#${id}` };
    }
    const xpath = ref.xpath?.trim();
    if (xpath) {
        return { type: 'element', ref: xpath };
    }
    const className = ref.className?.trim();
    if (className) {
        const tokens = className.split(/\s+/).filter(Boolean);
        if (tokens.length === 1) {
            return { type: 'element', ref: `.${tokens[0]}` };
        }
    }
    return null;
};
exports.elementRefToTarget = elementRefToTarget;
//# sourceMappingURL=elementRefToTarget.js.map