export class CliV2PickerService {
    static modelQuery(draft) {
        return queryAfterPrefix(draft, '/model set');
    }
    static sessionQuery(draft) {
        return queryAfterPrefix(draft, '/session choose');
    }
    static reasoningQuery(draft) {
        return queryAfterPrefix(draft, '/reasoning set');
    }
    static permissionModeQuery(draft) {
        return queryAfterPrefix(draft, '/permissions set');
    }
    static filterModels(modelOptions, query) {
        if (query === undefined) {
            return [];
        }
        const normalized = normalize(query);
        const models = modelOptions?.groups.flatMap((group) => group.options) ?? [];
        if (!normalized) {
            return models;
        }
        return models.filter((model) => matchesNormalizedQuery([model.id, model.label], normalized));
    }
    static filterSessions(sessions, query) {
        if (query === undefined) {
            return [];
        }
        const normalized = normalize(query);
        if (!normalized) {
            return sessions.map(({ id, name }) => ({ id, name }));
        }
        return sessions
            .filter((session) => matchesNormalizedQuery([session.id, session.name], normalized))
            .map(({ id, name }) => ({ id, name }));
    }
    static filterReasoningOptions(runtimeContext, query) {
        if (query === undefined) {
            return [];
        }
        const normalized = normalize(query);
        const options = runtimeContext?.reasoningOptions ?? [];
        if (!normalized) {
            return options;
        }
        return options.filter((option) => matchesNormalizedQuery([
            option.id,
            option.label,
            option.description,
        ], normalized));
    }
    static filterPermissionModes(runtimeContext, query) {
        if (query === undefined) {
            return [];
        }
        const normalized = normalize(query);
        const options = runtimeContext?.permissionModeOptions ?? [];
        if (!normalized) {
            return options;
        }
        return options.filter((option) => matchesNormalizedQuery([
            option.id,
            option.label,
            option.description,
            option.disabledReason,
        ], normalized));
    }
    static permissionModeInitialIndex(runtimeContext, query) {
        const options = CliV2PickerService.filterPermissionModes(runtimeContext, query);
        const activePermissionMode = runtimeContext?.permissionMode ?? 'default';
        return Math.max(0, options.findIndex((option) => option.id === activePermissionMode));
    }
    static clampIndex(index, itemCount) {
        return itemCount === 0 ? 0 : Math.min(index, Math.max(0, itemCount - 1));
    }
    static nextIndex(index, itemCount) {
        return itemCount === 0 ? 0 : (index + 1) % itemCount;
    }
    static previousIndex(index, itemCount) {
        return itemCount === 0 ? 0 : index <= 0 ? itemCount - 1 : index - 1;
    }
}
function queryAfterPrefix(draft, prefix) {
    const trimmedStart = draft.trimStart();
    if (!trimmedStart.startsWith(prefix)) {
        return undefined;
    }
    return trimmedStart.slice(prefix.length).trim();
}
function normalize(value) {
    return value.trim().toLowerCase();
}
function matchesNormalizedQuery(values, normalizedQuery) {
    return values.some((value) => value?.toLowerCase().includes(normalizedQuery) ?? false);
}
//# sourceMappingURL=picker-service.js.map