// Copyright (c) Gabriel Lepetit-Aimon
// Distributed under the terms of the Modified BSD License.

import { IJupyterWidgetRegistry } from "@jupyter-widgets/base";
import { ICommandPalette } from "@jupyterlab/apputils";
import { Application, IPlugin } from "@phosphor/application";
import { Widget } from "@phosphor/widgets";

import * as widgetExports from "./widgets";

import { initializeJApp } from "./ipywidgets/jbasewidget";
import { MODULE_NAME, MODULE_VERSION } from "./version";

/**
 * Register the plugin.
 */
const EXTENSION_ID = "jppype:plugin";
const jppypeWidgetsPlugin: IPlugin<Application<Widget>, void> = {
  id: EXTENSION_ID,
  requires: [IJupyterWidgetRegistry, ICommandPalette],
  activate: activateWidgetExtension,
  autoStart: true,
} as unknown as IPlugin<Application<Widget>, void>;
// the "as unknown as ..." typecast above is solely to support JupyterLab 1
// and 2 in the same codebase and should be removed when we migrate to Lumino.

export default jppypeWidgetsPlugin;

/**
 * Activate the widget extension.
 */
function activateWidgetExtension(
  app: Application<Widget>,
  registry: IJupyterWidgetRegistry,
  palette: ICommandPalette
): void {
  initializeJApp(app, palette);

  registry.registerWidget({
    name: MODULE_NAME,
    version: MODULE_VERSION,
    exports: widgetExports,
  });
}
