"use strict";
var _EventFlow_listenerInstances, _EventFlow_context;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventFlow = void 0;
const tslib_1 = require("tslib");
const runtime_1 = require("./runtime");
const generate_1 = require("./adapter/generate");
class EventFlow {
    constructor({ schema, context, options, }) {
        _EventFlow_listenerInstances.set(this, void 0);
        _EventFlow_context.set(this, void 0);
        tslib_1.__classPrivateFieldSet(this, _EventFlow_context, context, "f");
        const { id, description, listeners = [] } = schema || {};
        const startNode = listeners[0];
        const [_, eventFlowId] = (startNode === null || startNode === void 0 ? void 0 : startNode.eventName.match(/(.*?)\.start/)) || [];
        this.id = id || eventFlowId;
        this.description = description || (startNode === null || startNode === void 0 ? void 0 : startNode.description) || '';
        tslib_1.__classPrivateFieldSet(this, _EventFlow_listenerInstances, (0, generate_1.generateListnerInstances)({ $app: tslib_1.__classPrivateFieldGet(this, _EventFlow_context, "f").$app, $page: tslib_1.__classPrivateFieldGet(this, _EventFlow_context, "f").$page }, listeners || [], Object.assign(Object.assign({}, options), { key: `事件流 ${id}`, useScopeWidgetData: options.useScopeWidgetData === undefined ? true : options.useScopeWidgetData })), "f");
        /**
         * TODO: 缺乏 currentTarget
         */
        // const { $w } = context || {};
        // this._emit = generateEmit($w);
        return this;
    }
    trigger(additionalScope, options = {}) {
        const mergedContext = Object.assign(Object.assign({}, tslib_1.__classPrivateFieldGet(this, _EventFlow_context, "f")), options);
        const { $w, target, scopeContext, forContext, dataContext } = mergedContext;
        const emit = (trigger, eventData, originalEvent) => (0, runtime_1.generateEmit)($w, target)(trigger, tslib_1.__classPrivateFieldGet(this, _EventFlow_listenerInstances, "f"), eventData, forContext, originalEvent, scopeContext, dataContext, $w.page.id, true);
        return emit(`${this.id}.start`, additionalScope);
    }
    toJSON() {
        const KEYS = ['id', 'description'];
        return KEYS.reduce((obj, key) => {
            obj[key] = this[key];
            return obj;
        }, {});
    }
}
exports.EventFlow = EventFlow;
_EventFlow_listenerInstances = new WeakMap(), _EventFlow_context = new WeakMap();
//# sourceMappingURL=flow.js.map