///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2025, Open Design Alliance (the "Alliance").
// All rights reserved.
//
// This software and its documentation and related materials are owned by
// the Alliance. The software may only be incorporated into application
// programs owned by members of the Alliance, subject to a signed
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable
// trade secrets of the Alliance and its suppliers. The software is also
// protected by copyright law and international treaty provisions. Application
// programs incorporating this software must include the following statement
// with their copyright notices:
//
//   This application incorporates Open Design Alliance software pursuant to a
//   license agreement with Open Design Alliance.
//   Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance.
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////

import { IViewer } from "../viewer/IViewer";

/**
 * Defines the command executer service interface.
 */
export interface ICommandService {
  /**
   * Executes the command denoted by the given command ID.
   *
   * @param id - ID of the command to execute.
   * @param args - Parameters passed to the command handler function.
   * @returns Returns the result of the command handler function. Returns `undefined` when the command
   *   doesn't exists.
   */
  executeCommand(id: string, ...args: any[]): any;
}

/**
 * Defines the command map.
 */
export type ICommandsMap = Map<string, ICommand>;

/**
 * Defines the command handler function.
 */
export interface ICommandHandler {
  /**
   * @param viewer - Viewer instance that executes the command.
   * @param args - Parameters passed to the command handler function.
   */
  (viewer: any, ...args: any[]): any;
}

/**
 * Defines the command to execute by the {@link Viewer}.
 */
export interface ICommand {
  /**
   * Command ID.
   */
  id: string;

  /**
   * Handler function.
   */
  handler: ICommandHandler;

  /**
   * The `this` context used when invoking the handler function.
   */
  thisArg?: any;

  /**
   * Command description.
   */
  description?: ICommandDescription;
}

/**
 * Defines the command description.
 */
export interface ICommandDescription {
  /**
   * Command description string.
   */
  readonly description: string;
}

/**
 * Defines the viewer commands registry interface.
 */
export interface ICommandsRegistry {
  /**
   * Binds a command ID to a handler function. Registering a command with an existing ID twice overrides
   * the existing handler.
   *
   * @param id - Unique ID for the command.
   * @param handler - Command handler function.
   * @param description - Command description.
   * @param thisArg - The `this` context used when invoking the handler function.
   */
  registerCommand(id: string, handler: ICommandHandler, description?: ICommandDescription, thisArg?: any): void;

  /**
   * Registers an alias for a command.
   *
   * @param id - Unique ID for the command.
   * @param alias - Command alias string.
   */
  registerCommandAlias(id: string, alias: string): void;

  /**
   * Returns a list of registered commands.
   */
  getCommands(): ICommandsMap;

  /**
   * Returns the specified command or `undefined` when the command doesn't exists.
   *
   * @param id - Command ID.
   */
  getCommand(id: string): ICommand | undefined;

  /**
   * Executes the command denoted by the given command. If the command is not found, tries to set active
   * dragger with the specified name.
   *
   * @param id - Command ID or dragger name.
   * @param viewer - Viewer instance that wants to execute the command.
   * @param args - Parameters passed to the command handler function.
   * @returns Returns the result of the command handler function or new active dragger instance. Returns
   *   `undefined` if neither the command nor the dragger exists.
   */
  executeCommand(id: string, viewer: IViewer, ...args: any[]): any;
}
