## IMPORTS

# Utilities to read/write csv files
import json
import re

def getFormFields(mapfilename):

    with open (mapfilename) as mapfile:
        triple_blocks = json.load(mapfile)

        all_csvs = []
        for block in triple_blocks:
            all_csvs = all_csvs + extractFields(block)

        all_csvs_filtered = []
        for csv in all_csvs:
            if csv in all_csvs_filtered:
                continue
            all_csvs_filtered.append(csv)

        return all_csvs_filtered


def extractFields(entry: dict):
    try:
        subject_val = entry['subject']['value'] if type(entry['subject']) is dict else entry['subject']
        all_csvs = list( csvsFromVals(subject_val) )

        objs = map(lambda el: el['object'], entry['content'])
        for obj in objs:
            val = obj['value'] if type(obj) is dict else obj
            obj_csvs = csvsFromVals(val)
            all_csvs = all_csvs + list(obj_csvs)

        return all_csvs        
            
    except:
        raise Exception('Malformed Configuration File')

def csvsFromVals(value: str):
    val_parts = re.split('#', value)
    val_csvs = list( filter(lambda str1: str1.startswith('csv:'), val_parts) )
    val_csvs = map(lambda str1: str1.replace('csv:', ''), val_csvs)
    return val_csvs
