{"version":3,"file":"index.mjs","sources":["../../../../src/strapi/remote/flows/index.ts"],"sourcesContent":["import type { TransferStage } from '../../../../types';\n\nexport type Step =\n  | { kind: 'action'; action: string }\n  | { kind: 'transfer'; stage: TransferStage; locked?: boolean };\n\nexport { default as DEFAULT_TRANSFER_FLOW } from './default';\n\ninterface IState {\n  step: Step | null;\n}\n\nexport interface TransferFlow {\n  has(step: Step): boolean;\n  can(step: Step): boolean;\n  cannot(step: Step): boolean;\n  set(step: Step): this;\n  get(): Step | null;\n}\n\nexport const createFlow = (flow: readonly Step[]): TransferFlow => {\n  const state: IState = { step: null };\n\n  /**\n   * Equality check between two steps\n   */\n  const stepEqual = (stepA: Step, stepB: Step): boolean => {\n    if (stepA.kind === 'action' && stepB.kind === 'action') {\n      return stepA.action === stepB.action;\n    }\n\n    if (stepA.kind === 'transfer' && stepB.kind === 'transfer') {\n      return stepA.stage === stepB.stage;\n    }\n\n    return false;\n  };\n\n  /**\n   * Find the index for a given step\n   */\n  const findStepIndex = (step: Step) => flow.findIndex((flowStep) => stepEqual(step, flowStep));\n\n  return {\n    has(step: Step) {\n      return findStepIndex(step) !== -1;\n    },\n\n    can(step: Step) {\n      if (state.step === null) {\n        return true;\n      }\n\n      const indexesDifference = findStepIndex(step) - findStepIndex(state.step);\n\n      // It's possible to send multiple time the same transfer step in a row\n      if (indexesDifference === 0 && step.kind === 'transfer') {\n        return true;\n      }\n\n      return indexesDifference > 0;\n    },\n\n    cannot(step: Step) {\n      return !this.can(step);\n    },\n\n    set(step: Step) {\n      const canSwitch = this.can(step);\n\n      if (!canSwitch) {\n        throw new Error('Impossible to proceed to the given step');\n      }\n\n      state.step = step;\n\n      return this;\n    },\n\n    get() {\n      return state.step;\n    },\n  };\n};\n"],"names":["createFlow","flow","state","step","stepEqual","stepA","stepB","kind","action","stage","findStepIndex","findIndex","flowStep","has","can","indexesDifference","cannot","set","canSwitch","Error","get"],"mappings":"AAoBO,MAAMA,aAAa,CAACC,IAAAA,GAAAA;AAC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