import { Microservice } from "./Microservice";
import EventEmitter from "events";

/**
 * A generic service
 */
export abstract class InternalService<T extends Microservice = Microservice> extends EventEmitter
{
	/**
	 * The application instance
	 */
	protected readonly app: T;

	/**
	 * Enable/disable logging for this service
	 */
	public logging: boolean = true;

	/**
	 * Create a new service
	 */
	public constructor(app: T) {
		super();
		this.app = app;
	}

	// Required Service Implementation -------------------------------------------------------------

	/**
	 * The service name
	 */
	public abstract readonly NAME: string;

	/**
	 * Boot the service
	 */
	public async boot(): Promise<void> {
		// no-op
	}

	/**
	 * Shut the application down
	 */
	 public async shutdown(): Promise<void> {
		 // no-op
	 }

	// Miscellaneous ------------------------------------------------------------------------------

	/**
	 * Link to local services
	 */
	public link(app: T) {
		// no-op
	}

	/**
	 * Indicate the application is ready
	 */
	public start() {
		// no-op
	};

	/**
	 * Service-specific logging
	 */
	public log(...args: any[]) {
		if (!this.logging) {
			return;
		}
		console.log(`[${this.NAME}]:`, ...args);
	}
}
