"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.watchComponentRemoveEvent = exports.watchComponentAppendEvent = exports.watchElementCallEvent = exports.watchStatefulSetEvent = exports.watchUnitRenamePinEvent = exports.watchUnitEvent = void 0;
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);
    };
}
exports.watchUnitEvent = watchUnitEvent;
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);
    };
}
exports.watchUnitRenamePinEvent = watchUnitRenamePinEvent;
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);
    };
}
exports.watchStatefulSetEvent = watchStatefulSetEvent;
function watchElementCallEvent(event, unit, callback) {
    const listener = (data) => {
        callback({
            type: 'unit',
            event,
            data,
        });
    };
    unit.addListener(event, listener);
    return () => {
        unit.removeListener(event, listener);
    };
}
exports.watchElementCallEvent = watchElementCallEvent;
function watchComponentAppendEvent(event, unit, callback) {
    const listener = (bundle, path) => {
        if (path.length > 0) {
            return;
        }
        callback({
            type: 'unit',
            event,
            data: bundle,
            path,
        });
    };
    unit.addListener(event, listener);
    return () => {
        unit.removeListener(event, listener);
    };
}
exports.watchComponentAppendEvent = watchComponentAppendEvent;
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);
    };
}
exports.watchComponentRemoveEvent = watchComponentRemoveEvent;
//# sourceMappingURL=watchUnitEvent.js.map