"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncEE = exports.AsyncEERef = exports.AsyncEEWatch = exports.AsyncEECall = exports.AsyncEEGet = void 0;
const _1 = require("../../../Class/$");
const isObjNotNull_1 = require("../../../system/f/object/DeepMerge/isObjNotNull");
const object_1 = require("../../../util/object");
const AsyncEEGet = (emitter) => {
    return {
        $eventNames(data, callback) {
            const eventNames = emitter.eventNames();
            callback(eventNames);
        },
    };
};
exports.AsyncEEGet = AsyncEEGet;
const AsyncEECall = (emitter) => {
    return {
        $emit({ event, data }, callback) {
            emitter.emit(event, data);
            callback();
        },
    };
};
exports.AsyncEECall = AsyncEECall;
const AsyncEEWatch = (emitter) => {
    return {
        $addListener({ event }, callback) {
            return emitter.addListener(event, (...args) => {
                const deref = (data) => {
                    if (data instanceof _1.$) {
                        return { globalId: data.getGlobalId(), __: data.getInterface() };
                    }
                    if (Array.isArray(data)) {
                        data = data.map(deref);
                    }
                    else if ((0, isObjNotNull_1.isObjNotNull)(data)) {
                        data = (0, object_1.mapObjKV)(data, (key, value) => {
                            return deref(value);
                        });
                    }
                    return data;
                };
                const data = args.map(deref);
                callback(data);
            });
        },
    };
};
exports.AsyncEEWatch = AsyncEEWatch;
const AsyncEERef = (emitter) => {
    return {};
};
exports.AsyncEERef = AsyncEERef;
const AsyncEE = (emitter) => {
    return {
        ...(0, exports.AsyncEEGet)(emitter),
        ...(0, exports.AsyncEECall)(emitter),
        ...(0, exports.AsyncEEWatch)(emitter),
        ...(0, exports.AsyncEERef)(emitter),
    };
};
exports.AsyncEE = AsyncEE;
//# sourceMappingURL=AsyncEE.js.map