import { PropsWithChildren } from 'react';

import { AutomationTestingProps } from '@/types';

import { LoadingButton } from '@@/buttons';

import { FormSection } from './FormSection';

interface Props extends AutomationTestingProps {
  submitLabel: string;
  loadingText: string;
  isLoading: boolean;
  isValid: boolean;
  errors?: unknown;
}

export function FormActions({
  submitLabel = 'Save',
  loadingText = 'Saving',
  isLoading,
  children,
  isValid,
  errors,
  'data-cy': dataCy,
}: PropsWithChildren<Props>) {
  return (
    <FormSection title="Actions">
      <div className="form-group">
        <div className="col-sm-12">
          <div className="flex item-center gap-3">
            <LoadingButton
              className="!ml-0"
              loadingText={loadingText}
              isLoading={isLoading}
              disabled={!isValid}
              data-cy={dataCy}
            >
              {submitLabel}
            </LoadingButton>

            {!isValid && (
              <div className="hidden" data-cy="errors">
                {JSON.stringify(errors)}
              </div>
            )}

            {children}
          </div>
        </div>
      </div>
    </FormSection>
  );
}
