/**
 * Copyright 2018 Twitter, Inc.
 * Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 */ import { ensureArray } from "sqrl-common" ; import { Context , Execution , Instance , Manipulator , WhenCause , SqrlEntity , AT , } from "sqrl" ; export interface LabelService { addLabel ( manipulator , entity , label , cause ) ; removeLabel ( manipulator , entity , label , cause ) ; hasLabel ( ctx , entity , label ) : Promise < boolean > ; } export function registerLabelFunctions ( instance , service ) { instance . registerStatement ( "SqrlLabelStatements" , async function addLabel ( state , cause , entities , label ) { ensureArray ( entities ) . forEach ( ( entity ) => { if ( entity !== null ) { service . addLabel ( state . manipulator , entity , label , cause ) ; } } ) ; } , { args : [ AT . state , AT . whenCause , AT . any . sqrlEntityOrEntities , AT . constant . string , ] , allowNull : true , allowSqrlObjects : true , argstring : "entity | entity list, label" , docstring : "Adds the provided label to the specified entities" , } ) ; instance . registerStatement ( "SqrlLabelStatements" , async function removeLabel ( state , cause , entities , label ) { ensureArray ( entities ) . forEach ( ( entity ) => { if ( entity !== null ) { service . removeLabel ( state . manipulator , entity , label , cause ) ; } } ) ; } , { args : [ AT . state , AT . whenCause , AT . any . sqrlEntityOrEntities , AT . constant . string , ] , allowNull : true , allowSqrlObjects : true , argstring : "entity | entity list, label" , docstring : "Removes the provided label to the specified entities" , } ) ; instance . register ( async function hasLabel ( state , entity , label ) { return service . hasLabel ( state . ctx , entity , label ) ; } , { args : [ AT . state , AT . any . sqrlEntity , AT . constant . string ] , allowSqrlObjects : true , argstring : "entity, label" , docstring : "Returns true if the provided entity has the given label" , } ) ; }