"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.evaluateDataValue = evaluateDataValue;
exports.evaluateData = evaluateData;
const Unit_1 = require("../Class/Unit");
const evaluate_1 = require("./evaluate");
const parser_1 = require("./parser");
const primitive_1 = require("./primitive");
function evaluateDataValue(value, specs, classes) {
    const t = typeof value;
    let ref = [];
    let data;
    if (t === 'string') {
        const tree = (0, parser_1.getTree)(value, false, false);
        const [ref, replacedTree] = (0, parser_1.findAndReplaceUnitNodes_)(tree);
        data = (0, evaluate_1._evaluate)(replacedTree, specs, classes);
        return { ref, data };
    }
    else if (t === 'object') {
        return value;
    }
    return { ref, data };
}
function evaluateData(data, specs, classes, path = []) {
    let ref = [];
    if ((0, primitive_1.isBundle)(data)) {
        ref.push([]);
        data = data.__bundle;
        delete data.unit.memory;
    }
    else if ((0, primitive_1.isPrimitive)(data)) {
        //
    }
    else if (data.constructor.name === 'Array' ||
        data.constructor.name === 'Object') {
        const data_ = Array.isArray(data) ? [] : {};
        let ref_;
        for (const k in data) {
            const dataRef_ = evaluateData(data, specs, classes, [...path, k]);
            ref = ref.concat([path, ...dataRef_.ref]);
            data_[k] = dataRef_.data;
            ref.push(ref_);
        }
        data = data_;
    }
    else if (data instanceof Unit_1.Unit) {
        ref.push([]);
        // @ts-ignore
        data = data.constructor.__bundle;
        delete data.unit.memory;
    }
    return {
        ref,
        data,
    };
}
//# sourceMappingURL=evaluateDataValue.js.map