import type { ContentBlockParam } from '@anthropic-ai/sdk/resources/messages.js';
import React from 'react';
import type { LocalJSXCommandCall, LocalJSXCommandOnDone } from '../../types/command.js';
import { checkOverageGate, confirmOverage, launchRemoteReview } from './reviewRemote.js';
import { UltrareviewOverageDialog } from './UltrareviewOverageDialog.js';
function contentBlocksToString(blocks: ContentBlockParam[]): string {
  return blocks.map(b => b.type === 'text' ? b.text : '').filter(Boolean).join('\n');
}
async function launchAndDone(args: string, context: Parameters<LocalJSXCommandCall>[1], onDone: LocalJSXCommandOnDone, billingNote: string, signal?: AbortSignal): Promise<void> {
  const result = await launchRemoteReview(args, context, billingNote);
  // User hit Escape during the ~5s launch — the dialog already showed
  // "cancelled" and unmounted, so skip onDone (would write to a dead
  // transcript slot) and let the caller skip confirmOverage.
  if (signal?.aborted) return;
  if (result) {
    onDone(contentBlocksToString(result), {
      shouldQuery: true
    });
  } else {
    // Precondition failures now return specific ContentBlockParam[] above.
    // null only reaches here on teleport failure (PR mode) or non-github
    // repo — both are CCR/repo connectivity issues.
    onDone('Ultrareview failed to launch the remote session. Check that this is a GitHub repo and try again.', {
      display: 'system'
    });
  }
}
export const call: LocalJSXCommandCall = async (onDone, context, args) => {
  const gate = await checkOverageGate();
  if (gate.kind === 'not-enabled') {
    onDone('Free ultrareviews used. Enable Extra Usage at https://claude.ai/settings/billing to continue.', {
      display: 'system'
    });
    return null;
  }
  if (gate.kind === 'low-balance') {
    onDone(`Balance too low to launch ultrareview ($${gate.available.toFixed(2)} available, $10 minimum). Top up at https://claude.ai/settings/billing`, {
      display: 'system'
    });
    return null;
  }
  if (gate.kind === 'needs-confirm') {
    return <UltrareviewOverageDialog onProceed={async signal => {
      await launchAndDone(args, context, onDone, ' This review bills as Extra Usage.', signal);
      // Only persist the confirmation flag after a non-aborted launch —
      // otherwise Escape-during-launch would leave the flag set and
      // skip this dialog on the next attempt.
      if (!signal.aborted) confirmOverage();
    }} onCancel={() => onDone('Ultrareview cancelled.', {
      display: 'system'
    })} />;
  }

  // gate.kind === 'proceed'
  await launchAndDone(args, context, onDone, gate.billingNote);
  return null;
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJDb250ZW50QmxvY2tQYXJhbSIsIlJlYWN0IiwiTG9jYWxKU1hDb21tYW5kQ2FsbCIsIkxvY2FsSlNYQ29tbWFuZE9uRG9uZSIsImNoZWNrT3ZlcmFnZUdhdGUiLCJjb25maXJtT3ZlcmFnZSIsImxhdW5jaFJlbW90ZVJldmlldyIsIlVsdHJhcmV2aWV3T3ZlcmFnZURpYWxvZyIsImNvbnRlbnRCbG9ja3NUb1N0cmluZyIsImJsb2NrcyIsIm1hcCIsImIiLCJ0eXBlIiwidGV4dCIsImZpbHRlciIsIkJvb2xlYW4iLCJqb2luIiwibGF1bmNoQW5kRG9uZSIsImFyZ3MiLCJjb250ZXh0IiwiUGFyYW1ldGVycyIsIm9uRG9uZSIsImJpbGxpbmdOb3RlIiwic2lnbmFsIiwiQWJvcnRTaWduYWwiLCJQcm9taXNlIiwicmVzdWx0IiwiYWJvcnRlZCIsInNob3VsZFF1ZXJ5IiwiZGlzcGxheSIsImNhbGwiLCJnYXRlIiwia2luZCIsImF2YWlsYWJsZSIsInRvRml4ZWQiXSwic291cmNlcyI6WyJ1bHRyYXJldmlld0NvbW1hbmQudHN4Il0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB0eXBlIHsgQ29udGVudEJsb2NrUGFyYW0gfSBmcm9tICdAYW50aHJvcGljLWFpL3Nkay9yZXNvdXJjZXMvbWVzc2FnZXMuanMnXG5pbXBvcnQgUmVhY3QgZnJvbSAncmVhY3QnXG5pbXBvcnQgdHlwZSB7XG4gIExvY2FsSlNYQ29tbWFuZENhbGwsXG4gIExvY2FsSlNYQ29tbWFuZE9uRG9uZSxcbn0gZnJvbSAnLi4vLi4vdHlwZXMvY29tbWFuZC5qcydcbmltcG9ydCB7XG4gIGNoZWNrT3ZlcmFnZUdhdGUsXG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