import { __ } from '@wordpress/i18n';
import { Button, Icon, SelectControl } from '@wordpress/components';
import { useEffect, useMemo, useState } from '@wordpress/element';
import { linkOff } from '@wordpress/icons';
import ConnectionStatus from './ConnectionStatus';
import LianaAPIFields from './LianaAPIFields';
import Steps from '../../shared/Steps';
import steps from './automationSteps';
import AutomationTogglerFields from './AutomationTogglerFields';
import { makeOptionsOfChannels } from '../utils';
type LianaAutomationSettingsProps = {
channels: Array< { id: string; name: string } >;
connected: boolean;
cookieConsentPluginActive: boolean;
loading: boolean;
onReset: () => void;
onSave: ( event: React.FormEvent< HTMLFormElement > ) => void;
restEndpoint: string;
setStep: ( updater: ( prevStep: number ) => number ) => void;
settings: LianaAutomationSettings;
step: number;
};
export default function LianaAutomationSettings(
props: LianaAutomationSettingsProps
) {
const {
channels,
connected,
cookieConsentPluginActive,
loading,
onReset,
onSave,
restEndpoint,
setStep,
settings,
step,
} = props;
const [ stateSettings, setStateSettings ] = useState( settings );
// Sync stateSettings when props.settings changes on save
useEffect( () => {
setStateSettings( settings );
}, [ settings ] );
const onInputChange = ( name: string, value: string | boolean ) => {
// Update the state or perform any action on input change
const [ prefix, key ] = name.split( '_' );
setStateSettings( ( prevSettings ) => {
return {
...prevSettings,
[ prefix ]: {
...prevSettings[ prefix ],
[ key ]: value,
},
};
} );
};
const defaultChannelOptions = useMemo(
() =>
makeOptionsOfChannels( channels, {
value: '',
label: __( 'Select a channel', 'liana-with-growthstack' ),
} ),
[ channels ]
);
const channelOptions = useMemo(
() =>
makeOptionsOfChannels( channels, {
value: 'default',
label: __( 'Default channel', 'liana-with-growthstack' ),
} ),
[ channels ]
);
let submitButtonText = __( 'Save Settings', 'liana-with-growthstack' );
if ( loading ) {
submitButtonText = __( 'Saving…', 'liana-with-growthstack' );
} else if ( step === 1 || step === 2 ) {
submitButtonText = __( 'Continue', 'liana-with-growthstack' );
}
return (
<>
{ step > 0 &&