#!/usr/bin/env node

// src/common/osc-extractor.ts
var OSC_PATTERN = /\x1b\](\d+);([^\x07\x1b]*?)(?:\x07|\x1b\\)/g;
var SEMANTIC_TEXT_TAIL_MAX = 4096;
var OSC_SEQUENCE_PATTERN = /\x1b\](?:\d+);[^\x07\x1b]*?(?:\x07|\x1b\\)/g;
var CSI_SEQUENCE_PATTERN = /\x1b\[[0-?]*[ -/]*[@-~]/g;
var SIMPLE_ESC_PATTERN = /\x1b[@-Z\\-_]/g;
function extractOscSequences(rawData) {
  const regex = new RegExp(OSC_PATTERN.source, OSC_PATTERN.flags);
  const matches = [];
  let match;
  while ((match = regex.exec(rawData)) !== null) {
    matches.push({ code: parseInt(match[1], 10), text: match[2] });
  }
  return matches;
}
function lastSequence(matches, code) {
  for (let i = matches.length - 1; i >= 0; i -= 1) {
    if (matches[i].code === code) return matches[i];
  }
  return void 0;
}
function isCodexActionRequiredTitle(title) {
  return /\bAction Required\b/i.test(title);
}
function normalizePtySemanticText(rawData) {
  return rawData.replace(OSC_SEQUENCE_PATTERN, " ").replace(CSI_SEQUENCE_PATTERN, " ").replace(SIMPLE_ESC_PATTERN, " ").replace(/\r/g, "\n").replace(/[^\S\n]+/g, " ").replace(/\n+/g, "\n").trim();
}
function appendPtySemanticTextTail(previous, chunk) {
  const next = `${previous}
${normalizePtySemanticText(chunk)}`.slice(-SEMANTIC_TEXT_TAIL_MAX);
  return next.trim();
}
function extractClaudeTextSignal(text) {
  const toolMatch = text.match(/\bHook\s+PreToolUse:([A-Za-z0-9_.:-]+)/i);
  if (toolMatch?.[1] && /\brequires confirmation for this (?:command|tool)\b/i.test(text) && /\bDo you want to proceed\?/i.test(text)) {
    return {
      state: "approval_wait",
      tool: toolMatch[1],
      title: `Hook confirmation: ${toolMatch[1]}`
    };
  }
  const nativePermissionMatch = text.match(
    /\bDo you want to (?<action>make this edit to|create|update|delete|run|execute)\b[^\n?]*\?/i
  );
  if (nativePermissionMatch?.groups?.action && /\b1\.\s*Yes\b/i.test(text) && /\b3\.\s*No\b/i.test(text)) {
    const action = nativePermissionMatch.groups.action.toLowerCase();
    const tool = action === "run" || action === "execute" ? "Bash" : "Edit";
    return {
      state: "approval_wait",
      tool,
      title: `Claude permission: ${tool}`
    };
  }
  return null;
}
function extractTextSignals(semanticText, provider) {
  if (provider === "claude") {
    return extractClaudeTextSignal(semanticText);
  }
  return null;
}
function extractOscSignals(rawData, provider) {
  const matches = extractOscSequences(rawData);
  if (matches.length === 0) return null;
  const osc0 = lastSequence(matches, 0);
  const osc9 = lastSequence(matches, 9);
  if (osc9) {
    if (osc9.text.includes("waiting for your input") || osc9.text.trim() === "4;0;") {
      return { state: "turn_complete", ...osc0 ? { title: osc0.text } : {} };
    }
    if (osc9.text.includes("needs your permission")) {
      const toolMatch = osc9.text.match(/permission.*?:\s*(\S+)/);
      return {
        state: "approval_wait",
        ...osc0 ? { title: osc0.text } : {},
        ...toolMatch?.[1] ? { tool: toolMatch[1] } : {}
      };
    }
  }
  if (provider === "codex" && osc0 && isCodexActionRequiredTitle(osc0.text)) {
    return { state: "approval_wait", title: osc0.text };
  }
  if (osc0 && !osc9) {
    return { state: null, title: osc0.text };
  }
  return null;
}

// src/common/pty-approval-state.ts
function shouldReleaseApprovalWait(options) {
  if (options.currentState !== "approval_wait") return false;
  if (options.signalState === void 0) return false;
  if (options.signalState === null && options.allowTitleOnlyRelease === false) return false;
  return options.signalState !== "approval_wait";
}
function stateAfterApprovalRelease(signalState) {
  if (signalState === null) return "turn_complete";
  return signalState !== "approval_wait" ? signalState : "working";
}
function shouldReleaseTextApprovalOnInput(data) {
  if (data.includes("\r") || data.includes("\n")) return true;
  return data === "1" || data === "2" || data === "y" || data === "Y" || data === "n" || data === "N" || data === "\x1B" || data === "";
}

// src/common/pty-semantic-machine.ts
function withMeta(signal) {
  return {
    ...signal?.title !== void 0 ? { title: signal.title } : {},
    ...signal?.tool !== void 0 ? { tool: signal.tool } : {}
  };
}
function decidePtySemanticTransition(input) {
  const {
    currentState,
    signal,
    sessionStateIsWaitingApproval,
    allowTitleOnlyApprovalRelease = true
  } = input;
  if (signal?.state === "approval_wait") {
    return { nextState: "approval_wait", emit: true, meta: withMeta(signal) };
  }
  if (shouldReleaseApprovalWait({
    currentState,
    signalState: signal === null ? void 0 : signal.state,
    allowTitleOnlyRelease: allowTitleOnlyApprovalRelease
  })) {
    return {
      nextState: stateAfterApprovalRelease(signal.state),
      emit: true,
      meta: withMeta(signal)
    };
  }
  const inApprovalContext = currentState === "approval_wait" || sessionStateIsWaitingApproval === true;
  if (inApprovalContext && signal?.state !== "turn_complete") {
    return { nextState: "approval_wait", emit: true, meta: withMeta(signal) };
  }
  if (signal !== null && signal.state === null) {
    return { nextState: currentState, emit: false };
  }
  if (signal && signal.state !== null && signal.state !== "working") {
    return { nextState: signal.state, emit: true, meta: withMeta(signal) };
  }
  if (currentState !== "working") {
    return { nextState: "working", emit: true };
  }
  return { nextState: currentState, emit: false };
}

export {
  extractOscSequences,
  appendPtySemanticTextTail,
  extractTextSignals,
  extractOscSignals,
  shouldReleaseTextApprovalOnInput,
  decidePtySemanticTransition
};
//# sourceMappingURL=chunk-W6275ZRJ.js.map