import { launchPreparedPlaygroundPlatform } from "./platform-launcher.mjs";
function prefixFieldKey(platformId, key) {
    return `${platformId}.${key}`;
}
function prefixField(platformId, field) {
    return {
        ...field,
        key: prefixFieldKey(platformId, field.key)
    };
}
function mapPlatformOption(platform) {
    const descriptionParts = [
        platform.description
    ];
    if (platform.unavailableReason) descriptionParts.push(platform.unavailableReason);
    return {
        label: platform.label,
        value: platform.id,
        description: descriptionParts.filter(Boolean).join(' · ') || void 0
    };
}
function mapPlatformRegistration(platform) {
    return {
        id: platform.id,
        label: platform.label,
        description: platform.description,
        unavailableReason: platform.unavailableReason,
        supportsStandalone: platform.supportsStandalone,
        metadata: platform.metadata
    };
}
function pickPrefixedInput(input, platformId) {
    const result = {};
    const prefix = `${platformId}.`;
    Object.entries(input || {}).forEach(([key, value])=>{
        if (key.startsWith(prefix)) result[key.slice(prefix.length)] = value;
    });
    return result;
}
function buildPlatformSelectorField(platforms, selectorFieldKey) {
    return {
        key: selectorFieldKey,
        label: 'Platform',
        type: 'select',
        required: true,
        options: platforms.map(mapPlatformOption),
        placeholder: 'Select a platform'
    };
}
function normalizeChildSession(prepared, created) {
    return {
        ...created,
        platformId: created.platformId || prepared.platformId,
        title: created.title || prepared.title,
        platformDescription: created.platformDescription || prepared.description,
        preview: created.preview || prepared.preview,
        executionHooks: created.executionHooks || prepared.executionHooks,
        sidecars: created.sidecars || prepared.sidecars,
        metadata: {
            ...prepared.metadata || {},
            ...created.metadata || {}
        }
    };
}
function buildDirectPreparedSession(prepared) {
    if (!prepared.agent && !prepared.agentFactory) throw new Error(`Platform "${prepared.platformId}" does not expose a session manager or agent source`);
    return {
        agent: prepared.agent,
        agentFactory: prepared.agentFactory,
        displayName: prepared.title,
        metadata: {
            ...prepared.metadata || {}
        },
        platformId: prepared.platformId,
        title: prepared.title,
        platformDescription: prepared.description,
        preview: prepared.preview,
        executionHooks: prepared.executionHooks,
        sidecars: prepared.sidecars
    };
}
async function prepareMultiPlatformPlayground(platforms, options = {}) {
    const registryMap = new Map(platforms.map((platform)=>[
            platform.id,
            platform
        ]));
    const preparedCache = new Map();
    const activePlatformState = {};
    const selectorFieldKey = options.selectorFieldKey || 'platformId';
    const platformRegistry = platforms.map(mapPlatformRegistration);
    const getPreparedPlatform = async (platformId)=>{
        const cached = preparedCache.get(platformId);
        if (cached) return cached;
        const registration = registryMap.get(platformId);
        if (!registration) throw new Error(`Unknown platform: ${platformId}`);
        if (registration.unavailableReason) throw new Error(registration.unavailableReason);
        const prepared = await registration.prepare(registration.options);
        preparedCache.set(platformId, prepared);
        return prepared;
    };
    const sessionManager = {
        async getSetupSchema (input) {
            const platformId = 'string' == typeof input?.[selectorFieldKey] ? String(input[selectorFieldKey]) : void 0;
            const baseField = buildPlatformSelectorField(platforms, selectorFieldKey);
            if (!platformId) return {
                title: 'Choose a platform',
                description: 'Select a platform first, then complete its connection fields.',
                primaryActionLabel: 'Continue',
                fields: [
                    baseField
                ],
                platformRegistry,
                platformSelector: {
                    fieldKey: selectorFieldKey,
                    variant: options.selectorVariant || 'cards'
                }
            };
            const registration = registryMap.get(platformId);
            if (!registration) return {
                title: 'Choose a platform',
                description: `Unknown platform: ${platformId}`,
                primaryActionLabel: 'Continue',
                fields: [
                    baseField
                ],
                platformRegistry,
                platformSelector: {
                    fieldKey: selectorFieldKey,
                    variant: options.selectorVariant || 'cards'
                }
            };
            if (registration.unavailableReason) return {
                title: `Platform unavailable: ${registration.label}`,
                description: registration.unavailableReason,
                primaryActionLabel: 'Create Agent',
                fields: [
                    baseField
                ],
                platformRegistry,
                platformSelector: {
                    fieldKey: selectorFieldKey,
                    variant: options.selectorVariant || 'cards'
                }
            };
            const prepared = await getPreparedPlatform(platformId);
            const childSetup = prepared.sessionManager?.getSetupSchema ? await prepared.sessionManager.getSetupSchema(pickPrefixedInput(input, platformId)) : void 0;
            return {
                title: childSetup?.title || `Connect ${registration.label}`,
                description: childSetup?.description || registration.description,
                primaryActionLabel: childSetup?.primaryActionLabel || 'Create Agent',
                fields: [
                    {
                        ...baseField,
                        defaultValue: platformId
                    },
                    ...(childSetup?.fields || []).map((field)=>prefixField(platformId, field))
                ],
                targets: childSetup?.targets,
                platformRegistry,
                platformSelector: {
                    fieldKey: selectorFieldKey,
                    variant: options.selectorVariant || 'cards'
                },
                notice: childSetup?.notice
            };
        },
        async createSession (input) {
            const platformId = 'string' == typeof input?.[selectorFieldKey] ? String(input[selectorFieldKey]) : void 0;
            if (!platformId) throw new Error(`${selectorFieldKey} is required`);
            const prepared = await getPreparedPlatform(platformId);
            const childInput = pickPrefixedInput(input, platformId);
            activePlatformState.platformId = platformId;
            activePlatformState.sessionManager = prepared.sessionManager;
            if (prepared.sessionManager) return normalizeChildSession(prepared, await prepared.sessionManager.createSession(childInput));
            return buildDirectPreparedSession(prepared);
        },
        async destroySession (session) {
            const platformId = activePlatformState.platformId;
            if (!platformId) return;
            const prepared = preparedCache.get(platformId);
            const childSessionManager = activePlatformState.sessionManager || prepared?.sessionManager;
            activePlatformState.platformId = void 0;
            activePlatformState.sessionManager = void 0;
            await childSessionManager?.destroySession?.(session);
        }
    };
    return {
        platformId: options.platformId || 'multi-platform',
        title: options.title || 'Midscene Playground',
        description: options.description || 'Unified playground for multiple registered platforms',
        sessionManager,
        launchOptions: options.launchOptions,
        metadata: {
            sessionConnected: false,
            setupState: 'required',
            availablePlatforms: platformRegistry.map((platform)=>({
                    id: platform.id,
                    label: platform.label,
                    unavailableReason: platform.unavailableReason
                })),
            ...options.metadata || {}
        }
    };
}
function playgroundForPlatforms(platforms, options = {}) {
    return {
        async launch (overrides = {}) {
            const prepared = await prepareMultiPlatformPlayground(platforms, options);
            return launchPreparedPlaygroundPlatform(prepared, overrides);
        }
    };
}
export { playgroundForPlatforms, prepareMultiPlatformPlayground };
