'use strict';
// Cline CLI approval prompt provider.
// Cline uses a simple [Y/n] or (y/N) inline prompt for most approvals;
// destructive ops show a fuller "Proceed?" text. Approve with 'y'.
//
// Provider stub: approval parsing is best-effort.

const APPROVE_RE = /\b(Proceed|Execute|Run this|Apply this|Allow)\b.*\[?\s*[Yy]\s*\/\s*[Nn]\s*\]?|\([Yy]\/[Nn]\)/i;

module.exports = {
  id: 'cline',
  name: 'Cline',

  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 => APPROVE_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 (APPROVE_RE.test(lines[i])) { promptIdx = i; break; }
    }
    if (promptIdx < 0) return null;

    const ctxStart = Math.max(0, promptIdx - 12);
    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: 'Cline Action',
      command,
      warning: '',
      fullContext,
      hasAllowAll: false,
      providerId: 'cline',
    };
  },

  approveKeystroke() {
    return 'y';
  },

  approveAllKeystroke() {
    return null;
  },

  requiresEnter: true,
};
