"use strict";

// src/__fixtures__/DuplicateIdMachine.ts
var simpleMachine = [
  {
    "id": "sendSlackMessage",
    "transitions": [
      {
        "on": "CONTINUE",
        "target": "sendSlackMessage"
      },
      {
        "on": "ERROR",
        "target": "failure"
      }
    ]
  },
  {
    "id": "sendSlackMessage",
    "transitions": [
      {
        "on": "CONTINUE",
        "target": "sendSlackMessage"
      },
      {
        "on": "ERROR",
        "target": "failure"
      }
    ]
  },
  {
    "id": "sendSlackMessage",
    "transitions": [
      {
        "on": "CONTINUE",
        "target": "success"
      },
      {
        "on": "ERROR",
        "target": "failure"
      }
    ]
  },
  {
    "id": "success",
    "type": "final"
  },
  {
    "id": "failure",
    "type": "final"
  }
];
var complexMachine = [
  {
    "id": "sendSlackMessage",
    "transitions": [
      {
        "on": "CONTINUE",
        "target": "sendSlackMessage"
      },
      {
        "on": "ERROR",
        "target": "failure"
      }
    ]
  },
  {
    "id": "sendSlackMessage",
    "transitions": [
      {
        "on": "CONTINUE",
        "target": "parallelChecks"
      },
      {
        "on": "ERROR",
        "target": "failure"
      }
    ]
  },
  {
    id: "parallelChecks",
    type: "parallel",
    states: [
      {
        id: "RegulatoryCheck",
        transitions: [
          { on: "CONTINUE", target: "success" },
          { on: "ERROR", target: "failure" }
        ]
      },
      {
        id: "ConcentrationEstimation",
        transitions: [
          { on: "CONTINUE", target: "success" },
          { on: "ERROR", target: "failure" }
        ]
      }
    ],
    onDone: "sendSlackMessage"
  },
  {
    "id": "sendSlackMessage",
    "transitions": [
      {
        "on": "CONTINUE",
        "target": "success"
      },
      {
        "on": "ERROR",
        "target": "failure"
      }
    ]
  },
  {
    "id": "success",
    "type": "final"
  },
  {
    "id": "failure",
    "type": "final"
  }
];

// src/utils/uuid.ts
function uuidv4() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
    const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
    return v.toString(16);
  });
}

// src/utils/StateMachines.ts
function getUniqueStateIds(inputArray, parent) {
  let statesArray = [...inputArray];
  if (statesArray[0]?.id.indexOf("|") >= 0) {
    return statesArray;
  }
  statesArray = statesArray.map((state, index, original) => {
    switch (state.id) {
      // these states only hve one occurance in all machines
      case "success":
      case "failure":
      case "pause":
        break;
      default:
        state.id = `${state.id}|${uuidv4()}`;
        if (parent) {
          state.parentId = parent.id;
        }
        break;
    }
    return state;
  });
  statesArray = statesArray.map((state, index) => {
    switch (state.id) {
      // these states only hve one occurance in all machines
      case "success":
      case "failure":
      case "pause":
        break;
      default:
        const futureStates = statesArray.slice(index + 1);
        state.transitions = state.transitions?.map((stateTranistion) => {
          const targetState = futureStates.find((futureState) => futureState.id.indexOf(stateTranistion.target) >= 0);
          stateTranistion.target = targetState ? targetState.id : stateTranistion.target;
          return stateTranistion;
        });
        if (state.onDone) {
          if (state.onDone instanceof Array) {
            state.onDone = state.onDone[0].target;
          }
          if (state.onDone instanceof Object) {
            state.onDone = state.onDone.target;
          }
          const targetState = futureStates.find((futureState) => futureState.id.indexOf(state.onDone) >= 0);
          state.onDone = targetState ? targetState.id : state.onDone;
        }
        if (state.states) {
          getUniqueStateIds(state.states, state);
        }
        break;
    }
    return state;
  });
  return statesArray;
}

// src/test/StateMachines.test.ts
var counter = 0;
jest.mock("../utils", () => ({
  ...jest.requireActual("../utils"),
  uuidv4: jest.fn(() => (++counter).toString())
}));
afterAll(() => {
  jest.clearAllMocks();
});
beforeEach(() => counter = 0);
describe("Testing the getUniqueStateIds function", () => {
  test("Testing Deuplication of a state machines without parellel states", async () => {
    const inputStates = simpleMachine;
    const deduplicatedStates = getUniqueStateIds(inputStates);
    const serializedResults = JSON.stringify(deduplicatedStates);
    expect(serializedResults).toBe('[{"id":"sendSlackMessage|1","transitions":[{"on":"CONTINUE","target":"sendSlackMessage|2"},{"on":"ERROR","target":"failure"}]},{"id":"sendSlackMessage|2","transitions":[{"on":"CONTINUE","target":"sendSlackMessage|3"},{"on":"ERROR","target":"failure"}]},{"id":"sendSlackMessage|3","transitions":[{"on":"CONTINUE","target":"success"},{"on":"ERROR","target":"failure"}]},{"id":"success","type":"final"},{"id":"failure","type":"final"}]');
  });
  test("Testing Deuplication of a state machines with parellel states", async () => {
    const inputStates = complexMachine;
    const deduplicatedStates = getUniqueStateIds(inputStates);
    const serializedResults = JSON.stringify(deduplicatedStates);
    expect(serializedResults).toBe('[{"id":"sendSlackMessage|1","transitions":[{"on":"CONTINUE","target":"sendSlackMessage|2"},{"on":"ERROR","target":"failure"}]},{"id":"sendSlackMessage|2","transitions":[{"on":"CONTINUE","target":"parallelChecks|3"},{"on":"ERROR","target":"failure"}]},{"id":"parallelChecks|3","type":"parallel","states":[{"id":"RegulatoryCheck|5","transitions":[{"on":"CONTINUE","target":"success"},{"on":"ERROR","target":"failure"}],"parentId":"parallelChecks|3"},{"id":"ConcentrationEstimation|6","transitions":[{"on":"CONTINUE","target":"success"},{"on":"ERROR","target":"failure"}],"parentId":"parallelChecks|3"}],"onDone":"sendSlackMessage|4"},{"id":"sendSlackMessage|4","transitions":[{"on":"CONTINUE","target":"success"},{"on":"ERROR","target":"failure"}]},{"id":"success","type":"final"},{"id":"failure","type":"final"}]');
  });
});
