import {
  ArrayInput,
  AttachmentInput,
  BooleanInput,
  LocalizedTextInput,
  SimpleForm,
  SimpleFormIterator,
  required
} from '@/';

function requiredAtLeastOneTranslation(message = 'ra.validation.at_least_one_translation') {
  return (value, values) => {
    if (!value || !Object.keys(value).length) {
      return message;
    }
    return undefined;
  };
}
function CategoryForm(props) {
  return (
    <SimpleForm {...props}>
      <LocalizedTextInput source="description" fullWidth validate={requiredAtLeastOneTranslation()} />
      <BooleanInput source="active" />
      <AttachmentInput source="attachment" fullWidth />
      <AttachmentInput source="attachments" multiple fullWidth />
      <ArrayInput source="subCategories" fullWidth>
        <SimpleFormIterator divider>
          <AttachmentInput label={false} source="attachments" multiple fullWidth />
        </SimpleFormIterator>
      </ArrayInput>
    </SimpleForm>
  );
}

export { CategoryForm };
