# baseURI: http://tests.script.topbraid.org/actions/batchactionwizard

@prefix dash: <http://datashapes.org/dash#> .
@prefix ex: <http://tests.script.topbraid.org/actions/batchactionwizard#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://tests.script.topbraid.org/actions/batchactionwizard>
  a owl:Ontology ;
  <http://topbraid.org/swa#defaultNamespace> "http://tests.script.topbraid.org/actions/batchactionwizard#" ;
  rdfs:label "New File (BatchActionWizard.ttl)" ;
  owl:imports <http://datashapes.org/dash> ;
.
ex:MyBatchActions
  a dash:ActionGroup ;
  rdfs:label "My Batch Actions" ;
.
ex:ReplacePropertyValuesBatchAction
  a dash:BatchAction ;
  dash:actionGroup ex:MyBatchActions ;
  dash:canWrite true ;
  dash:js """function begin() {
    let propertyURIs = new Set();
    focusNodes.forEach(node => {
        graph.triples(node, null, null, true).
            filter(t => t.object.isLiteral() && t.object.datatype == xsd.string.uri).
            forEach(t => {
                propertyURIs.add(t.predicate.uri);
            });
    })
    let properties = [...propertyURIs];
    properties.sort();
    return DataViewers.createNextPageJSON({
        title: 'Select property',
        message: 'Select one property from those that have (string) values at the selected assets',
        params: [
            {
                varName: 'property',
				label: 'property',
                enumValues: properties.map(uri => ({
					label: graph.qname(uri),
					uri: uri, 
				}))
            }
        ],
        callback: createPage2,
    })
}

function createPage2() {
	let nodes = focusNodes.filter(node => node.values(property).length > 0);
	return DataViewers.createNextPageJSON({
		title: 'Enter new values',
		message: `Here you can enter new values for the property ${graph.qname(property)} for each of the ${nodes.length} assets. Leave empty to not overwrite.`,
		last: true,
		params: nodes.map((node, index) => ({
			varName: `value${index}`,
			label: node.toString(),
			datatype: xsd.string,
			optional: true,
		})),
		callback: execute,
		paramsObject: true,  // Declare 'params' object instead of individual named variables
		state: {
			property: property,
		}
	})
}

function execute(state) {
	let nodes = focusNodes.filter(node => node.values(state.property).length > 0);
	nodes.forEach((node, index) => {
		let value = params[`value${index}`]; // Fetch value0, value1 etc
		if(value) {
			graph.remove(node, state.property);
			node.add(state.property, value);
		}
	})
}
""" ;
  dash:wizard true ;
  rdfs:comment """A batch action to demonstrate the wizard capabilities.

On the first page, the user selects a property that has literal values within some of the given nodes.

On the second page, the user can enter new values for all instances that have the selected property.

On OK the values get replaced.""" ;
  rdfs:label "Replace property values..." ;
.
