import { jsx as _jsx } from "react/jsx-runtime";
import { Sparkles } from "lucide-react";
import { sanitizeSvg } from "../../../lib/sanitize";
/**
 * Handler for "agent" command type.
 * Opens the AI agents panel with a specific profile and prompt.
 */
export const agentCommandHandler = {
    type: "agent",
    disabled: (command, context) => {
        const settings = command.settings;
        // Disable if context mode requires components but none selected
        if (settings.contextMode === "component" && !context.components?.length) {
            return true;
        }
        return false;
    },
    execute: async (command, context) => {
        const { editContext, components, event } = context;
        const settings = command.settings;
        if (event?.preventDefault)
            event.preventDefault();
        if (event?.stopPropagation)
            event.stopPropagation();
        // Select components if provided
        if (components?.length) {
            try {
                editContext.select(components.map((c) => c.id));
            }
            catch {
                // Ignore selection errors
            }
        }
        // Open the Agents panel
        editContext.setShowAgentsPanel?.(true);
        // Build context factory based on contextMode
        let contextFactory;
        switch (settings.contextMode) {
            case "page":
                contextFactory = "page:context:v1";
                break;
            case "component":
                contextFactory = "component:context:v1";
                break;
            case "field":
                contextFactory = "field:context:v1";
                break;
            case "none":
            default:
                contextFactory = undefined;
                break;
        }
        // Delay slightly to ensure the panel mounts before dispatching
        setTimeout(() => {
            try {
                const page = editContext.page?.item?.descriptor;
                const pageName = editContext.item?.name;
                const selection = components?.map((c) => c.id);
                const detail = page
                    ? {
                        metadata: {
                            profile: settings.profileName,
                            additionalData: {
                                profileId: settings.profileId,
                                profileName: settings.profileName,
                                contextFactory,
                                initialPrompt: settings.initialPrompt,
                                context: {
                                    pages: [
                                        {
                                            id: page.id,
                                            language: page.language,
                                            version: page.version,
                                            name: pageName,
                                        },
                                    ],
                                    componentIds: selection && selection.length ? selection : undefined,
                                },
                            },
                        },
                    }
                    : {
                        metadata: {
                            profile: settings.profileName,
                            additionalData: {
                                profileId: settings.profileId,
                                profileName: settings.profileName,
                                contextFactory,
                                initialPrompt: settings.initialPrompt,
                            },
                        },
                    };
                window.dispatchEvent(new CustomEvent("editor:addNewAgent", { detail }));
            }
            catch {
                // Ignore errors
            }
            try {
                window.dispatchEvent(new CustomEvent("editor:focusAgentPrompt"));
            }
            catch {
                // Ignore errors
            }
        }, 100);
    },
    renderIcon: (command) => {
        if (command.svgIcon) {
            return (_jsx("span", { className: "custom-command-icon", dangerouslySetInnerHTML: { __html: sanitizeSvg(command.svgIcon) } }));
        }
        return _jsx(Sparkles, { size: 16, strokeWidth: 1 });
    },
};
//# sourceMappingURL=agentHandler.js.map