"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.computeLeafColor = computeLeafColor;
exports.findSelectedNode = findSelectedNode;
exports.computeSamplesCount = void 0;

var _lodash = _interopRequireDefault(require("lodash"));

var _polished = require("polished");

var _constants = require("./constants");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function computeLeafColor(confidence) {
  if (confidence != null) {
    var blend = Math.pow(confidence, 3);
    return (0, _polished.mix)(blend, _constants.COLOR_LEAVES_CONFIDENCE_1, _constants.COLOR_LEAVES_CONFIDENCE_0);
  }

  return _constants.COLOR_LEAVES_CONFIDENCE_UNDEFINED;
}

function findSelectedNode(path, tree) {
  var pathArray = path.split(_constants.NODE_PATH_SEPARATOR); // making the root node an exception

  if (path == '0') {
    return tree;
  } else {
    // remove the first element of the path because it is the root path;
    return findSelectedNodeRecursion(_lodash.default.tail(pathArray), tree, {});
  }
}

function findSelectedNodeRecursion(path, tree, decisionRules) {
  // adding decision rules of the node
  if (tree.decision_rule) {
    if (decisionRules[tree.decision_rule.property]) {
      decisionRules[tree.decision_rule.property].push({
        operator: tree.decision_rule.operator,
        operand: tree.decision_rule.operand
      });
    } else {
      decisionRules[tree.decision_rule.property] = [{
        operator: tree.decision_rule.operator,
        operand: tree.decision_rule.operand
      }];
    }
  }

  if (path.length !== 0) {
    var indexChild = parseInt(path[0]);
    return findSelectedNodeRecursion(_lodash.default.tail(path), tree.children[indexChild], decisionRules);
  } else {
    tree.decisionRules = decisionRules;
    return tree;
  }
}

var computeSamplesCount = _lodash.default.memoize(function (dtNode) {
  var selfCount = dtNode.prediction && dtNode.prediction.nb_samples || 0;
  var children = dtNode.children || [];
  return children.reduce(function (count, child) {
    return count + computeSamplesCount(child);
  }, selfCount);
});

exports.computeSamplesCount = computeSamplesCount;