#pragma once

#include "xtr/io/OptionsIO.hpp"
#include "xtr/doc/FileTypes.h"

#include <string_view>

namespace xtr::io {

//! Export CstGraph
class CstGraphExporter {
  const ccl::semantic::RSCore& target;
  CstGraphOptions options{};

public:
  explicit CstGraphExporter(const ccl::semantic::RSCore& target) noexcept
    : target{ target } {}

public:
  [[nodiscard]] BOOL Export(const CstGraphOptions& exportParams, CString fileName, FileType type);

private:
  struct Triplet {
    std::string_view from;
    io::LinkType type;
    std::string_view to;
  };

  template<typename LinePutter>
  void OutputLinks(const io::LinkType linkType, LinePutter out) {
    for (const auto toID : target.List()) {
      switch (linkType) {
      case io::LinkType::formula: {
        for (const auto fromID : target.RSLang().Graph().InputsFor(toID)) {
          out(Triplet{ target.GetRS(fromID).alias, linkType, target.GetRS(toID).alias });
        }
        break;
      }
      case io::LinkType::lexTerm: {
        for (const auto fromID : target.Texts().TermGraph().InputsFor(toID)) {
          out(Triplet{ target.GetRS(fromID).alias, linkType, target.GetRS(toID).alias });
        }
        break;
      }
      case io::LinkType::lexDef: {
        for (const auto fromID : target.Texts().DefGraph().InputsFor(toID)) {
          out(Triplet{ target.GetRS(fromID).alias, linkType, target.GetRS(toID).alias });
        }
        break;
      }
      case io::LinkType::size_: break;
      }
    }
  }
};

} // namespace xtr::io