<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" xmlns:editor="http://vscxml.com/editor" version="1.0" datamodel="ecmascript" initial="workflow" name="DoneEventsDemo">
  <datamodel>
    <data id="totalSteps" expr="0"/>
    <data id="results" expr="[]"/>
  </datamodel>
  <state id="workflow" initial="phase1" editor:x="50" editor:y="48" editor:width="668" editor:height="762" editor:label="workflow" editor:color="#e0f2fe" editor:zIndex="1202">
    <onentry>
      <log label="Workflow" expr="'Starting multi-phase workflow'"/>
      <assign location="results" expr="[]"/>
    </onentry>
    <transition target="phase2" event="done.state.phase1" editor:id="t_01KEFSXTB5AF9HF363ETSWY92G">
      <script><![CDATA[results.push({
                    phase: _event.data.phase,
                    items: _event.data.itemsCollected
                });
                totalSteps = totalSteps + _event.data.itemsCollected;]]></script>
      <log label="Workflow" expr="'Phase 1 done! Data: ' + JSON.stringify(_event.data)"/>
    </transition>
    <transition target="phase3" event="done.state.phase2" editor:id="t_01KEFSXTB5R1E6SNNPEKEG8X0E">
      <script><![CDATA[results.push({
                    phase: _event.data.phase,
                    valid: _event.data.valid
                });
                totalSteps = totalSteps + _event.data.checksPerformed;]]></script>
      <log label="Workflow" expr="'Phase 2 done! Data: ' + JSON.stringify(_event.data)"/>
    </transition>
    <transition target="complete" event="done.state.phase3" editor:waypoints="888,228" editor:id="t_01KEFSXTB54XG02Z6PP745G65X">
      <script><![CDATA[results.push({
                    phase: _event.data.phase,
                    success: _event.data.success
                });
                totalSteps = totalSteps + 2;]]></script>
      <log label="Workflow" expr="'Phase 3 done! Data: ' + JSON.stringify(_event.data)"/>
    </transition>
    <transition target="cancelled" event="cancel" editor:waypoints="888,412" editor:id="t_01KEFSXTB5XJ7XFGWJ4JP3243D"/>
    <state id="phase1" initial="collect_name" editor:x="30" editor:y="33" editor:width="601" editor:height="200" editor:label="phase1" editor:color="#e0f2fe" editor:zIndex="1203">
      <onentry>
        <log label="Phase1" expr="'Phase 1: Data Collection'"/>
      </onentry>
      <state id="collect_name" editor:x="30" editor:y="30" editor:width="120" editor:height="80" editor:label="collect_name" editor:color="#ffffff" editor:zIndex="1204">
        <onentry>
          <log label="Step" expr="'Collecting name...'"/>
          <send event="name_collected" delay="500ms"/>
        </onentry>
        <transition target="collect_email" event="name_collected" editor:id="t_01KEFSXTB5H0V1DCGM6QTB47N4"/>
      </state>
      <state id="collect_email" editor:x="249.5" editor:y="30" editor:width="120" editor:height="80" editor:label="collect_email" editor:color="#ffffff" editor:zIndex="1205">
        <onentry>
          <log label="Step" expr="'Collecting email...'"/>
          <send event="email_collected" delay="500ms"/>
        </onentry>
        <transition target="phase1_done" event="email_collected" editor:id="t_01KEFSXTB5H9XH7EFAXJWVTZR3"/>
      </state>
      <final id="phase1_done" editor:x="469" editor:y="30" editor:width="120" editor:height="80" editor:label="phase1_done" editor:color="#ffffff" editor:zIndex="1206">
        <onentry>
          <log label="Phase1" expr="'Phase 1 complete!'"/>
        </onentry>
        <donedata>
          <param name="phase" expr="&apos;collection&apos;"/>
          <param name="itemsCollected" expr="2"/>
        </donedata>
      </final>
    </state>
    <state id="phase2" initial="validate_format" editor:x="30" editor:y="281" editor:width="601" editor:height="200" editor:label="phase2" editor:color="#e0f2fe" editor:zIndex="1207">
      <onentry>
        <log label="Phase2" expr="'Phase 2: Validation'"/>
      </onentry>
      <state id="validate_format" editor:x="30" editor:y="30" editor:width="120" editor:height="80" editor:label="validate_format" editor:color="#ffffff" editor:zIndex="1208">
        <onentry>
          <log label="Step" expr="'Validating format...'"/>
          <send event="format_ok" delay="300ms"/>
        </onentry>
        <transition target="validate_content" event="format_ok" editor:id="t_01KEFSXTB5TWM2PT6G3QC48H74"/>
      </state>
      <state id="validate_content" editor:x="249.5" editor:y="30" editor:width="120" editor:height="80" editor:label="validate_content" editor:color="#ffffff" editor:zIndex="1209">
        <onentry>
          <log label="Step" expr="'Validating content...'"/>
          <send event="content_ok" delay="300ms"/>
        </onentry>
        <transition target="phase2_done" event="content_ok" editor:id="t_01KEFSXTB5RBDPQV1JAK5FDV9C"/>
      </state>
      <final id="phase2_done" editor:x="469" editor:y="30" editor:width="120" editor:height="80" editor:label="phase2_done" editor:color="#ffffff" editor:zIndex="1210">
        <onentry>
          <log label="Phase2" expr="'Phase 2 complete!'"/>
        </onentry>
        <donedata>
          <param name="phase" expr="&apos;validation&apos;"/>
          <param name="checksPerformed" expr="2"/>
          <param name="valid" expr="true"/>
        </donedata>
      </final>
    </state>
    <state id="phase3" initial="prepare" editor:x="30" editor:y="532" editor:width="601" editor:height="200" editor:label="phase3" editor:color="#e0f2fe" editor:zIndex="1211">
      <onentry>
        <log label="Phase3" expr="'Phase 3: Submission'"/>
      </onentry>
      <state id="prepare" editor:x="30" editor:y="30" editor:width="120" editor:height="80" editor:label="prepare" editor:color="#ffffff" editor:zIndex="1212">
        <onentry>
          <log label="Step" expr="'Preparing submission...'"/>
          <send event="prepared" delay="200ms"/>
        </onentry>
        <transition target="submit" event="prepared" editor:id="t_01KEFSXTB5DFNQ0G31C6HNDYT9"/>
      </state>
      <state id="submit" editor:x="244" editor:y="30" editor:width="120" editor:height="80" editor:label="submit" editor:color="#ffffff" editor:zIndex="1214">
        <onentry>
          <log label="Step" expr="'Submitting...'"/>
          <send event="submitted" delay="500ms"/>
        </onentry>
        <transition target="phase3_done" event="submitted" editor:id="t_01KEFSXTB5WV38J1NB3M8C82XJ"/>
      </state>
      <final id="phase3_done" editor:x="469" editor:y="30" editor:width="120" editor:height="80" editor:label="phase3_done" editor:color="#ffffff" editor:zIndex="1213">
        <donedata>
          <param name="phase" expr="&apos;submission&apos;"/>
          <param name="success" expr="true"/>
          <param name="timestamp" expr="Date.now()"/>
        </donedata>
      </final>
    </state>
  </state>
  <state id="complete" editor:x="828" editor:y="50" editor:width="120" editor:height="80" editor:label="complete" editor:color="#ffffff" editor:zIndex="43">
    <onentry>
      <log label="Workflow" expr="'=== WORKFLOW COMPLETE ==='"/>
      <log label="Summary" expr="'Total steps: ' + totalSteps"/>
      <log label="Results" expr="JSON.stringify(results, null, 2)"/>
    </onentry>
    <transition target="workflow" event="restart" editor:id="t_01KEFSXTB5K8Z4ZGSA65AZJ9ZS"/>
  </state>
  <state id="cancelled" editor:x="828" editor:y="271" editor:width="120" editor:height="80" editor:label="cancelled" editor:color="#ffffff" editor:zIndex="1049">
    <onentry>
      <log label="Workflow" expr="'Workflow cancelled'"/>
    </onentry>
    <transition target="workflow" event="restart" editor:id="t_01KEFSXTB5NHH4HAG1D3K937J2"/>
  </state>
  <editor:events>
    <editor:event name="cancel" visibility="public"/>
    <editor:event name="content_ok" visibility="internal"/>
    <editor:event name="done.state.phase1" visibility="internal"/>
    <editor:event name="done.state.phase2" visibility="internal"/>
    <editor:event name="done.state.phase3" visibility="internal"/>
    <editor:event name="email_collected" visibility="internal"/>
    <editor:event name="format_ok" visibility="internal"/>
    <editor:event name="name_collected" visibility="internal"/>
    <editor:event name="prepared" visibility="internal"/>
    <editor:event name="restart" visibility="public"/>
    <editor:event name="submitted" visibility="internal"/>
  </editor:events>
</scxml>