
/**
 * Custom documentation about the VFX Framework.
 * {@include ../../../docs/md/vfx-framework.md}
 * @module vfx
 */

import VFXPath from "@client/canvas/vfx/vfx-path.mjs";
import * as components from "./components/_module.mjs";
import * as animations from "./animations/_module.mjs";
import {default as arcPath} from "./paths/vfx-arc-path.mjs";
import {default as weavePath} from "./paths/vfx-weave-path.mjs";

/**
 * @import {VFXComponentAnimation, VFXPathGenerator} from "./canvas/vfx/_types.mjs";
 */

let _vfxActive = false;

/**
 * Activate the animejs for the Canvas ticker.
 * This is called during `Canvas#draw` before the `canvasReady` hook.
 */
export function activate() {
  if ( !CONFIG.Canvas.vfx.enabled ) return;
  console.log("%cThe Foundry VTT version 14 EXPERIMENTAL VFX framework is enabled."
    + " Its classes, functions, and configuration are likely to change over coming releases."
    + " The entire VFX module should be treated as non-stable regardless of the stability designation of the release"
    + " build that contains it.", "color: #cc6600");
  _vfxActive = true;
  animejs.engine.useDefaultMainLoop = false;
  const ticker = canvas.app.ticker;
  ticker.add(_onTick, undefined, PIXI.UPDATE_PRIORITY.HIGH);
}

/* -------------------------------------------- */

/**
 * Deactivate the animejs engine for the Canvas ticker.
 * This is called during `Canvas#teardown` before the `canvasTearDown` hook.
 */
export function deactivate() {
  if ( !_vfxActive ) return;
  const ticker = canvas.app.ticker;
  ticker.remove(_onTick);
  animejs.engine.useDefaultMainLoop = true;
}

/* -------------------------------------------- */

/**
 * Advance the animejs engine every tick.
 * @param {number} _dt
 */
function _onTick(_dt) {
  animejs.engine.update();
}

/* -------------------------------------------- */
/*  One-Time Configuration                      */
/* -------------------------------------------- */

/**
 * One-time configuration to register components, animations, and paths to CONFIG.Canvas.vfx.
 */
export function configure() {
  const cfg = CONFIG.Canvas.vfx;

  // Register components
  for ( const [componentName, component] of Object.entries(components) ) cfg.components[componentName] = component;

  // Register paths
  cfg.paths.linear = (points, _parameters) => new VFXPath(points);
  cfg.paths.arc = arcPath;
  cfg.paths.weave = weavePath;

  // Register animations
  for ( const [animationName, animation] of Object.entries(animations) ) cfg.animations[animationName] = animation;
}

/* -------------------------------------------- */
/*  Module Exports                              */
/* -------------------------------------------- */

export * as fields from "./fields/_module.mjs";
export * as constants from "./vfx-constants.mjs";
export * as utils from "./vfx-utils.mjs";
export {default as VFXCanvasContainer} from "./vfx-canvas-container.mjs";
export {default as VFXComponent} from "./vfx-component.mjs";
export {default as VFXEffect} from "./vfx-effect.mjs";
export {animations, components, VFXPath};
