"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 evaluate_1 = require("../../../spec/evaluate");
const evaluateMemorySpec_1 = require("../../../spec/evaluate/evaluateMemorySpec");
const evaluateDataValue_1 = require("../../../spec/evaluateDataValue");
const resolveDataValue_1 = require("../../../spec/resolveDataValue");
const stringify_1 = require("../../../spec/stringify");
const stringifySpec_1 = require("../../../spec/stringifySpec");
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 {
        $getSpec(data, callback) {
            const spec = unit.getSpec();
            callback(spec);
        },
        $paused(data, callback) {
            const paused = unit.paused();
            callback(paused);
        },
        async $snapshot(data, callback) {
            const memory = unit.snapshot();
            (0, stringifySpec_1.stringifyMemorySpecData)(memory);
            callback(memory);
        },
        $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);
            (0, stringifySpec_1.stringifyUnitBundleSpecData)(unitBundleSpec);
            const $unitBundleSpec = (0, clone_1.clone)(unitBundleSpec);
            callback($unitBundleSpec);
        },
        $hasPin(data, callback) {
            const { type, pinId } = data;
            const has = unit.hasPinNamed(type, pinId);
            callback(has);
        },
    };
};
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);
        },
        $takeErr(data) {
            unit.takeErr();
        },
        $setPinData({ type, pinId, data, }) {
            const { classes, specs } = unit.__system;
            const dataRef = (0, evaluateDataValue_1.evaluateDataValue)(data, specs, classes);
            const _data = (0, resolveDataValue_1.resolveDataRef)(dataRef, 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();
        },
        $restore: function (data) {
            const { memory } = data;
            (0, evaluateMemorySpec_1.evaluateMemorySpec)(memory, unit.__system.specs, unit.__system.classes);
            unit.restore(memory);
        },
        $destroy: function (data) {
            unit.destroy();
        },
    };
};
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.__system;
            const { globalId, __ } = data;
            const $ = (0, global_1.getGlobalRef)(system, globalId);
            if (!$) {
                return null;
            }
            const $obj = (0, Async_1.Async)($, __, system.async);
            return $obj;
        },
        $refPin(data) {
            const { type, pinId } = data;
            const system = unit.__system;
            const pin = unit.getPin(type, pinId);
            const $pin = (0, Async_1.Async)(pin, ['V'], system.async);
            return $pin;
        },
    };
};
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