import type {
  GoatChainContext as DimSdkContext,
  ToolCall,
  ToolResult,
} from '#cli/core/model'
import type { PropType } from 'vue'
import type { CliAgentBridge as DimSdkAgent } from './agent-bridge'
import type { DimSdkStore } from './core/store'
import type { DimSdkSessionsApi } from './keys'
import { TRouterView } from '@simon_he/vue-tui'
import { defineComponent, h } from 'vue'
import { DimSdkProvider } from './provider'
import { dimsdkCliRoutes } from './routes'

export type ToolRunner = (call: ToolCall) => Promise<ToolResult>

export const DimSdkShell = defineComponent({
  name: 'DimSdkShell',
  props: {
    toolRunner: { type: Function as PropType<ToolRunner>, required: true },
    context: { type: Object as PropType<DimSdkContext>, required: true },
    agent: { type: Object as PropType<DimSdkAgent>, required: true },
    onStoreCreated: {
      type: Function as PropType<((store: DimSdkStore) => void) | undefined>,
      default: undefined,
    },
    sessions: {
      type: Object as PropType<DimSdkSessionsApi | undefined>,
      default: undefined,
    },
    uiForcedSkills: {
      type: Object as PropType<Set<string> | undefined>,
      default: undefined,
    },
  },
  setup(props) {
    return () =>
      h(
        DimSdkProvider,
        {
          toolRunner: props.toolRunner,
          context: props.context,
          agent: props.agent,
          onStoreCreated: props.onStoreCreated,
          sessions: props.sessions,
          uiForcedSkills: props.uiForcedSkills,
        },
        () => h(TRouterView, { routes: dimsdkCliRoutes }),
      )
  },
})
