import { createMemo, Show } from "solid-js"
import { createStore } from "solid-js/store"
import fuzzysort from "fuzzysort"
import type { AlertOptions, ConfirmOptions, PromptOptions, SelectOptions } from "@opencode/plugin/gui"
import { Button } from "@opencode/ui/button"
import { Dialog, DialogBody, DialogFooter, DialogHeader, DialogTitle, DialogTitleGroup } from "@opencode/ui/dialog"
import { SearchHighlight, SearchList } from "@opencode/ui/search-list"
import { TextInput } from "@opencode/ui/text-input"
import { useLanguage } from "@/runtime/i18n/language"
import "@/shell/commands/dialog.css"
import "@/shell/palette-menu.css"
// The dialogs behind `Dialogs.confirm`, `alert`, `prompt` and `select`. Each answers through `done`; the host closes
// the dialog and settles the plugin's promise.
/** `Dialogs.confirm`: the app's confirm dialog. A destructive action focuses Cancel, as the app's own do. */
export function PluginConfirmDialog(props: { options: ConfirmOptions; done: (confirmed: boolean) => void }) {
const language = useLanguage()
return (
)
}
export function PluginAlertDialog(props: { options: AlertOptions; done: () => void }) {
const language = useLanguage()
return (
)
}
/** `Dialogs.prompt`: one text field. Submitting needs a value, so the dialog never answers with "". */
export function PluginPromptDialog(props: { options: PromptOptions; done: (value: string | undefined) => void }) {
const language = useLanguage()
const [store, setStore] = createStore({ value: props.options.value ?? "", selected: false })
return (
)
}
/** `Dialogs.select`: a searchable list in the command palette's dialog. Rows are keyed by their index. */
export function PluginSelectDialog(props: { options: SelectOptions; done: (value: T | undefined) => void }) {
const language = useLanguage()
const [store, setStore] = createStore({ query: "" })
const rows = createMemo(() =>
props.options.options.map((option, index) => ({
key: String(index),
option,
title: option.title,
description: option.description ?? "",
})),
)
const results = createMemo(() => {
const query = store.query.trim()
if (!query) return rows()
return fuzzysort.go(query, rows(), { keys: ["title", "description"] }).map((result) => result.obj)
})
return (
)
}