import jsonpointer from 'json-pointer'
import SparqlAttribute from './SparqlAttribute'
import SparqlTask from '../Core/SparqlTask'

export function getAttribute(rootReference: string, pointer: string) {
    let path = jsonpointer.parse(pointer);
    let nestedPath = [...path];
    nestedPath.pop();
    let model = SparqlTask.sparqlObjects[rootReference];
    let attribute: SparqlAttribute = new SparqlAttribute(model);
    while (path.length > 0) {
        attribute = model.dictionify()[<string>path.shift()];
        model = SparqlTask.sparqlObjects[attribute.reference];
    }

    return {nestedPath : nestedPath, attribute: attribute};
}

export default getAttribute