"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.remoteRef = remoteRef;
exports.makeRemoteUnitAPI = makeRemoteUnitAPI;
const wrapper_1 = require("../types/interface/async/wrapper");
const method_1 = require("./method");
function remoteRef(ref) {
    let _ref = {};
    for (const name in ref) {
        const method = ref[name];
        const _method = (data) => {
            const { _ } = data;
            const $unit = method(data);
            const remoteApi = {
                get: {},
                call: {},
                watch: {},
                ref: {},
            };
            for (const __ of _) {
                const methods = method_1.METHOD[__];
                for (const type in methods) {
                    const methodList = methods[type];
                    for (const methodName of methodList) {
                        const $methodName = `$${methodName}`;
                        if (type === 'ref') {
                            remoteApi[type][$methodName] = (...args) => {
                                let result = $unit[$methodName](...args);
                                result = makeRemoteUnitAPI(result, result.__);
                                return result;
                            };
                        }
                        else {
                            remoteApi[type][$methodName] = $unit[$methodName];
                        }
                    }
                }
            }
            return remoteApi;
        };
        _ref[name] = _method;
    }
    return _ref;
}
function makeRemoteUnitAPI(unit, _) {
    const api = {
        get: {},
        call: {},
        watch: {},
        ref: {},
    };
    for (const __ of _) {
        const method = method_1.METHOD[__];
        const async_ = wrapper_1.ASYNC[__];
        const $unit = async_(unit);
        for (const type of method_1.METHOD_TYPES) {
            for (const name of method[type]) {
                const $name = `$${name}`;
                api[type] = { ...api[type], [$name]: $unit[$name] };
            }
        }
    }
    api.ref = remoteRef(api.ref);
    return api;
}
//# sourceMappingURL=makeRemoteUnitAPI.js.map