class Metanorma::Plugin::Lutaml::LutamlUmlAttributesTablePreprocessor

@example [lutaml_uml_attributes_table,path/to/lutaml,EntityName]
Macro for quick rendering of datamodel attributes/values table

def lutaml_document_from_file(document, file_path)

def lutaml_document_from_file(document, file_path)
  ::Lutaml::Parser
    .parse(File.new(Utils.relative_file_path(document, file_path),
                    encoding: "UTF-8"))
    .first
end

def model_representation(entity_definition, document, skip_headers)

def model_representation(entity_definition, document, skip_headers)
  render_result, errors = Utils.render_liquid_string(
    template_string: table_template(skip_headers),
    context_items: entity_definition,
    context_name: "definition",
    document: document
  )
  Utils.notify_render_errors(document, errors)
  render_result.split("\n")
end

def parse_marco(lutaml_path, entity_name, document, skip_headers)

def parse_marco(lutaml_path, entity_name, document, skip_headers)
  lutaml_document = lutaml_document_from_file(document, lutaml_path)
    .serialized_document
  entities = [lutaml_document["classes"], lutaml_document["enums"]]
    .compact
    .flatten
  entity_definition = entities.detect do |klass|
    klass["name"] == entity_name.strip
  end
  model_representation(entity_definition, document, skip_headers)
end

def process(document, reader)

into yaml2text blocks
read include derectives that goes after that in block and transform
search document for block `datamodel_attributes_table`
def process(document, reader)
  input_lines = reader.readlines.to_enum
  Asciidoctor::Reader.new(processed_lines(document, input_lines))
end

def processed_lines(document, input_lines)

def processed_lines(document, input_lines)
  input_lines.each_with_object([]) do |line, result|
    if match = line.match(MARCO_REGEXP)
      lutaml_path = match[1]
      entity_name = match[2]
      skip_headers = match[3]
      result.push(*parse_marco(lutaml_path, entity_name, document, skip_headers))
    else
      result.push(line)
    end
  end
end

def table_template(skip_headers)

rubocop:disable Layout/IndentHeredoc
def table_template(skip_headers)
  <<~TEMPLATE
  #{"=== {{ definition.name }}" unless skip_headers}
  {{ definition.definition }}
  {% if definition.attributes %}
  {% if definition.keyword == 'enumeration' %}
  .{{ definition.name }} values
  |===
  |Name |Definition
  {% for item in definition.attributes %}
  |{{ item.name }} |{{ item.definition }}
  {% endfor %}
  |===
  {% else %}
  .{{ definition.name }} attributes
  |===
  |Name |Definition |Mandatory / Optional / Conditional |Max Occur |Data Type
  {% for item in definition.attributes %}
  |{{ item.name }} |{% if item.definition %}{{ item.definition }}{% endif %} |{% if item.cardinality.min == "0" %}O{% else %}M{% endif %} |{% if item.cardinality.max == "*" %}N{% else %}1{% endif %} |{% if item.origin %}<<{{ item.origin }}>>{% endif %} `{{ item.type }}`
  {% endfor %}
  |===
  {% endif %}
  {% endif %}
  TEMPLATE
end