class SplitMetadataHandler implements StepMetadataHandler {

    @Override
    public DynamicActionMetadata handle(DynamicActionMetadata metadata) {
        try {
            if (metadata.outputShape() != null) {
                DataShape dataShape = new DataShape.Builder()
                                            .createFrom(metadata.outputShape())
                                            .decompress()
                                            .build();

                if (StepMetadataHelper.isUnifiedJsonSchemaShape(dataShape)) {
                    dataShape = new DataShape.Builder()
                                        .createFrom(dataShape)
                                        .specification(extractUnifiedJsonBodySpec(dataShape.getSpecification()))
                                        .build();
                }

                Optional<DataShape> singleElementShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT);

                if (singleElementShape.isPresent()) {
                    if (dataShape.equals(singleElementShape.get())) {
                        return new DynamicActionMetadata.Builder()
                                        .createFrom(metadata)
                                        .outputShape(singleElementShape.get())
                                .build();
                    } else {
                        return new DynamicActionMetadata.Builder()
                                .createFrom(metadata)
                                .outputShape(new DataShape.Builder()
                                        .createFrom(singleElementShape.get())
                                        .addAllVariants(StepMetadataHelper.extractVariants(dataShape, singleElementShape.get(), DataShapeMetaData.VARIANT_ELEMENT))
                                        .build())
                                .build();
                    }
                }

                DataShape collectionShape = dataShape.findVariantByMeta(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_COLLECTION).orElse(dataShape);

                String specification = collectionShape.getSpecification();
                if (StringUtils.hasText(specification)) {
                    if (collectionShape.getKind() == DataShapeKinds.JSON_SCHEMA) {
                        JsonSchema schema = JsonSchemaUtils.reader().readValue(specification);

                        if (schema.isArraySchema()) {
                            ArraySchema.Items items = schema.asArraySchema().getItems();
                            if (items.isSingleItems()) {
                                JsonSchema itemSchema = items.asSingleItems().getSchema();
                                itemSchema.set$schema(schema.get$schema());
                                return new DynamicActionMetadata.Builder()
                                        .createFrom(metadata)
                                        .outputShape(new DataShape.Builder().createFrom(collectionShape)
                                                .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT)
                                                .specification(JsonUtils.writer().writeValueAsString(itemSchema))
                                                .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION))
                                                .build())
                                        .build();
                            }
                        } else {
                            return new DynamicActionMetadata.Builder()
                                    .createFrom(metadata)
                                    .outputShape(new DataShape.Builder().createFrom(collectionShape)
                                            .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT)
                                            .build())
                                    .build();
                        }
                    } else if (collectionShape.getKind() == DataShapeKinds.JSON_INSTANCE) {
                        if (JsonUtils.isJsonArray(specification)) {
                            List<Object> items = JsonUtils.reader().forType(List.class).readValue(specification);
                            if (!items.isEmpty()) {
                                return new DynamicActionMetadata.Builder()
                                        .createFrom(metadata)
                                        .outputShape(new DataShape.Builder().createFrom(collectionShape)
                                                .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT)
                                                .specification(JsonUtils.writer().writeValueAsString(items.get(0)))
                                                .addAllVariants(StepMetadataHelper.extractVariants(dataShape, collectionShape, DataShapeMetaData.VARIANT_COLLECTION))
                                                .build())
                                        .build();
                            }
                        } else {
                            return new DynamicActionMetadata.Builder()
                                    .createFrom(metadata)
                                    .outputShape(new DataShape.Builder().createFrom(collectionShape)
                                            .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT)
                                            .build())
                                    .build();
                        }
                    }
                }
            }
        } catch (IOException e) {
            LOG.warn("Unable to read output data shape on dynamic meta data inspection", e);
        }

        return DynamicActionMetadata.NOTHING;
    }

}