import { JSX as JSX_2 } from 'react/jsx-runtime';
import { MemoExoticComponent } from 'react';
import { ReactNode } from 'react';

export declare type AgentInterruptEvent = EvaluationResultElement | DoneElement | StatusUpdateElement | PlanUpdateElement | ErrorElement | ToolResultElement | ToolCallElement | AgentMessage_2 | ContextualKnowledgeElement | PausedElement;

export declare const AgentMessage: MemoExoticComponent<({ message, isSelected, onClick, annotationOverlay, selectionIndicator, className, hideMetadata, hideActions, showBorder, onOpenToolsPanel, onOpenPlanPanel, onOpenCustomWidget, selectedCustomWidget }: AgentMessageProps) => JSX_2.Element>;

declare type AgentMessage_2 = ConversationElement & {
    type: ConversationElementType.AGENT_MESSAGE;
    content: string;
    subtype: 'thinking_started' | 'thinking_completed' | 'guardrail_started' | 'guardrail_ended' | 'strategy_updated' | 'thinking_failed' | 'intermediate' | 'final' | 'question' | 'workflow_task' | 'final_stream' | 'attach_files' | 'output_files' | 'ask_permission' | 'ask_permission_result' | 'ask_credential' | 'ask_credential_result';
    isStreaming?: boolean;
    relatedStepId?: string;
    expectedResponseType?: 'text' | 'boolean' | 'selection' | 'json';
    options?: string[];
    taskData?: Record<string, any>;
    metadata?: {
        [key: string]: any;
        documents?: AttachedDocument[];
        requestedSchema?: any;
        permissionRequest?: PermissionRequest;
        credentialRequest?: CredentialsRequest;
    };
    parentMessageId?: string;
};

export declare type AgentMessageAnnotationProps = {
    isSelected?: boolean;
    onClick?: () => void;
    annotationOverlay?: ReactNode;
    selectionIndicator?: ReactNode;
    className?: string;
    hideMetadata?: boolean;
    hideActions?: boolean;
    showBorder?: boolean;
    message: SdkAgentMessage;
};

export declare type AgentMessageContextProps = {
    onOpenToolsPanel?: (message: SdkAgentMessage) => void;
    onOpenPlanPanel?: (message: SdkAgentMessage) => void;
    onOpenCustomWidget?: (widget: SdkCustomWidget) => void;
    selectedCustomWidget?: SdkCustomWidget;
};

declare type AgentMessageProps = AgentMessageAnnotationProps & AgentMessageContextProps;

declare enum AgentResponseFormat {
    Json = "Json",
    Markdown = "Markdown"
}

declare type AiMessage = EvaluationResultElement | DoneElement | StatusUpdateElement | PlanUpdateElement | ErrorElement | ToolResultElement | ToolCallElement | AgentMessage_2 | ContextualKnowledgeElement | PausedElement;

declare type AttachedDocument = {
    id: string;
    sourceDocumentId?: string;
    fileName?: string;
    summary?: string;
    mimeType?: string;
    source?: string;
    state?: DocumentProcessingState;
    error?: string;
};

export declare type ChatMessage = UserMessage_2 | AiMessage;

export declare const ChatRightPanel: ({ isOpen: externalIsOpen, onClose: externalOnClose, selectedTab: externalSelectedTab, onTabChange: externalOnTabChange, selectedMessage: externalSelectedMessage, container: externalContainer, useThinkingState }?: ChatRightPanelProps) => JSX_2.Element;

export declare type ChatRightPanelProps = {
    isOpen?: boolean;
    onClose?: () => void;
    selectedTab?: ChatRightPanelTab;
    onTabChange?: (tab: ChatRightPanelTab) => void;
    selectedMessage?: SdkAgentMessage;
    container?: HTMLElement | null;
    useThinkingState?: boolean;
};

export declare enum ChatRightPanelTab {
    Plan = "Plan",
    Tools = "Tools",
    ContextualKnowledge = "ContextualKnowledge"
}

export declare const ChatWidget: ({ onClose, embedded, ...props }: ChatWidgetProps) => JSX_2.Element;

declare type ChatWidgetContainerProps = (ChatWidgetPopupContainerProps & {
    embedded?: false;
}) | (ChatWidgetEmbeddedContainerProps & {
    embedded: true;
});

declare type ChatWidgetEmbeddedContainerProps = {
    children: React.ReactNode;
    onClose?: () => void;
    widgetRef?: React.Ref<ChatWidgetRef>;
};

declare type ChatWidgetPopupContainerProps = {
    children: React.ReactNode;
    defaultOpen?: boolean;
    onClose?: () => void;
    widgetRef?: React.Ref<ChatWidgetRef>;
};

export declare type ChatWidgetProps = CommonWidgetProps & Omit<ChatWidgetContainerProps, 'children'> & {
    embedded?: boolean;
};

export declare type ChatWidgetRef = {
    open: () => void;
    close: () => void;
};

declare type CommonWidgetProps = {
    instanceId?: string;
    config: WidgetConfig;
    user: WidgetUser;
    theme?: WidgetTheme;
    defaultExecutionId?: string;
    onExecutionChange?: (executionId?: string) => void;
    disableFilterConversationsByUser?: boolean;
    responseFormat?: AgentResponseFormat;
    enableViewOnObservability?: boolean;
    isConsumer?: boolean;
};

export declare type ContextualKnowledge = {
    knowledge: string;
    userRequest: string;
    timestamp: string;
};

declare type ContextualKnowledgeElement = ConversationElement & ContextualKnowledge & {
    type: ConversationElementType.CONTEXTUAL_KNOWLEDGE;
};

declare type ConversationElement = {
    id: string;
    type: ConversationElementType;
    timestamp: Date;
    conversationId?: string;
    metadata?: Record<string, any>;
};

declare enum ConversationElementType {
    USER_MESSAGE = "user_message",
    AGENT_MESSAGE = "agent_message",
    TOOL_CALL = "tool_call",
    TOOL_RESULT = "tool_result",
    PLAN_UPDATE = "plan_update",
    STATUS_UPDATE = "status_update",
    CONTEXTUAL_KNOWLEDGE = "contextual_knowledge",
    EVALUATION_RESULT = "evaluation_result",
    PAUSED = "paused",
    DONE = "done",
    ERROR = "error"
}

export declare const createHeadlessAgentInstance: (params: CreateHeadlessAgentInstanceParams) => HeadlessAgentInstance;

export declare type CreateHeadlessAgentInstanceParams = Pick<CreateWidgetInstanceParams, 'config' | 'user' | 'instanceId'>;

declare type CreateWidgetInstanceParams = Omit<CommonWidgetProps, 'theme'> & {
    instanceId: string;
};

declare type CredentialsRequest = {
    id: string;
    title: string;
    description: string;
    toolCallId?: string;
    serviceId?: IntegrationServiceId;
};

declare enum DocumentProcessingState {
    Pending = "Pending",
    Uploaded = "Uploaded",
    SummaryProcessing = "SummaryProcessing",
    SummaryProcessed = "SummaryProcessed",
    SummaryFailed = "SummaryFailed"
}

declare interface DoneElement extends ConversationElement {
    type: ConversationElementType.DONE;
    plan?: Plan;
    content?: string;
    error?: string;
    usedMemories?: UsedMemories;
}

declare type ErrorElement = ConversationElement & {
    type: ConversationElementType.ERROR;
    error: string;
    source: 'agent' | 'tool' | 'system' | 'external';
    context?: {
        toolName?: string;
        stepId?: string;
        originalRequest?: string;
    };
};

declare type EvaluationResponse = {
    score: number;
    reasoning: string;
    is_correct: boolean;
};

declare interface EvaluationResultElement extends ConversationElement {
    type: ConversationElementType.EVALUATION_RESULT;
    result: EvaluationResponse;
}

declare type FullChatWidgetContainerProps = {
    children: React.ReactNode;
    defaultOpen?: boolean;
    hideTrigger?: boolean;
    onClose?: () => void;
    widgetRef?: React.Ref<FullScreenChatWidgetRef>;
};

export declare const FullScreenAnnotationChatWidget: ({ defaultOpen, widgetRef, onClose, hideTrigger, ...props }: FullScreenAnnotationChatWidgetProps) => JSX_2.Element;

declare type FullScreenAnnotationChatWidgetContainerProps = {
    children: React.ReactNode;
    defaultOpen?: boolean;
    hideTrigger?: boolean;
    onClose?: () => void;
    widgetRef?: React.Ref<FullScreenAnnotationChatWidgetRef>;
};

export declare type FullScreenAnnotationChatWidgetProps = CommonWidgetProps & Omit<FullScreenAnnotationChatWidgetContainerProps, 'children'>;

export declare type FullScreenAnnotationChatWidgetRef = {
    open: () => void;
    close: () => void;
};

export declare const FullScreenChatWidget: ({ defaultOpen, widgetRef, onClose, hideTrigger, ...props }: FullScreenChatWidgetProps) => JSX_2.Element;

export declare type FullScreenChatWidgetProps = CommonWidgetProps & Omit<FullChatWidgetContainerProps, 'children'>;

export declare type FullScreenChatWidgetRef = {
    open: () => void;
    close: () => void;
};

export declare type HeadlessAgentInstance = {
    emit: (event: 'query', payload: QueryPayload) => void;
    on: <T extends HeadlessEventType>(event: T, handler: HeadlessEventHandler<T>) => () => void;
    cleanup: () => void;
};

declare type HeadlessEventHandler<T extends HeadlessEventType> = T extends 'agent-interrupt' ? (message: AgentInterruptEvent) => void : never;

declare type HeadlessEventType = 'agent-interrupt';

declare type IntegrationServiceId = 'twilio-whatsapp' | 'resend' | 'stripe' | 'redtail';

export declare type IntermediateAgentMessage = Omit<SdkAgentMessage, 'intermediateMessages'>;

declare type PausedElement = ConversationElement & {
    type: ConversationElementType.PAUSED;
    resumeInfo: any;
};

declare type PermissionRequest = {
    id: string;
    title: string;
    description: string;
    toolCallId?: string;
};

export declare type Plan = {
    id: string;
    status: 'running' | 'completed' | 'failed';
    steps: PlanStep[];
};

export declare type PlanStep = {
    id: string;
    description: string;
    status: 'pending' | 'running' | 'paused' | 'completed' | 'failed' | 'waiting_for_input';
};

declare type PlanUpdateElement = ConversationElement & {
    type: ConversationElementType.PLAN_UPDATE;
    plan?: Plan;
    changeType: 'created' | 'updated' | 'step_added' | 'step_updated' | 'completed';
};

export declare type QueryPayload = {
    content: string;
    executionId?: string;
    files?: File[];
    documents?: {
        signedUrl: string;
        fileName?: string;
        mimeType?: string;
    }[];
};

declare type SdkAgentGuardrailMessage = {
    id: string;
    intent: string;
    result?: string;
    startTime: Date;
    endTime?: Date;
    metadata?: Record<string, any>;
    type: SdkAgentStepType.GUARDRAIL;
};

export declare type SdkAgentMessage = Pick<AgentMessage_2, 'content' | 'id' | 'timestamp' | 'metadata'> & {
    type: 'agent';
    subtype?: AgentMessage_2['subtype'];
    outputFiles?: AgentMessage_2[];
    plan?: Plan;
    thinking?: {
        startTime: Date;
        endTime: Date;
    };
    contextualKnowledge?: ContextualKnowledge;
    usedMemories?: UsedMemories;
    isStreaming?: boolean;
    intermediateMessages?: IntermediateAgentMessage[];
    agentSteps?: SdkAgentStep[];
    selectedStrategy?: string;
};

declare type SdkAgentStep = SdkTool | SdkAgentThinkingMessage | SdkAgentGuardrailMessage | SdkContextualKnowledge;

declare enum SdkAgentStepType {
    TOOL_CALL = "TOOL_CALL",
    THINKING = "THINKING",
    GUARDRAIL = "GUARDRAIL",
    CONTEXTUAL_KNOWLEDGE = "CONTEXTUAL_KNOWLEDGE"
}

declare type SdkAgentThinkingMessage = {
    id: string;
    intent: string;
    thoughtProcess?: string;
    error?: string;
    startTime: Date;
    endTime?: Date;
    metadata?: Record<string, any>;
    type: SdkAgentStepType.THINKING;
};

export declare type SdkChatMessage = SdkUserMessage | SdkAgentMessage;

declare type SdkContextualKnowledge = {
    id: string;
    knowledge: string;
    userRequest: string;
    timestamp: string;
    intent: string;
    startTime: Date;
    endTime?: Date;
    metadata?: Record<string, any>;
    type: SdkAgentStepType.CONTEXTUAL_KNOWLEDGE;
};

export declare type SdkCustomWidget = {
    widgetId: string;
    toolResult?: any;
    uri: string;
    title: string;
    timestamp: Date;
    toolId: string;
    metadata?: Record<string, any>;
};

export declare type SdkTool = {
    id: string;
    inputs: Record<string, any>;
    intent?: string;
    name: string;
    displayName?: string;
    startTime: Date;
    endTime?: Date;
    result?: any;
    widget?: ToolWidgetMetaData;
    metadata?: Record<string, any>;
    type: SdkAgentStepType.TOOL_CALL;
    error?: string;
};

export declare type SdkUserMessage = Pick<UserMessage_2, 'metadata' | 'id' | 'content' | 'timestamp'> & {
    type: 'user';
    status?: 'pending' | 'acknowledged' | 'error' | 'failed';
};

declare interface StatusUpdateElement extends ConversationElement {
    type: ConversationElementType.STATUS_UPDATE;
    status: string;
    context?: string;
    relatedStepId?: string;
}

declare type ToolCallElement = ConversationElement & {
    type: ConversationElementType.TOOL_CALL;
    skillId: string;
    toolName: string;
    displayName?: string;
    intent?: string;
    inputs: Record<string, any>;
    status: 'pending' | 'executing' | 'completed' | 'failed';
    metadata?: Record<string, any>;
    relatedStepId?: string;
};

declare type ToolResultElement = ConversationElement & {
    type: ConversationElementType.TOOL_RESULT;
    toolCallId: string;
    result: any;
    success: boolean;
    error?: string;
    duration?: number;
    metadata?: Record<string, unknown>;
    widget?: ToolWidgetMetaData;
};

declare type ToolWidgetMetaData = {
    uri: string;
    widgetId: string;
    title: string;
    toolId: string;
};

export declare const transformMessagesToSdkMessages: (_messages: ChatMessage[]) => {
    messages: SdkChatMessage[];
    thinking: {
        startTime: Date;
        content: string;
    } | undefined;
    plan: Plan | undefined;
    currentIntermediateMessage: IntermediateAgentMessage | undefined;
    intermediateMessages: IntermediateAgentMessage[];
    agentSteps: SdkAgentStep[];
    outputFiles: AgentMessage_2[];
};

/** FAQs and user persona memories that were used during agent execution */
declare interface UsedMemories {
    faqs?: Array<{
        question: string;
        answer: string;
        similarity?: number;
    }>;
    userPersona?: {
        memories: Array<{
            type: string;
            value: string;
            confidence?: number;
        }>;
    };
}

export declare const UserMessage: ({ message, isSelected, onClick, annotationOverlay, selectionIndicator, className }: UserMessageProps) => JSX_2.Element;

declare type UserMessage_2 = ConversationElement & {
    type: ConversationElementType.USER_MESSAGE;
    content: string;
    subtype: 'text' | 'file';
    source: 'direct' | 'slack' | 'discord' | 'teams' | 'api';
    status?: 'pending' | 'acknowledged' | 'error' | 'failed';
    metadata?: {
        channel?: string;
        thread?: string[];
        user?: string;
        platform?: string;
        questionId?: string;
        fileName?: string;
        documents?: AttachedDocument[];
        payload?: any;
    };
};

export declare type UserMessageAnnotationProps = {
    isSelected?: boolean;
    onClick?: () => void;
    annotationOverlay?: ReactNode;
    selectionIndicator?: ReactNode;
    className?: string;
};

declare type UserMessageProps = {
    message: SdkUserMessage;
} & UserMessageAnnotationProps;

declare type WidgetConfig = {
    serverUrl: string;
    agentId: string;
    widgetKey: string;
    appWidgetKey?: string;
    agentExecutorVersion?: string;
    appId?: string;
    accountId?: string;
    variables?: Record<string, string>;
};

export declare type WidgetsSdk = {
    getInstance: (instanceId: string) => {
        on: (eventName: string, callback: (data?: any) => any) => () => void;
        emit: (eventName: string, data?: any) => void;
    };
};

declare type WidgetTheme = {
    palette?: WidgetThemePalette;
};

declare type WidgetThemePalette = {
    primary00?: string;
    primary100?: string;
    primary200?: string;
    primary300?: string;
    primary400?: string;
    primary500?: string;
    secondary300?: string;
    secondary500?: string;
    secondary700?: string;
    background00?: string;
    background100?: string;
    background200?: string;
    background300?: string;
    background800?: string;
    background900?: string;
    foreground00?: string;
    foreground200?: string;
    foreground600?: string;
    foreground800?: string;
    foreground900?: string;
    border100?: string;
    border200?: string;
    border300?: string;
    border400?: string;
    primary?: string;
    primaryForeground?: string;
    secondary?: string;
    secondaryForeground?: string;
    background?: string;
    foreground?: string;
    border?: string;
    input?: string;
    success?: string;
    successForeground?: string;
    warning?: string;
    warningForeground?: string;
    destructive?: string;
    destructiveForeground?: string;
    sidebar?: string;
    sidebarForeground?: string;
    ring?: string;
};

declare type WidgetUser = {
    uid: string;
    firstName: string;
    lastName?: string;
    email: string;
};

export { }
