"use strict";
/**
 * Folder-to-profile routing rules.
 *
 * Used by the `ccs rule` command and the generated claude() shell wrapper to
 * pick a profile based on the directory the user launches Claude from.
 * Matching is "longest path wins": the most specific matching rule is used,
 * and when nothing matches the wrapper falls back to plain `claude`.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.matchFolderRule = exports.normalizeFolderRules = void 0;
/**
 * Coerce arbitrary parsed YAML into a clean FolderRule[].
 * Drops malformed entries; returns undefined when nothing valid remains so the
 * serializer can omit an empty `rules:` block.
 */
function normalizeFolderRules(value) {
    if (!Array.isArray(value)) {
        return undefined;
    }
    const rules = [];
    for (const entry of value) {
        if (!entry || typeof entry !== 'object') {
            continue;
        }
        const record = entry;
        const rulePath = typeof record.path === 'string' ? record.path.trim() : '';
        const profile = typeof record.profile === 'string' ? record.profile.trim() : '';
        if (rulePath && profile) {
            rules.push({ path: rulePath, profile });
        }
    }
    return rules.length > 0 ? rules : undefined;
}
exports.normalizeFolderRules = normalizeFolderRules;
/**
 * Resolve the matching rule for a target path using "longest path wins".
 * A rule matches when the target equals its path or sits inside it on a path
 * boundary (so /a/b does not match /a/bc).
 */
function matchFolderRule(rules, targetPath) {
    let best;
    let bestLength = -1;
    for (const rule of rules) {
        const base = rule.path;
        const withSep = base.endsWith('/') ? base : `${base}/`;
        if (targetPath === base || targetPath.startsWith(withSep)) {
            if (base.length > bestLength) {
                bestLength = base.length;
                best = rule;
            }
        }
    }
    return best;
}
exports.matchFolderRule = matchFolderRule;
//# sourceMappingURL=rules.js.map