{"version":3,"sources":["/home/mkabumattar/work/withrawi/rawi/dist/chunk-DHWMJITY.cjs","../src/cli/commands/completion/generator.ts"],"names":["getCompletionDir","join","homedir","copyCompletionTemplate","shell","templateContent","TEMPLATES"],"mappings":"AAAA;AACA,wDAAwC,wDAAyC,wDAA2C,wBCD3D,wBAC3C,4BACH,4EACD,SAWTA,CAAAA,CAAAA,CAA2B,CAClC,OAAOC,wBAAAA,yBAAKC,CAAQ,CAAG,OAAA,CAAS,YAAY,CAC9C,CAEA,SAASC,CAAAA,CAAuBC,CAAAA,CAAuB,CACrD,GAAI,CACF,IAAMC,CAAAA,CAAkBC,mBAAAA,CAAUF,CAA+B,CAAA,CAEjE,EAAA,CAAI,CAACC,CAAAA,CACH,MAAM,IAAI,KAAA,CAAM,CAAA,8BAAA,EAAiCD,CAAK,CAAA,CAAA;AAuDrC;AAER;AA0BiC;AAkChD,oBAAA;ADtIqa","file":"/home/mkabumattar/work/withrawi/rawi/dist/chunk-DHWMJITY.cjs","sourcesContent":[null,"import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'node:fs';\nimport {homedir} from 'node:os';\nimport {join} from 'node:path';\nimport chalk from 'chalk';\nimport {SUPPORTED_SHELLS} from './data.js';\nimport {TEMPLATES} from './templates.js';\nimport {detectShell, getShellProfile} from './utils.js';\n\ninterface CompletionOptions {\n  shell: string;\n  install: boolean;\n  advanced: boolean;\n}\n\nfunction getCompletionDir(): string {\n  return join(homedir(), '.rawi', 'completion');\n}\n\nfunction copyCompletionTemplate(shell: string): string {\n  try {\n    const templateContent = TEMPLATES[shell as keyof typeof TEMPLATES];\n\n    if (!templateContent) {\n      throw new Error(`Template not found for shell: ${shell}`);\n    }\n\n    const completionDir = getCompletionDir();\n    mkdirSync(completionDir, {recursive: true});\n\n    const destPath = join(completionDir, `rawi-completion.${shell}`);\n\n    writeFileSync(destPath, templateContent);\n\n    return destPath;\n  } catch (error) {\n    throw new Error(\n      `Failed to copy completion template: ${error instanceof Error ? error.message : String(error)}`,\n    );\n  }\n}\n\nfunction isCompletionLoaded(shell: string): boolean {\n  try {\n    const profile = getShellProfile(shell);\n    if (!profile || !existsSync(profile)) {\n      return false;\n    }\n\n    const profileContent = readFileSync(profile, 'utf8');\n    return (\n      profileContent.includes('# rawi completion') ||\n      (profileContent.includes('source') &&\n        profileContent.includes('.rawi/completion'))\n    );\n  } catch {\n    return false;\n  }\n}\n\nasync function installCompletion(\n  completionPath: string,\n  shell: string,\n): Promise<void> {\n  const profile = getShellProfile(shell);\n  if (!profile) {\n    throw new Error(`Cannot determine profile file for shell: ${shell}`);\n  }\n\n  if (isCompletionLoaded(shell)) {\n    console.log(chalk.yellow(`Completion already installed for ${shell}`));\n    return;\n  }\n\n  const sourceCommand =\n    shell === 'fish'\n      ? `source ${completionPath}`\n      : `[ -f \"${completionPath}\" ] && source \"${completionPath}\"`;\n\n  const installBlock = `\n# rawi completion\n${sourceCommand}\n`;\n\n  const {appendFileSync} = await import('node:fs');\n  appendFileSync(profile, installBlock);\n\n  console.log(chalk.green(`✓ Installed completion to: ${profile}`));\n  console.log(chalk.gray('Restart your shell or run:'));\n  console.log(chalk.cyan(`  source ${profile}`));\n}\n\nexport async function generateCompletionScript(\n  options: CompletionOptions,\n): Promise<void> {\n  try {\n    let shell = options.shell;\n\n    if (shell === 'auto') {\n      shell = detectShell();\n      console.log(chalk.gray(`Detected shell: ${shell}`));\n    }\n\n    if (!SUPPORTED_SHELLS.includes(shell) || shell === 'auto') {\n      console.error(chalk.red(`Unsupported shell: ${shell}`));\n      console.log(\n        chalk.gray(\n          `Supported shells: ${SUPPORTED_SHELLS.filter((s) => s !== 'auto').join(', ')}`,\n        ),\n      );\n      process.exit(1);\n    }\n\n    console.log(chalk.gray('Copying completion template...'));\n    const completionPath = copyCompletionTemplate(shell);\n\n    if (options.install) {\n      await installCompletion(completionPath, shell);\n    } else {\n      console.log(\n        chalk.green(`✓ Completion script copied to: ${completionPath}`),\n      );\n      console.log(chalk.gray('\\nTo install manually:'));\n      const profile = getShellProfile(shell);\n      if (profile) {\n        const sourceCmd =\n          shell === 'fish'\n            ? `echo 'source ${completionPath}' >> ${profile}`\n            : `echo '[ -f \"${completionPath}\" ] && source \"${completionPath}\"' >> ${profile}`;\n        console.log(chalk.cyan(`  ${sourceCmd}`));\n        console.log(chalk.cyan(`  source ${profile}`));\n      }\n      console.log(chalk.gray('Or use --install to install automatically'));\n    }\n  } catch (error) {\n    console.error(chalk.red('Failed to setup completion:'));\n    console.error(\n      chalk.red(error instanceof Error ? error.message : String(error)),\n    );\n    process.exit(1);\n  }\n}\n"]}