import React, { useState } from 'react';
import { __, _n, sprintf } from '@wordpress/i18n';
import { Badge, Button, Notice } from './ui';
import { IconX } from './icons';
function Field({ field, value, onChange }) {
const common = {
value: value ?? '',
placeholder: field.hint,
onChange: (e) => onChange(e.target.value),
};
return (
{field.type === 'lines' ? (
) : field.type === 'select' ? (
) : (
)}
{field.hint && field.type !== 'lines' && (
{field.hint}
)}
);
}
export function TypePicker({ grouped, value, onChange }) {
const [search, setSearch] = useState('');
const q = search.trim().toLowerCase();
const filtered = Object.entries(grouped || {})
.map(([group, entries]) => [group, entries.filter((e) => !q || e.label.toLowerCase().includes(q))])
.filter(([, entries]) => entries.length > 0);
return (
setSearch(e.target.value)}
/>
);
}
export default function SchemaEditor({ data, onChange, showVariables = true }) {
const items = data?.items || [];
const types = data?.types || {};
const validation = data?.validation || [];
const setItem = (index, patch) =>
onChange(items.map((it, i) => (i === index ? { ...it, ...patch } : it)));
const firstType = Object.keys(data?.grouped?.[Object.keys(data?.grouped || {})[0]] || {}).length
? Object.values(data.grouped)[0][0].value
: 'Article';
return (
{data?.templates?.length > 0 && (
{data.templates.length === 1
? sprintf(__('The template “%1$s” also applies %2$s schema to this page.', 'ai-rank-suite'), data.templates[0].name, data.templates[0].type)
: sprintf(__('%d global templates also apply schema to this page.', 'ai-rank-suite'), data.templates.length)}
)}
{items.length === 0 && (
{__('No custom schema on this page yet — Article/WebPage and breadcrumbs are always emitted automatically.', 'ai-rank-suite')}
)}
{items.map((item, index) => {
const type = types[item.type];
const check = validation[index];
return (
{type?.label || item.type}
{check && !check.valid && {sprintf(__('%d required missing', 'ai-rank-suite'), check.missing_required.length)}}
{check && check.valid && check.missing_recommended.length > 0 && (
{sprintf(__('%d recommended missing', 'ai-rank-suite'), check.missing_recommended.length)}
)}
{check && check.valid && check.missing_recommended.length === 0 && ✓ {__('complete', 'ai-rank-suite')}}
setItem(index, { type: next, data: {} })}
/>
{check && check.missing_required.length > 0 && (
{sprintf(__('Google will ignore this schema until you fill in: %s.', 'ai-rank-suite'), check.missing_required.join(', '))}
)}
{(type?.fields || []).map((field) => (
setItem(index, { data: { ...item.data, [field.key]: value } })}
/>
))}
);
})}
{showVariables && (
{__('Available variables', 'ai-rank-suite')}
{Object.entries(data?.variables || {}).map(([token, label]) => (
{token}
))}
)}
{data?.preview?.length > 0 && (
{sprintf(_n('Preview JSON-LD (%d node)', 'Preview JSON-LD (%d nodes)', data.preview.length, 'ai-rank-suite'), data.preview.length)}
{JSON.stringify(data.preview, null, 2)}
)}
);
}