import { CONST, shim } from './shim.js' export class helpers { static runOnTargetOrSelectedTokens (runFunc) { let tokens = [] const targets = Array.from(shim.targets) if (targets.length > 0) { tokens = targets } else if (shim.controlled.length > 0) { tokens = shim.controlled } if (tokens.length > 0) { runFunc(tokens) } else { shim.notifications.error('Please select or target a token') } } static createEffectDocument (icon, name, durationRounds, changes) { const effectData = { icon, name, duration: { rounds: durationRounds }, flags: { swade: { expiration: CONST.SWADE.STATUS_EFFECT_EXPIRATION.EndOfTurnPrompt, loseTurnOnHold: true } }, changes } return effectData } static createMutationWithEffect (icon, name, durationRounds, changes) { const effect = helpers.createEffectDocument(icon, name, durationRounds, changes) const mutate = { embedded: { ActiveEffect: {} } } mutate.embedded.ActiveEffect[name] = effect return mutate } static defaultMutationOptions (name) { const mutateOptions = { name, permanent: true, description: name } return mutateOptions } static getActorFolderByPath (path) { const names = path.split('/') if (names[0] === '') { names.shift() } let name = names.shift() let folder = shim.folders.find(f => f.name === name && !f.folder) while (names.length > 0) { name = names.shift() folder = folder.children.find(c => c.folder.name === name) folder = folder.folder } return folder } static getActorsInFolder (inFolder) { const prefixStack = [''] const actors = {} const folderStack = [inFolder] while (folderStack.length > 0) { const prefix = prefixStack.shift() const folder = folderStack.shift() for (const actor of folder.contents) { if ( shim.user.isGM || actor.testUserPermission( shim.user, CONST.FOUNDRY.DOCUMENT_OWNERSHIP_LEVELS.OBSERVER) ) { actors[`${prefix}${actor.name}`] = actor } } for (const child of folder.children) { const newPrefix = `${prefix}${child.folder.name} | ` prefixStack.push(newPrefix) folderStack.push(child.folder) } } return actors } }