import React from 'react';
import AnnotationType from './AnnotationType';
import {
    ANNOTATION_TARGET,
    ANNOTATION_TYPE,
    SESSION_STORAGE_ACTIVE_TARGETS,
    SESSION_STORAGE_ACTIVE_TYPES
} from './Constants';

// Render active annotations per type
export const renderPerType = (
    annotationsPerType,
    target,
    annotation,
    annotationClient,
    activeTypes
) => {
    // render types
    return (
        <div className="annotations">
            {activeTypes.map(type => (
                <AnnotationType
                    key={type}
                    type={type}
                    target={target}
                    targetAnnotation={annotation}
                    annotations={annotationsPerType[type]}
                    annotationClient={annotationClient}
                />
            ))}
        </div>
    );
};

// Get all annotations split by type
export const getAnnotationsPerType = (activeTypes, annotation) => {
    // annotations per Type
    const annotationsPerType = {};

    // fill with active types
    Object.values(activeTypes).forEach(type => {
        annotationsPerType[type] = [];
    });

    // split annotations per type
    if (annotation && annotation.body) {
        annotation.body
            // remove empty bodies
            .filter(a => a)
            // collect
            .forEach(a => {
                // only add the annotations to active types
                if (a.annotationType in annotationsPerType) {
                    annotationsPerType[a.annotationType].push(a);
                }
            });
    }

    // render types
    return annotationsPerType;
};
