"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var temen = require("balena-temen");
var cloneDeep = require("lodash/cloneDeep");
var get = require("lodash/get");
var isPlainObject = require("lodash/isPlainObject");
var merge = require("lodash/merge");
var set = require("lodash/set");
var getFormulaKeys = function (obj, prefix) {
    if (prefix === void 0) { prefix = ''; }
    var keys = Object.keys(obj);
    prefix = prefix ? prefix + '.' : '';
    return keys.reduce(function (result, key) {
        if (isPlainObject(obj[key])) {
            result = result.concat(getFormulaKeys(obj[key], prefix + key));
        }
        else if (key === '$$formula') {
            result.push(prefix + key);
        }
        return result;
    }, []);
};
var runFormulas = function (schema, value) {
    var data = cloneDeep(value);
    var keys = getFormulaKeys(schema).map(function (path) {
        return {
            formula: get(schema, path),
            path: path.replace(/properties\./g, ''),
        };
    });
    keys.forEach(function (y) {
        set(data, y.path, y.formula);
    });
    var evaluate;
    try {
        evaluate = temen.evaluate(data);
    }
    catch (e) {
        console.error('Caught error when evaluating formulas', e);
        evaluate = value;
    }
    var startingState = defaultValueForSchema(schema);
    return merge(startingState, value, evaluate);
};
exports.runFormulas = runFormulas;
var defaultValueForSchema = function (schema) {
    return schema.type === 'array' ? [] : {};
};
exports.defaultValueForSchema = defaultValueForSchema;
