"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseRequestEvent = exports.BaseEvent = exports.EventEmitter = void 0;
const events_1 = require("events");
/**
 * Event emitter
 */
class EventEmitter {
    /**
     * @internal
     */
    constructor() {
        this.internal = new events_1.EventEmitter();
    }
    /**
     * Add an event listener for a particular event type.
     * @param eventName - The name of the event.
     * @param handler - The event handler.
     */
    addEventListener(eventName, handler) {
        this.internal.addListener(eventName, handler);
    }
    /**
     * Remove an event listener for a particular event type.
     * @param eventName - The name of the event.
     * @param handler - The event handler to remove. If none is provided, all
     * handlers for this event name are removed.
     */
    removeEventListener(eventName, handler) {
        if (handler) {
            this.internal.removeListener(eventName, handler);
        }
        else {
            this.internal.removeAllListeners();
        }
    }
    /**
     * Remove all event listeners.
     */
    removeAllEventListeners() {
        this.internal.removeAllListeners();
    }
    /**
     * Emit an event of the given type.
     * @param eventName - The name of the event.
     * @param args - The event arguments.
     */
    emit(eventName, ...args) {
        this.internal.emit(eventName, ...args);
    }
}
exports.EventEmitter = EventEmitter;
/**
 * The base event type.
 * @typeparam TData - Type of data
 */
class BaseEvent {
    /**
     * @internal
     */
    constructor(data) {
        this.data = data;
    }
}
exports.BaseEvent = BaseEvent;
/**
 * The base request event type.
 *
 * @typeparam TAcceptedOptions - options type
 * @typeparam TAccepted - return type
 */
class BaseRequestEvent {
    /**
     * @internal
     */
    constructor(onAccepted, onRejected) {
        this.onAccepted = onAccepted;
        this.onRejected = onRejected;
    }
    /**
     * Accept the request, returning the accepted object.
     * @param options - options
     * @returns accepted ojbect
     */
    accept(options) {
        return __awaiter(this, void 0, void 0, function* () {
            const instance = yield this.acceptInternal(options);
            if (this.onAccepted) {
                this.onAccepted(instance);
            }
            return instance;
        });
    }
    /**
     * Reject the request, with an optional reason.
     * @param reason - reason
     * @returns Promise
     */
    reject(reason) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.rejectInternal(reason);
            if (this.onRejected) {
                this.onRejected(reason);
            }
        });
    }
}
exports.BaseRequestEvent = BaseRequestEvent;
//# sourceMappingURL=index.js.map