{# SPDX-License-Identifier: MIT #}
{# Copyright © 2023 André Santos #}

{% import 'js/common.js.jinja' as G %}

{##############################################################################}
{# TIMER MACROS #}
{##############################################################################}


{# assume: local('timestamp') #}
{% macro _on_timer(sm) -%}
if (this._state === {{ G.STATE_ACTIVE }} && (timestamp - this.timeState) >= {{ sm.timeout }}) {
  {% if sm.reentrant_scope %}
{{ G.change_to_state(G.STATE_SAFE, returns=false)|indent(2, first=true) }}
  {%- else %}
{{ G.change_to_state(G.STATE_TRUE, returns=false)|indent(2, first=true) }}
  {%- endif %}
}
{%- endmacro %}


{##############################################################################}
{# MSG EVENT MACROS #}
{##############################################################################}


{# assume: local('timestamp') #}
{# assume: local('msg') #}
{% macro _on_msg(sm, event, topic) -%}
{% if event.event_type == G.EVENT_ACTIVATOR %}
{# after or after-until -#}
{{ G.activator_event(event, topic) }}
{%- elif event.event_type == G.EVENT_TERMINATOR %}
{# until or after-until -#}
{{ G.terminator_event(sm, event, topic) }}
{%- elif event.event_type == G.EVENT_BEHAVIOUR %}
{{ _behaviour(event, topic) }}
{%- else %}
assert(false, "unexpected event type: {{ event.event_type }}");
{%- endif %}
{%- endmacro %}


{# assume: local('timestamp') #}
{# assume: local('msg') #}
{% macro _behaviour(event, topic) -%}
  {% if event.activator %}
assert(this.witness.length >= 1, "missing activator");
const v_{{ event.activator }} = this.witness[0].msg;
  {% endif %}
  {% call G.change_to_state_if(event.predicate, G.STATE_FALSE) %}
this.witness.push(MsgRecord("{{ topic }}", timestamp, msg));
  {%- endcall %}
{%- endmacro %}


{##############################################################################}
{# RENDERED CODE #}
{##############################################################################}

{# assume: global('state_machine') #}
{% call(cb) G.state_machine(state_machine) -%}
  {% if cb == G.CALLBACK_TIMER -%}
{{ _on_timer(state_machine) }}
  {%- elif cb == G.CALLBACK_MSG -%}
{{ _on_msg(state_machine, varargs[0], varargs[1]) }}
  {%- else -%}
assert(false, "unexpected macro function type: {{ args }}");
  {%- endif %}
{%- endcall %}
