/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { checkRuntimeDependencies, getDescriptor, installDependency } from './dependency-check.js';
async function promptSingleDep(dep, skipPrompt) {
    const descriptor = getDescriptor(dep.id);
    if (!descriptor)
        return { id: dep.id, action: 'skipped' };
    if (!descriptor.installCommand) {
        return { id: dep.id, action: 'skipped', error: descriptor.installHint };
    }
    let shouldInstall = skipPrompt;
    if (!shouldInstall) {
        const { confirm } = await import('@inquirer/prompts');
        shouldInstall = await confirm({
            message: `${descriptor.label} is required but not found. Install via \`${descriptor.installCommand}\`?`,
            default: true,
        });
    }
    if (!shouldInstall)
        return { id: dep.id, action: 'skipped' };
    const installResult = installDependency(dep.id);
    return installResult.success
        ? { id: dep.id, action: 'installed' }
        : { id: dep.id, action: 'failed', error: installResult.error };
}
export function promptInstallMissing(missing, opts) {
    return missing.reduce((chain, dep) => chain.then(async (results) => [...results, await promptSingleDep(dep, opts?.skipPrompt === true)]), Promise.resolve([]));
}
export function detectAvailableRuntimes() {
    const result = checkRuntimeDependencies();
    const available = result.installed.map((d) => d.id);
    const preferred = available.length > 0 ? available[0] : undefined;
    return { available, preferred };
}
export async function ensureRuntime(requiredFor, opts) {
    const { preferred } = detectAvailableRuntimes();
    if (preferred)
        return preferred;
    const result = checkRuntimeDependencies();
    const claudeDep = result.missing.find((d) => d.id === 'claude');
    if (!claudeDep)
        return null;
    const descriptor = getDescriptor('claude');
    let shouldInstall = opts?.skipPrompt === true;
    if (!shouldInstall) {
        const { confirm } = await import('@inquirer/prompts');
        shouldInstall = await confirm({
            message: `No AI runtime found (needed for ${requiredFor}). Install ${descriptor.label} via \`${descriptor.installCommand}\`?`,
            default: true,
        });
    }
    if (!shouldInstall)
        return null;
    const installResult = installDependency('claude');
    return installResult.success ? 'claude' : null;
}
//# sourceMappingURL=dependency-prompt.js.map