/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include "YGConfig.h"

YGConfig::YGConfig(YGLogger logger) : cloneNodeCallback_{nullptr} {
  logger_.noContext = logger;
  loggerUsesContext_ = false;
}

void YGConfig::log(YGConfig* config, YGNode* node, YGLogLevel logLevel,
                   void* logContext, const char* format, va_list args) {
  if (loggerUsesContext_) {
    logger_.withContext(config, node, logLevel, logContext, format, args);
  } else {
    logger_.noContext(config, node, logLevel, format, args);
  }
}

YGNodeRef YGConfig::cloneNode(YGNodeRef node, YGNodeRef owner, int childIndex,
                              void* cloneContext) {
  YGNodeRef clone = nullptr;
  if (cloneNodeCallback_.noContext != nullptr) {
    clone = cloneNodeUsesContext_
                ? cloneNodeCallback_.withContext(node, owner, childIndex,
                                                 cloneContext)
                : cloneNodeCallback_.noContext(node, owner, childIndex);
  }
  if (clone == nullptr) {
    clone = YGNodeClone(node);
  }
  return clone;
}
