"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncU = exports.AsyncURef = exports.AsyncUWatch = exports.AsyncUCall = exports.$$refGlobalObj = void 0;
const watchUnit_1 = require("../../../debug/watchUnit");
const global_1 = require("../../../global");
const proxyWrap_1 = require("../../../proxyWrap");
const evaluate_1 = require("../../../spec/evaluate");
const object_1 = require("../../../util/object");
const stringifyPinData_1 = require("../../stringifyPinData");
const Async_1 = require("./Async");
const $$refGlobalObj = (system, globalId) => {
    const $ = (0, global_1.getGlobalRef)(system, globalId);
    const $unit = (0, Async_1.Async)($, $.__);
    return $unit;
};
exports.$$refGlobalObj = $$refGlobalObj;
const AsyncUCall = (unit) => {
    return {
        $getGlobalId(data, callback) {
            const __globalId = unit.getGlobalId();
            callback(__globalId);
        },
        $play(data) {
            unit.play();
        },
        $pause(data) {
            unit.pause();
        },
        $paused(data, callback) {
            const paused = unit.paused();
            callback(paused);
        },
        $reset(data) {
            unit.reset();
        },
        $push({ pinId, data }) {
            const { classes, specs } = unit.__system;
            const _data = (0, evaluate_1.evaluate)(data, specs, classes);
            unit.push(pinId, _data);
        },
        $takeInput(data) {
            const { pinId } = data;
            unit.takeInput(pinId);
        },
        $setPinData({ type, pinId, data }) {
            const { classes, specs } = unit.__system;
            const _data = (0, evaluate_1.evaluate)(data, specs, classes);
            unit.setPinData(type, pinId, _data);
        },
        $removePinData(data) {
            const { type, pinId } = data;
            unit.removePinData(type, pinId);
        },
        $getPinData(data, callback) {
            const _data = unit.getPinsData();
            const __data = (0, stringifyPinData_1.stringifyPinData)(_data);
            callback(__data);
        },
        $getInputData(data, callback) {
            const _data = unit.getInputData();
            callback(_data);
        },
        $getRefInputData(data, callback) {
            const _data = unit.getRefInputData();
            const __data = (0, object_1.mapObjVK)(_data, (unit) => {
                const __ = unit.getInterface();
                const globalId = unit.getGlobalId();
                return { globalId, __ };
            });
            callback(__data);
        },
        $getUnitBundleSpec(data, callback) {
            const unitBundleSpec = unit.getUnitBundleSpec(data.deep);
            const $unitBundleSpec = (0, object_1.clone)(unitBundleSpec);
            callback($unitBundleSpec);
        },
        $pullInput(data) {
            const { pinId } = data;
            const input = unit.getInput(pinId);
            input.pull();
        },
    };
};
exports.AsyncUCall = AsyncUCall;
const AsyncUWatch = (unit) => {
    return {
        $watch({ events }, callback) {
            return (0, watchUnit_1.watchUnit)(unit, callback, events);
        },
    };
};
exports.AsyncUWatch = AsyncUWatch;
const AsyncURef = (unit) => {
    return {
        $refGlobalObj(data) {
            const __system = unit.refSystem();
            const { globalId } = data;
            const $ = (0, exports.$$refGlobalObj)(__system, globalId);
            return (0, proxyWrap_1.proxyWrap)($, $.__);
        },
    };
};
exports.AsyncURef = AsyncURef;
const AsyncU = (unit) => {
    return Object.assign(Object.assign(Object.assign({}, (0, exports.AsyncUCall)(unit)), (0, exports.AsyncUWatch)(unit)), (0, exports.AsyncURef)(unit));
};
exports.AsyncU = AsyncU;
//# sourceMappingURL=AsyncU.js.map