"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.AutoBeAgentBase = void 0;
const getAutoBeGenerated_1 = require("./factory/getAutoBeGenerated");
const emplaceMap_1 = require("./utils/emplaceMap");
class AutoBeAgentBase {
    constructor(asset) {
        this.asset = asset;
        this.listeners_ = new Map();
    }
    getFiles(options) {
        return __awaiter(this, void 0, void 0, function* () {
            return (0, getAutoBeGenerated_1.getAutoBeGenerated)(yield this.asset.compiler(), this.asset.state(), this.getHistories(), this.getTokenUsage(), options);
        });
    }
    on(type, listener) {
        (0, emplaceMap_1.emplaceMap)(this.listeners_, type, () => new Set()).add(listener);
        return this;
    }
    off(type, listener) {
        const set = this.listeners_.get(type);
        if (set === undefined)
            return this;
        set.delete(listener);
        if (set.size === 0)
            this.listeners_.delete(type);
        return this;
    }
    /** @internal */
    dispatch(event) {
        return __awaiter(this, void 0, void 0, function* () {
            const set = this.listeners_.get(event.type);
            if (set === undefined)
                return;
            yield Promise.all(Array.from(set).map((listener) => __awaiter(this, void 0, void 0, function* () {
                try {
                    yield listener(event);
                }
                catch (_a) { }
            })));
        });
    }
}
exports.AutoBeAgentBase = AutoBeAgentBase;
//# sourceMappingURL=AutoBeAgentBase.js.map