// Figma readiness resolution (Epic #750, Issue #751).
//
// Pure, deterministic. Resolves whether a scoped node subtree is "release-ready" using a
// fixed precedence with graceful fallback:
//
//   1. version  — a pinned version id was supplied (operator pinned a named version).
//   2. section  — a node anywhere in the subtree is named with a configurable release
//                 marker (default case-insensitive substring `release`).
//   3. devStatus — one or more nodes carry Dev-Mode `devStatus.type === "READY_FOR_DEV"`.
//
// `devStatus` is OPTIONAL in the Figma REST response and may be absent for an entire
// account/plan. When it is absent (or no node is READY_FOR_DEV) and the higher signals
// did not fire, the result degrades to `{ source: "none", ready: false }`. The connector
// still returns the scoped subtree — readiness is advisory provenance, not a gate here.
const DEFAULT_RELEASE_MARKER = "release";
const READY_FOR_DEV = "READY_FOR_DEV";
const walk = (node, visit) => {
    if (visit(node))
        return true;
    for (const child of node.children ?? []) {
        if (walk(child, visit))
            return true;
    }
    return false;
};
const findReleaseNamedNode = (root, marker) => {
    const needle = marker.toLowerCase();
    let match;
    walk(root, (node) => {
        if (node.name.toLowerCase().includes(needle)) {
            match = node;
            return true;
        }
        return false;
    });
    return match;
};
const countReadyForDevNodes = (root) => {
    let count = 0;
    walk(root, (node) => {
        if (node.devStatus?.type === READY_FOR_DEV)
            count += 1;
        return false;
    });
    return count;
};
export const resolveReadiness = (root, options) => {
    if (options.version !== undefined && options.version.length > 0) {
        return { source: "version", ready: true, version: options.version };
    }
    const marker = options.releaseMarker ?? DEFAULT_RELEASE_MARKER;
    const namedNode = findReleaseNamedNode(root, marker);
    if (namedNode !== undefined) {
        return { source: "section", ready: true, matchedNodeName: namedNode.name };
    }
    const readyNodeCount = countReadyForDevNodes(root);
    if (readyNodeCount > 0) {
        return { source: "devStatus", ready: true, readyNodeCount };
    }
    return { source: "none", ready: false };
};
