import { jsx as _jsx } from "react/jsx-runtime";
import { resolve } from '../../../../lib/server-container.js';
import { getSettings } from '../../../../../../../packages/core/src/infrastructure/services/settings.service.js';
import { getWorkflowDefaults } from '../../../actions/get-workflow-defaults.js';
import { getViewerPermission } from '../../../actions/get-viewer-permission.js';
import { CreateDrawerClient } from '../../../../components/common/control-center-drawer/create-drawer-client.js';
import { URL_PARAMS } from '../../../../lib/url-params.js';
/** Skip static pre-rendering since we need runtime DI container. */
export const dynamic = 'force-dynamic';
function parseMode(raw) {
    if (raw === 'application' || raw === 'fast' || raw === 'spec')
        return raw;
    return undefined;
}
export default async function CreateDrawerPage({ searchParams }) {
    const params = await searchParams;
    const repo = params[URL_PARAMS.repo];
    const parent = params[URL_PARAMS.parent];
    const prompt = params[URL_PARAMS.prompt];
    const mode = params[URL_PARAMS.mode];
    const applicationId = params[URL_PARAMS.applicationId];
    const initialMode = parseMode(mode);
    const listFeatures = resolve('ListFeaturesUseCase');
    const listRepos = resolve('ListRepositoriesUseCase');
    const settings = getSettings();
    // When an applicationId is supplied, look up the Application server-side so
    // the drawer can pre-fill repositoryPath and pin spec mode. A malformed id
    // or a missing application falls back to non-scoped mode (the drawer is a
    // parallel-slot overlay — a hard 404 here would hide the underlying canvas).
    const applicationPromise = applicationId
        ? resolve('GetApplicationUseCase')
            .execute(applicationId)
            .catch(() => null)
        : Promise.resolve(null);
    const [features, repositories, workflowDefaults, viewerPerm, application] = await Promise.all([
        listFeatures.execute(),
        listRepos.execute().catch(() => []),
        getWorkflowDefaults().catch(() => undefined),
        repo
            ? getViewerPermission(repo).catch(() => ({ canPushDirectly: false }))
            : Promise.resolve({ canPushDirectly: false }),
        applicationPromise,
    ]);
    const featureOptions = features
        .map((f) => ({ id: f.id, name: f.name }))
        .filter((f) => f.id && !f.id.startsWith('#'));
    const repositoryOptions = repositories.map((r) => ({
        id: r.id,
        name: r.name,
        path: r.path,
    }));
    // Application-scoped overrides: the entry-point-determined fields are
    // pinned, everything else stays user-editable per the spec's resolved Q6.
    // The URL `applicationId` is the canonical scope signal — it locks the
    // drawer even if the server-side Application lookup couldn't resolve a
    // repository path (e.g., DI not yet warm in tests).
    const scopedRepositoryPath = application?.repositoryPath ?? repo ?? '';
    const scopedApplicationId = application?.id ?? applicationId;
    return (_jsx(CreateDrawerClient, { repositoryPath: scopedRepositoryPath, initialParentId: parent, initialDescription: prompt, initialMode: initialMode, initialApplicationId: scopedApplicationId, features: featureOptions, repositories: repositoryOptions, workflowDefaults: workflowDefaults, currentAgentType: settings.agent.type, currentModel: settings.models.default, canPushDirectly: viewerPerm.canPushDirectly }));
}
