import { isFormField, isFormGroup } from "@inkline/inkline/types";
import { reservedValidationFields } from "@inkline/inkline/constants";
export function serializeSchema(schema) {
  const serializedSchema = {};
  Object.keys(schema).forEach((key) => {
    if (!schema.hasOwnProperty(key) || reservedValidationFields.includes(key)) {
      return;
    }
    const schemaField = schema[key];
    if (Array.isArray(schemaField)) {
      serializedSchema[key] = schemaField.map(
        (item) => {
          if (isFormGroup(item)) {
            return serializeSchema(item);
          }
          return item.value;
        }
      );
    } else if (isFormGroup(schemaField)) {
      serializedSchema[key] = serializeSchema(
        schemaField
      );
    } else if (isFormField(schemaField)) {
      serializedSchema[key] = schemaField.value;
    }
  });
  return serializedSchema;
}
