import cx from 'classnames';

const plugin = prevConfig => {
  const classes = {
    Field: ({ field }) => ({
      'Formatic_Field-IsReadOnly': prevConfig.fieldIsReadOnly(field),
    }),
    Json_TextareaInput: ({ isValid }) => ({
      'Formatic_Json_TextareaInput-HasError': !isValid,
    }),
    PrettyText_PrettyTextInputTabTarget: ({ field }) => {
      if (prevConfig.fieldTemplateIsSingleLine(field.rawFieldTemplate)) {
        return {
          'Formatic_PrettyText_PrettyTextInputTabTarget-IsSingleLine': true,
          'Formatic_PrettyTextInputTabTarget-IsSingleLine': true,
        };
      }
      return undefined;
    },
    RequiredLabel: ({ field }) => {
      const errorSet = new Set(
        prevConfig.fieldErrors(field).map(error => error.type)
      );
      return {
        'Formatic_RequiredLabel-IsRequired': prevConfig.fieldIsRequired(field),
        'Formatic_RequiredLabel-IsNotRequired': !prevConfig.fieldIsRequired(
          field
        ),
        'Formatic_RequiredLabel-IsRequiredError': errorSet.has('required'),
      };
    },
    SelectArrow: ({ isOpen }) => {
      return {
        'Formatic_SelectArrow-IsOpen': isOpen,
      };
    },
  };

  const origRenderTag = prevConfig.renderTag;

  return {
    renderTag: (tagName, tagProps, metaProps, ...children) => {
      tagProps = {
        ...tagProps,
      };
      const typeName = metaProps.typeName || metaProps.parentTypeName;
      const { elementName } = metaProps;
      const fieldElementName = `${typeName}_${elementName}`;
      tagProps.className = cx(
        tagProps.className,
        `Formatic_${fieldElementName}`,
        `Formatic_${elementName}`,
        classes[typeName] && classes[typeName](metaProps),
        classes[elementName] && classes[elementName](metaProps),
        classes[fieldElementName] && classes[fieldElementName](metaProps)
      );
      return origRenderTag(tagName, tagProps, metaProps, ...children);
    },
  };
};

export default plugin;

@@ -38,21 +38,17 @@
 lugin = 
-prevC
+c
 onfig =%3E
@@ -50,16 +50,48 @@
 ig =%3E %7B%0A
+  const %7B renderTag %7D = config;%0A
   const 
@@ -166,21 +166,17 @@
 dOnly': 
-prevC
+c
 onfig.fi
@@ -381,21 +381,17 @@
     if (
-prevC
+c
 onfig.fi
@@ -727,21 +727,17 @@
         
-prevC
+c
 onfig.fi
@@ -847,21 +847,17 @@
 uired': 
-prevC
+c
 onfig.fi
@@ -927,21 +927,17 @@
 ired': !
-prevC
+c
 onfig.fi
@@ -954,33 +954,13 @@
 red(
-%0A          field%0A        
+field
 ),%0A 
@@ -1173,55 +1173,8 @@
 %7D;%0A%0A
-  const origRenderTag = prevConfig.renderTag;%0A%0A
   re
@@ -1824,13 +1824,9 @@
 urn 
-origR
+r
 ende

Clarify correct config