import SparqlObject from './SparqlObject'
import SparqlTask from '../Core/SparqlTask'
import sparqljs from 'sparqljs';
import { jsPointerToBinding, parseValue } from './common';
import formatter from '../common/stringifier';
import jsonpointer from 'json-pointer'

export interface InsertModel {
    model: string,
    instances: {[key: string]: any}[]
}

// export function buildInsert(this: SparqlObject, instance: { [json_pointer: string]: sparqljs.Term }) {
//     let parsedInstances: string[] = [];
//     // let attributesDictionary = this.dictionify();
//     let dictionary: { [key: string]: string } = {};
//     // Add all the triples.
//     // Uniquify these to get the definition back
//     parsedInstances = parsedInstances.filter((value, index, self) => {
//         return self.indexOf(value) === index;
//     });
//     Object.keys(instance)
//         .forEach((json_pointer) => {
//             let rootPointer = json_pointer.replace(/\/\d+$/, '')
//             let binding = jsPointerToBinding(rootPointer);
//             let value = parseValue(instance[json_pointer]);
//             // let attribute = attributesDictionary[rootPointer];
//             // if ((rootPointer.length !== json_pointer.length) // The jspointer decomposition reveals an array element
//             //     && attribute.arrayable) {
//             //     // Add triples which have multiple values for the same binding
//             //     let baseTriples = attribute.triples;
//             //     // baseTriples.forEach((tripleFromAttribute) => {
//             //     //     // Remove the general definition
//             //     //     let indexToRemove = parsedInstances.indexOf(tripleFromAttribute);
//             //     //     // Check is necessary as splice(-1, 1) will remove the last element
//             //     //     if (indexToRemove !== -1) {
//             //     //         // This will occur only once
//             //     //         parsedInstances.splice(indexToRemove, 1);
//             //     //     }
//             //     //     let addedTriple = tripleFromAttribute.replace(binding, value)
//             //     //     parsedInstances.push(addedTriple)
//             //     // })
//             // } else {
//             //     // And dictionary key:value for further string formatting
//             //     dictionary[binding] = value;
//             // }
//         });
//     return formatter(parsedInstances.join('.\n'), dictionary,{asRegExp: false})
// }


// export function insertModel(insertQuery: InsertModel) {
//     return insertQuery.instances.map((instance) => {
//         let dictObject = jsonpointer.dict(instance); // Flatten the object with a jspointer as key
//         let parsedInsert = SparqlTask.sparqlObjects[insertQuery.model].buildInsert(dictObject as {[json_pointer:string]: sparqljs.Term}) 
//         return parsedInsert;
//     }).join('\n\n')
// }
// export default buildInsert;