import { invariant } from "../../../invariant";
import { completeDrop } from "../../action-creators";

const dropAnimiationFinishMiddleware = (store) => (next) => (action) => {
  if (action.type !== "DROP_ANIMATION_FINISHED") {
    next(action);
    return;
  }
  const state = store.getState();
  invariant(state.phase === "DROP_ANIMATING", "Cannot finish a drop animating when no drop is occurring");
  store.dispatch(
    completeDrop({
      completed: state.completed
    })
  );
};

export default dropAnimiationFinishMiddleware;
