import React, { useState } from 'react';
import { __ } from '@wordpress/i18n';
import { api } from '../api';
import { Card, Button, Badge, Notice } from './ui';
import { IconSparkle } from './icons';
/**
* The site-audit copilot.
*
* The deterministic checks are the truth; the copilot may only group them,
* explain the likely cause and the impact, estimate effort and risk, and put
* them in an order worth working. Ids that do not exist in the run are
* dropped server-side, so nothing here can invent a finding.
*/
const tone = (v) => (v === 'high' ? 'serious' : v === 'medium' ? 'warning' : 'good');
export default function AuditCopilot({ hasRun }) {
const [data, setData] = useState(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState(null);
const run = async () => {
setBusy(true);
setError(null);
try {
setData(await api.post('/ai/audit/explain', {}));
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
}
};
return (
}
action={
}
>
{!hasRun && {__('Run the audit first; the copilot explains its results.', 'ai-rank-suite')}}
{hasRun && !data && !error && !busy && (
{__('Nothing explained yet. The AI reads every finding of the last run, groups them by cause, and writes the steps in the order worth working — with effort and risk for each group.', 'ai-rank-suite')}
)}
{busy && !data && {__('Reading the findings…', 'ai-rank-suite')}}
{error && {error}}
{data && data.groups.length === 0 && {__('Nothing to explain — the run has no findings.', 'ai-rank-suite')}}
{data && data.groups.length > 0 && (