"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncJ = exports.AsyncJRef = exports.AsyncJWatch = exports.AsyncJCall = exports.AsyncJGet = void 0;
const _1 = require("../../../Class/$");
const evaluate_1 = require("../../../spec/evaluate");
const Async_1 = require("./Async");
const AsyncJGet = (value) => {
    return {
        $get({ name }, callback) {
            let data;
            try {
                data = value.get(name);
            }
            catch (err) {
                callback(undefined, err.message);
            }
            callback(data);
        },
    };
};
exports.AsyncJGet = AsyncJGet;
const AsyncJCall = (value) => {
    return {
        $set({ name, data }, callback) {
            try {
                const _data = (0, evaluate_1.evaluate)(data, {}, {});
                value.set(name, _data);
            }
            catch (err) {
                callback(undefined, err.message);
            }
            callback();
        },
    };
};
exports.AsyncJCall = AsyncJCall;
const AsyncJWatch = (value) => {
    return {};
};
exports.AsyncJWatch = AsyncJWatch;
const AsyncJRef = (value) => {
    return {
        $ref({ name }) {
            let obj;
            try {
                obj = value.get(name);
            }
            catch (err) {
                return null;
            }
            if (obj === undefined) {
                return null;
            }
            if (obj instanceof _1.$) {
                const $obj = (0, Async_1.Async)(obj, obj.__, obj.__system.async);
                return $obj;
            }
            else {
                return null;
            }
        },
    };
};
exports.AsyncJRef = AsyncJRef;
const AsyncJ = (value) => {
    return {
        ...(0, exports.AsyncJGet)(value),
        ...(0, exports.AsyncJCall)(value),
        ...(0, exports.AsyncJWatch)(value),
        ...(0, exports.AsyncJRef)(value),
    };
};
exports.AsyncJ = AsyncJ;
//# sourceMappingURL=AsyncJ.js.map