'use server';
import { resolve } from '../../lib/server-container.js';
import { ApplicationNotFoundError } from '../../../../../packages/core/src/domain/errors/application-not-found.error.js';
import { ApplicationRepositoryNotOnDiskError } from '../../../../../packages/core/src/domain/errors/application-repository-not-on-disk.error.js';
import { CannotDeploySelfError } from '../../../../../packages/core/src/domain/errors/cannot-deploy-self.error.js';
/**
 * Start a local dev server for an application.
 *
 * Thin server action: parses input, delegates to
 * `StartApplicationDeploymentUseCase`, and maps domain errors to the
 * action's historical response shape.
 *
 * All orchestration (repo lookup, filesystem precondition, self-instance
 * guard, `.start()` call, logging) lives in the use case — see
 * `packages/core/src/application/use-cases/deployments/
 * start-application-deployment.use-case.ts`.
 */
export async function deployApplication(applicationId) {
    if (!applicationId?.trim()) {
        return { success: false, error: 'applicationId is required' };
    }
    try {
        const useCase = resolve('StartApplicationDeploymentUseCase');
        const result = await useCase.execute({ applicationId });
        return { success: true, state: result.state };
    }
    catch (error) {
        if (error instanceof ApplicationNotFoundError) {
            return { success: false, error: `Application not found: ${applicationId}` };
        }
        if (error instanceof ApplicationRepositoryNotOnDiskError) {
            return {
                success: false,
                error: `Repository path does not exist: ${error.repositoryPath}`,
            };
        }
        if (error instanceof CannotDeploySelfError) {
            return {
                success: false,
                error: 'Cannot start a dev server for the repository Shep is running from',
            };
        }
        const message = error instanceof Error ? error.message : 'Failed to deploy application';
        return { success: false, error: message };
    }
}
