/**
* TemplatePicker — card grid shown for "New Workflow": a blank workflow
* card first, then the templates library.
*/
import { useState, useEffect, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { apiGet, apiPost } from '../../../utils/api';
import { toast } from '../../common/Toast';
import Loader from '../../common/Loader';
import { Button } from '../../common/WpComponents';
import { triggerIcon } from './utils/icons';
import AiWorkflowModal from './AiWorkflowModal';
const TemplatePicker = ( { onBack, onNavigate } ) => {
const [ loading, setLoading ] = useState( true );
const [ templates, setTemplates ] = useState( [] );
const [ applyingId, setApplyingId ] = useState( null );
const [ aiModalOpen, setAiModalOpen ] = useState( false );
const [ brandVoices, setBrandVoices ] = useState( [] );
const load = useCallback( async () => {
setLoading( true );
try {
const res = await apiGet( '/workflow-automation/templates' );
setTemplates( res?.templates || [] );
} catch ( e ) {
toast( e?.message || __( 'Failed to load templates.', 'ai-marketing-expert' ), 'error' );
} finally {
setLoading( false );
}
}, [] );
useEffect( () => {
load();
apiGet( '/content/brand-voices' )
.then( ( res ) => setBrandVoices( res?.items || [] ) )
.catch( () => {} );
}, [ load ] );
const apply = async ( tpl ) => {
setApplyingId( tpl.id );
try {
const res = await apiPost( `/workflow-automation/templates/${ tpl.id }/apply` );
if ( res?.workflow_id ) {
onNavigate( 'edit-workflow', { id: res.workflow_id } );
}
} catch ( e ) {
toast( e?.message || __( 'Could not apply this template.', 'ai-marketing-expert' ), 'error' );
} finally {
setApplyingId( null );
}
};
const handleAiGenerated = ( generatedWf ) => {
onNavigate( 'edit-workflow', { initialWorkflow: generatedWf } );
};
if ( loading ) {
return