{% set assign=true %}
{% set signal=true %}
{% set resolve=true %}
{% set result=true %}

{% import "src/default/types/block.js.njk" as block with context %}

{% call block.header() %}
  {% include "src/default/macros/block-library-header.js.njk" %}
{% endcall %}

{% call block.definition() %}

  {# NEW: Check if 'new' keyword is present for constructor call #}
  {% if context.transform.new !== undefined %}
    {# Constructor instantiation with 'new' keyword #}
    {% if context.lib.type==='atom' and context.lib.atom.protocol!=='use:' %}
      const LibClass=LIBRARY;
    {% elseif context.lib.atom.protocol==='use:' %}
      {# For use:e:: protocol, call value is the class name (e.g., use:e::Map → Map) #}
      const LibClass={{context.transform.import}};
      if(!LibClass) throw new Error('[use] Couldnt find class.');
    {% else %}
      throw new Error('Cannot instantiate: unsupported lib type.');
    {% endif %}

    {# Create instance with constructor args #}
    {% if context.transform.new %}
      const constructorArgs = {{ context.transform.new | safe }};
      const instance = Array.isArray(constructorArgs) ? new LibClass(...constructorArgs) : new LibClass(constructorArgs);
    {% else %}
      const instance = new LibClass();
    {% endif %}

  {% endif %}

  const result = instance;
  
{% endcall %}

{% call block.footer()%} 
{% endcall %}