// Copyright (c) 2023 Dominic Masters
// 
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#include "VNSceneItemParser.hpp"

using namespace Dawn;

std::vector<std::string> VNSceneItemParser::getRequiredAttributes() {
  return { "prefab" };
}

std::map<std::string, std::string> VNSceneItemParser::getOptionalAttributes() {
  return {
    { "ref", "" }
  };
}

int32_t VNSceneItemParser::onParse(
  Xml *node,
  std::map<std::string, std::string> values,
  struct VNSceneItem *out,
  std::string *error
) {
  out->ref = values["ref"];
  out->prefab = values["prefab"];
  
  // Split prefab by / and use the last part as the class name
  std::string clazz = out->prefab;
  size_t pos = clazz.find_last_of('/');
  if(pos != std::string::npos) clazz = clazz.substr(pos+1);
  out->className = clazz;

  // Make sure prefab and className is not empty
  if(out->prefab.empty()) {
    *error = "Prefab cannot be empty.";
    return -1;
  }

  if(out->className.empty()) {
    *error = "Class name cannot be empty.";
    return -1;
  }

  return 0;
}