'use strict';
// GitHub Copilot CLI approval prompt provider.
// `gh copilot` mostly surfaces suggestions without approval widgets, but
// interactive modes ask "Execute command?" with [Y/n]. The signal is narrow
// to avoid false positives from normal `gh` output.

const EXEC_RE = /\b(Execute|Run)\b\s+command\??\s*\[?[Yy]\s*\/\s*[Nn]\s*\]?/i;

module.exports = {
  id: 'copilot',
  name: 'GitHub Copilot CLI',

  detect(text) {
    const lines = text.split('\n').filter(l => l.trim());
    if (lines.length < 1) return false;
    const tail = lines.slice(-5);
    return tail.some(l => EXEC_RE.test(l));
  },

  parse(text) {
    const lines = text.split('\n').map(l => l.trim()).filter(Boolean);
    let promptIdx = -1;
    for (let i = lines.length - 1; i >= 0; i--) {
      if (EXEC_RE.test(lines[i])) { promptIdx = i; break; }
    }
    if (promptIdx < 0) return null;

    const ctxStart = Math.max(0, promptIdx - 8);
    const command = lines.slice(ctxStart, promptIdx).join('\n').slice(0, 2000);
    const fullContext = lines.slice(ctxStart, promptIdx + 1).join('\n').slice(0, 2000);

    return {
      toolName: 'Copilot Shell Command',
      command,
      warning: '',
      fullContext,
      hasAllowAll: false,
      providerId: 'copilot',
    };
  },

  approveKeystroke() {
    return 'y';
  },

  approveAllKeystroke() {
    return null;
  },

  requiresEnter: true,
};
