"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateCommandPaletteAction = generateCommandPaletteAction;
function generateCommandPaletteAction() {
    return `import { z } from 'zod'

export type CommandPaletteActionPage = {
  label?: string
  actions: CommandPaletteAction[]
  back?: CommandPaletteActionPage
}

export type CommandPaletteActionVisibility = 'visible' | 'llm' | 'user'

export type CommandPaletteActionResult =
  | ({ kind: 'page' } & CommandPaletteActionPage)
  | { kind: 'output'; output: Record<string, unknown> }
  | { kind: 'close'; output?: Record<string, unknown> }
  | { kind: 'continue'; output?: Record<string, unknown> }

export type CommandPaletteAction = {
  kind: 'command' | 'input'
  key: string
  label: string
  icon?: React.ReactNode
  visibility: CommandPaletteActionVisibility
  group?: string
  keywords?: string[]
  suggestions?: string[]
  inputSchema?: z.ZodSchema
  run: (
    query: string,
    input: unknown,
    signal: AbortSignal,
    render: (component: React.FC | null) => void,
  ) => Promise<CommandPaletteActionResult>
}

const zInputQuery = z.object({ query: z.string() })

export const command = (action: Omit<CommandPaletteAction, 'kind'>): CommandPaletteAction => {
  const key = sanitizeKey(action.key)

  return {
    ...action,
    key,
    keywords: [...key.split('-'), ...(action.keywords ?? [])],
    kind: 'command',
  }
}

export const input = (action: Omit<CommandPaletteAction, 'kind'>): CommandPaletteAction => {
  const key = sanitizeKey(action.key)

  return {
    ...action,
    key,
    keywords: [...key.split('-'), ...(action.keywords ?? [])],
    kind: 'input',
    inputSchema: action.inputSchema ?? zInputQuery,
    run: action.inputSchema
      ? action.run
      : async (query, input, signal, render) => {
          const parsed = zInputQuery.safeParse(input)
          if (parsed.success) {
            return action.run(parsed.data.query, parsed.data, signal, render)
          }

          return action.run(query, { query }, signal, render)
        },
  }
}

export const waitForNextFrame = async () => {
  await new Promise((resolve) => requestAnimationFrame(() => resolve(undefined)))
}

const sanitizeKey = (value: string): string =>
  value
    .trim()
    .toLowerCase()
    .replaceAll(/[^a-z0-9-]+/g, '-')
    .replaceAll(/-{2,}/g, '-')
    .replaceAll(/(^-+)|(-+$)/g, '')
`;
}
//# sourceMappingURL=command-palette-action.generator.js.map