import { copy } from './common';
import DefinitionPattern from './DefinitionPattern';
import sparqljs from 'sparqljs';
import attributeGetter from '../SparqlAttribute/getter';

export function bound(reference: string, definition: DefinitionPattern, json_pointers: string, value: string) {
    let incremented_definition = copy(definition);

    let { attribute, nestedPath } = { ...attributeGetter(reference, json_pointers) };
    let pointerToDefinition = incremented_definition;
    nestedPath.forEach((path) => {
        pointerToDefinition = pointerToDefinition.subQuery[path];
    });
    let bound = {
        type: 'filter',
        expression:
        {
            type: 'operation',
            operator: 'bound',
            args: ['?' + attribute.name]
        }
    };
    // let look_alike = `FILTER regex(STR(?${attribute.name}), '${value}', 'i') .\n`
    (<sparqljs.Pattern[]>pointerToDefinition.query.where).push(<sparqljs.Pattern>bound);
    return incremented_definition;
}

export function prependBound(operation: sparqljs.OperationExpression, attributeName: string) {
    return <sparqljs.OperationExpression>{
            type: 'operation',
            operator: '&&',
            args:
                [{ type: 'operation', operator: 'bound', args: ['?' + attributeName] },
                operation]
    };
}
export default bound;