import {
  OpenWidgetCommand,
  StandardWidget,
  StandardWidgetOptions,
  Viewer,
  WidgetFactoryFunction
} from '@stereograph/teiaviewer';

// Create the class of the new widget we want to add to the library
class ExampleWidget extends StandardWidget {
  readonly type = 'ExampleWidget';
  public _myParam: number;
  constructor(viewer: Viewer, myParam: number, options: StandardWidgetOptions = {}) {
    super(viewer, options);
    this._myParam = myParam;
  }
}

// Declare the Factory function in the global scope. This is mandatory to make the factory
// aknowledge the new type.
// The first generic param is the class of the widget to register
// The second generic param is the type of param to require to create a the widget
declare global {
  interface ViewerWidgetRegistry {
    ExampleWidget: WidgetFactoryFunction<ExampleWidget, { myParam: number }>;
  }
}

// Register the widget to the viewer factory
const viewer = new Viewer();
viewer.widgetFactory.registerWidget('ExampleWidget', ({ myParam }) => {
  return new ExampleWidget(viewer, myParam);
});

// Open the widget
// Here myParam is required to open the widget as indicated in the WidgetFactoryFunction
const cmd = new OpenWidgetCommand(viewer, 'ExampleWidget', {
  myParam: 12
});
cmd.execute();