"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.watchUnitEvent = watchUnitEvent;
exports.watchUnitRenamePinEvent = watchUnitRenamePinEvent;
exports.watchStatefulSetEvent = watchStatefulSetEvent;
exports.watchElementCallEvent = watchElementCallEvent;
exports.watchComponentAppendEvent = watchComponentAppendEvent;
exports.watchComponentAppendChildrenEvent = watchComponentAppendChildrenEvent;
exports.watchComponentRemoveEvent = watchComponentRemoveEvent;
const stringify_1 = require("../spec/stringify");
function watchUnitEvent(event, unit, callback) {
    const listener = (data) => {
        callback({
            type: 'unit',
            event,
            data,
        });
    };
    unit.addListener(event, listener);
    return () => {
        unit.removeListener(event, listener);
    };
}
function watchUnitRenamePinEvent(event, unit, callback) {
    const listener = (name, newName) => {
        callback({
            type: 'unit',
            event,
            data: {
                name,
                newName,
            },
        });
    };
    unit.addListener(event, listener);
    return () => {
        unit.removeListener(event, listener);
    };
}
function watchStatefulSetEvent(event, unit, callback) {
    const listener = (name, data) => {
        data = data !== undefined ? (0, stringify_1.stringify)(data) : data;
        callback({
            type: 'unit',
            event,
            data: { name, data },
        });
    };
    unit.addListener(event, listener);
    return () => {
        unit.removeListener(event, listener);
    };
}
function watchElementCallEvent(event, unit, callback) {
    const listener = (data) => {
        callback({
            type: 'unit',
            event,
            data,
        });
    };
    unit.addListener(event, listener);
    return () => {
        unit.removeListener(event, listener);
    };
}
function watchComponentAppendEvent(event, unit, callback) {
    const listener = ({ bundle, child, _, }, path = []) => {
        if (path.length > 0) {
            return;
        }
        callback({
            type: 'unit',
            event,
            data: { bundle },
            path,
        });
    };
    unit.addListener(event, listener);
    return () => {
        unit.removeListener(event, listener);
    };
}
function watchComponentAppendChildrenEvent(event, unit, callback) {
    const listener = ({ bundles, }, path = []) => {
        if (path.length > 0) {
            return;
        }
        callback({
            type: 'unit',
            event,
            data: { bundles },
            path,
        });
    };
    unit.addListener(event, listener);
    return () => {
        unit.removeListener(event, listener);
    };
}
function watchComponentRemoveEvent(event, unit, callback) {
    const listener = ({ at }, path = []) => {
        if (path.length > 0) {
            return;
        }
        callback({
            type: 'unit',
            event,
            data: { at },
            path,
        });
    };
    // @ts-ignore
    unit.addListener(event, listener);
    return () => {
        // @ts-ignore
        unit.removeListener(event, listener);
    };
}
//# sourceMappingURL=watchUnitEvent.js.map