/**
 * Auto-Link Helper — shared logic for creating providers for auto-link
 *
 * Provides a helper to instantiate an ExternalProvider from the settings service
 * for use in auto-linking newly created tasks. Used by both the CLI `sf task create`
 * command and the workflow/playbook instantiation route.
 */
import { resolveDatabasePath } from '../db.js';
import { createStorage, initializeSchema } from '@stoneforge/storage';
// ============================================================================
// Helper
// ============================================================================
/**
 * Try to create an ExternalProvider for auto-linking.
 *
 * Reads the provider settings (token, defaultProject) from the settings service,
 * then creates the appropriate provider instance. Returns the provider and project
 * on success, or an error message on failure.
 *
 * @param providerName - The provider to create (e.g., 'github', 'linear')
 * @param options - CLI global options (used to resolve the database path)
 * @returns Result with provider and project, or error
 */
export async function tryCreateProviderForAutoLink(providerName, options) {
    try {
        const dbPath = resolveDatabasePath(options);
        if (!dbPath) {
            return { error: 'No database found' };
        }
        const backend = createStorage({ path: dbPath, create: true });
        initializeSchema(backend);
        // Dynamic import to handle optional peer dependency
        // @ts-ignore — smithy is an optional runtime dependency, may not be installed
        const { createSettingsService } = await import('@stoneforge/smithy/services');
        const settingsService = createSettingsService(backend);
        const providerConfig = settingsService.getProviderConfig(providerName);
        if (!providerConfig?.token) {
            return { error: `Provider "${providerName}" has no token configured` };
        }
        if (!providerConfig.defaultProject) {
            return { error: `Provider "${providerName}" has no default project configured` };
        }
        // Create the provider with the token
        let provider;
        if (providerName === 'github') {
            const { createGitHubProvider } = await import('../../external-sync/providers/github/index.js');
            provider = createGitHubProvider({
                provider: 'github',
                token: providerConfig.token,
                apiBaseUrl: providerConfig.apiBaseUrl,
                defaultProject: providerConfig.defaultProject,
            });
        }
        else if (providerName === 'linear') {
            const { createLinearProvider } = await import('../../external-sync/providers/linear/index.js');
            provider = createLinearProvider({
                apiKey: providerConfig.token,
            });
        }
        else {
            return { error: `Unsupported auto-link provider: ${providerName}` };
        }
        return { provider, project: providerConfig.defaultProject };
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        // If the import fails, the smithy package isn't available
        if (message.includes('Cannot find') || message.includes('MODULE_NOT_FOUND')) {
            return { error: 'Auto-link requires @stoneforge/smithy package' };
        }
        return { error: message };
    }
}
//# sourceMappingURL=auto-link-helper.js.map