"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncU = exports.AsyncURef = exports.AsyncUWatch = exports.AsyncUCall = exports.AsyncUGet = 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 stringify_1 = require("../../../spec/stringify");
const clone_1 = require("../../../util/clone");
const object_1 = require("../../../util/object");
const stringifyPinData_1 = require("../../stringifyPinData");
const Async_1 = require("./Async");
const AsyncUGet = (unit) => {
    return {
        $getGlobalId(data, callback) {
            const __globalId = unit.getGlobalId();
            callback(__globalId);
        },
        $paused(data, callback) {
            const paused = unit.paused();
            callback(paused);
        },
        $reset(data) {
            unit.reset();
        },
        $getPinData({ type, pinId }, callback) {
            const _data = unit.getPinData(type, pinId);
            const __data = (0, stringify_1.stringify)(_data);
            callback(__data);
        },
        $getAllPinData(data, callback) {
            const _data = unit.getPinsData();
            const __data = (0, stringifyPinData_1.stringifyPinData)(_data);
            callback(__data);
        },
        $getAllInputData(data, callback) {
            const _data = unit.getInputData();
            callback(_data);
        },
        $getAllRefInputData(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, clone_1.clone)(unitBundleSpec);
            callback($unitBundleSpec);
        },
    };
};
exports.AsyncUGet = AsyncUGet;
const AsyncUCall = (unit) => {
    return {
        $play(data) {
            unit.play();
        },
        $pause(data) {
            unit.pause();
        },
        $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);
        },
        $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, global_1.getGlobalRef)(system, globalId);
            const $obj = (0, Async_1.Async)($, __ !== null && __ !== void 0 ? __ : $.__, system.async);
            return (0, proxyWrap_1.proxyWrap)($obj, $.__);
        },
    };
};
exports.AsyncURef = AsyncURef;
const AsyncU = (unit) => {
    return {
        ...(0, exports.AsyncUGet)(unit),
        ...(0, exports.AsyncUCall)(unit),
        ...(0, exports.AsyncUWatch)(unit),
        ...(0, exports.AsyncURef)(unit),
    };
};
exports.AsyncU = AsyncU;
//# sourceMappingURL=AsyncU.js.map