/** @jsxImportSource @opentui/solid */

import { createMemo, Match, Show, Switch, createSignal } from "solid-js"
import type { ToolPart as ToolPartType } from "@saeeol/sdk/v2"
import { useSync } from "@tui/context/sync"
import { Suggest } from "@/saeeol/suggestion/tui/render" 
import { use } from "../context"
import type { ToolProps } from "./tool-parts"
import { GenericTool, InlineTool, BlockTool } from "./tool-parts"
import { Bash, Write, Glob, Read, Grep, WebFetch, WebSearch, SemanticSearch, Task } from "./tool-renderers"
import { Edit, ApplyPatch, TodoWrite, Question, Skill } from "./tool-renderers-2"

export function ToolPart(props: { last: boolean; part: ToolPartType; message: any }) {
  const ctx = use()
  const sync = useSync()

  const shouldHide = createMemo(() => {
    if (ctx.showDetails()) return false
    if (props.part.state.status !== "completed") return false
    return true
  })

  const toolprops = {
    get metadata() {
      return props.part.state.status === "pending" ? {} : (props.part.state.metadata ?? {})
    },
    get input() {
      return props.part.state.input ?? {}
    },
    get output() {
      return props.part.state.status === "completed" ? props.part.state.output : undefined
    },
    get permission() {
      const permissions = sync.data.permission[props.message.sessionID] ?? []
      const permissionIndex = permissions.findIndex((x: any) => x.tool?.callID === props.part.callID)
      return permissions[permissionIndex]
    },
    get tool() {
      return props.part.tool
    },
    get part() {
      return props.part
    },
  }

  return (
    <Show when={!shouldHide()}>
      <Switch>
        <Match when={props.part.tool === "bash"}>
          <Bash {...toolprops} />
        </Match>
        <Match when={props.part.tool === "glob"}>
          <Glob {...toolprops} />
        </Match>
        <Match when={props.part.tool === "read"}>
          <Read {...toolprops} />
        </Match>
        <Match when={props.part.tool === "grep"}>
          <Grep {...toolprops} />
        </Match>
        <Match when={props.part.tool === "semantic_search"}>
          <SemanticSearch {...toolprops} />
        </Match>
        <Match when={props.part.tool === "webfetch"}>
          <WebFetch {...toolprops} />
        </Match>
        <Match when={props.part.tool === "websearch"}>
          <WebSearch {...toolprops} />
        </Match>
        <Match when={props.part.tool === "write"}>
          <Write {...toolprops} />
        </Match>
        <Match when={props.part.tool === "edit"}>
          <Edit {...toolprops} />
        </Match>
        <Match when={props.part.tool === "task"}>
          <Task {...toolprops} />
        </Match>
        <Match when={props.part.tool === "apply_patch"}>
          <ApplyPatch {...toolprops} />
        </Match>
        <Match when={props.part.tool === "todowrite"}>
          <TodoWrite {...toolprops} />
        </Match>
        <Match when={props.part.tool === "question"}>
          <Question {...toolprops} />
        </Match>
        <Match when={props.part.tool === "suggest"}>
          {(() => {
            const pending = createMemo(() => {
              const requests = sync.data.suggestion[props.message.sessionID] ?? []
              return requests.find(
                (r) =>
                  r.blocking === false &&
                  r.tool?.callID === props.part.callID &&
                  r.tool?.messageID === props.part.messageID,
              )
            })
            return <Suggest {...toolprops} InlineTool={InlineTool} BlockTool={BlockTool} pendingRequest={pending()} />
          })()}
        </Match>
        <Match when={props.part.tool === "skill"}>
          <Skill {...toolprops} />
        </Match>
        <Match when={true}>
          <GenericTool {...toolprops} />
        </Match>
      </Switch>
    </Show>
  )
}
