"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.proxyWrap = exports.proxy = void 0;
const ASYNC_GRAPH_CALL_EVENT_MAP_1 = require("./constant/ASYNC_GRAPH_CALL_EVENT_MAP");
const ASYNC_GRAPH_WATCH_SET_1 = require("./constant/ASYNC_GRAPH_WATCH_SET");
function proxy(unit, CALL, WATCH) {
    let stop_event = undefined;
    const proxy = new Proxy(unit, {
        get: (target, name) => {
            const value = target[name];
            if (CALL[name]) {
                stop_event = CALL[name];
            }
            else if (WATCH.has(name)) {
                return (data, callback) => {
                    return value.call(target, data, (moment) => {
                        var _a, _b, _c;
                        const { event } = moment;
                        if (stop_event === event &&
                            ((_c = (_b = (_a = moment.data) === null || _a === void 0 ? void 0 : _a.path) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) === 0) {
                            stop_event = undefined;
                        }
                        else {
                            callback(moment);
                        }
                    });
                };
            }
            return value;
        },
    });
    return proxy;
}
exports.proxy = proxy;
const ASYNC_INTERFACE_PROXY_CALL_FILTER = {
    V: { $write: 'write' },
    J: { $set: 'set' },
    CA: { $draw: 'draw' },
    G: ASYNC_GRAPH_CALL_EVENT_MAP_1.ASYNC_GRAPH_CALL_EVENT_MAP,
    // TODO
    B: {},
    IB: {},
    CC: {},
    C: {},
    CS: {},
    CO: {},
    GP: {},
    M: {},
    E: {},
    EE: {},
    BD: {},
    BS: {},
    BSE: {},
    BC: {},
    NO: {},
    A: {},
    PP: {},
    PS: {},
    CH: {},
    MS: {},
    S: {},
    U: {},
    TR: {},
    RE: {},
    F: {},
    L: {},
    AC: {},
    D: {},
};
const ASYNC_INTERFACE_PROXY_WATCH_FILTER = {
    G: ASYNC_GRAPH_WATCH_SET_1.ASYNC_GRAPH_WATCH_SET,
};
function proxyWrap(unit, _ = []) {
    let CALL = {};
    let WATCH = new Set();
    for (const __ of _) {
        const CALL_FILTER = ASYNC_INTERFACE_PROXY_CALL_FILTER[__] || {};
        const WATCH_FILTER = ASYNC_INTERFACE_PROXY_WATCH_FILTER[__] || new Set();
        CALL = Object.assign(Object.assign({}, CALL), CALL_FILTER);
        WATCH = new Set([...WATCH, ...WATCH_FILTER]);
    }
    return proxy(unit, CALL, WATCH);
}
exports.proxyWrap = proxyWrap;
//# sourceMappingURL=proxyWrap.js.map