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

/**
 * Define the dispatchable methosd available in the internal service
 */
export type IDispatchableMethods = {
	[K in "boot"|"start"|"shutdown"]: () => Promise<void>
}

export abstract class InternalService<M extends Microservice = Microservice> extends EventEmitter implements IDispatchableMethods
{
	/**
	 * The name of the service
	 */
	public abstract readonly NAME: string;

	/**
	 * Store a reference to the owning microservice instance
	 */
	readonly #microservice: M;

	/**
	 * Create a new internal service instance
	 */
	public constructor(microservice: M) {
		super();
		this.#microservice = microservice;
	}

	/**
	 * Boot the service
	 */
	public async boot() {
		// NO-OP
	}

	/**
	 * Start the service
	 */
	public async start() {
		// NO-OP
	}

	/**
	 * Shutdown the service
	 */
	public async shutdown() {
		// NO-OP
	}

	// Accessors -----------------------------------------------------------------------------------

	/**
	 * Fetch the reference to the owning microservice instance
	 */
	public get microservice() {
		return this.#microservice;
	}
}
