import { AWSStepFunctionsHydratorManager, StateMachine } from "flowbject";

const manager = new AWSStepFunctionsHydratorManager();

export default manager.hydrateStateMachine(new StateMachine(), {
  StartAt: "getUser",
  Comment: "Create a user package",
  States: {
    getUser: {
      Resource: "user:get",
      ResultPath: "$.user",
      Next: "getUserPackage",
      Catch: [
        {
          ErrorEquals: ["States.ALL"],
          Next: "publishResults",
          ResultPath: "$.pub.error",
        },
      ],
      Type: "Task",
    },
    getUserPackage: {
      Resource: "package:get",
      ResultPath: "$.package",
      Next: "getScript",
      Catch: [
        {
          ErrorEquals: ["NotFoundError"],
          Next: "registerNamespace",
          ResultPath: null,
        },
        {
          ErrorEquals: ["States.ALL"],
          Next: "publishResults",
          ResultPath: "$.pub.error",
        },
      ],
      Type: "Task",
    },
    registerNamespace: {
      Resource: "package:registerNamespace",
      ResultPath: null,
      Next: "createPackage",
      Catch: [
        {
          ErrorEquals: ["States.ALL"],
          Next: "publishResults",
          ResultPath: "$.pub.error",
        },
      ],
      Type: "Task",
    },
    createPackage: {
      Resource: "package:create",
      ResultPath: "$.package",
      Next: "getScript",
      Catch: [
        {
          ErrorEquals: ["States.ALL"],
          Next: "publishResults",
          ResultPath: "$.pub.error",
        },
      ],
      Type: "Task",
    },
    scriptAlreadyExists: {
      ResultPath: "$.pub.error",
      Result: {
        Error: "ScriptAlreadyExists",
        Cause: "Version override is not allowed.",
      },
      Next: "publishResults",
      Type: "Pass",
    },
    getScript: {
      Resource: "script:get",
      ResultPath: null,
      Next: "scriptAlreadyExists",
      Catch: [
        {
          ErrorEquals: ["NotFoundError"],
          Next: "resolveDependencies",
          ResultPath: null,
        },
        {
          ErrorEquals: ["States.ALL"],
          Next: "publishResults",
          ResultPath: "$.pub.error",
        },
      ],
      Type: "Task",
    },
    resolveDependencies: {
      Resource: "script:resolveDependencies",
      ResultPath: "$.resolvedDependencies",
      Next: "createScript",
      Catch: [
        {
          ErrorEquals: ["States.ALL"],
          Next: "publishResults",
          ResultPath: "$.pub.error",
        },
      ],
      Type: "Task",
    },
    createScript: {
      Resource: "script:create",
      ResultPath: "$.pub.result",
      Next: "logActivity",
      Catch: [
        {
          ErrorEquals: ["States.ALL"],
          Next: "publishResults",
          ResultPath: "$.pub.error",
        },
      ],
      Type: "Task",
    },
    logActivity: {
      Resource: "activities:log",
      ResultPath: null,
      InputPath: "$.activity",
      Next: "publishResults",
      Catch: [
        {
          ErrorEquals: ["States.ALL"],
          Next: "publishResults",
          ResultPath: "$.pub.error",
        },
      ],
      Type: "Task",
    },
    publishResults: {
      Resource: "notification:pub",
      End: true,
      Type: "Task",
    },
  },
});
