import { makeAppObjectRepo } from "@vived/core";
import { MockAppHandlerEntity } from "../Mocks/MockAppHandlerEntity";
import { makeSetAppStateUC } from "./SetAppStateUC";
import { MockUndoRedo } from "../../UndoRedo/Mocks/MockUndoRedo";
import { MockStateMachine } from "../../StateMachine/Mocks/MockStateMachine";
import { makeBaseAppState } from "../../BaseApp/Entity/BaseAppState";
function makeTestRig() {
    const appObjectRepo = makeAppObjectRepo();
    const appObj = appObjectRepo.getOrCreate("Handler");
    const mockAppHandler = new MockAppHandlerEntity(appObj);
    const mockUndo = new MockUndoRedo(appObj);
    mockUndo.submitAction = jest.fn();
    mockUndo.clearStack = jest.fn();
    const mockStateMachine = new MockStateMachine(appObj);
    const baseApp = makeBaseAppState(appObj);
    const setAppState = makeSetAppStateUC(appObj);
    const mockPayload = {
        finalState: { foo: "bar" },
        hasNextSlide: false,
        hasPreviousSlide: false,
        hideNavigation: false,
    };
    return {
        mockAppHandler,
        setAppState,
        mockStateMachine,
        baseApp,
        mockUndo,
        mockPayload,
    };
}
describe("Start App Base Handler", () => {
    it("Registers as a handler when constructed", () => {
        const { mockAppHandler, setAppState } = makeTestRig();
        expect(mockAppHandler.registerRequestHandler).toHaveBeenCalledWith(setAppState);
    });
    it("Triggers the action with the payload", () => {
        const { setAppState, mockPayload } = makeTestRig();
        setAppState.action = jest.fn();
        setAppState.handleRequest(3, mockPayload);
        expect(setAppState.action).toHaveBeenCalledWith(mockPayload);
    });
    it("Throws for an bungled payload", () => {
        const { setAppState } = makeTestRig();
        expect(() => setAppState.handleRequest(3, { foo: "bar" })).toThrowError();
    });
    it("Throws for an unsupported version", () => {
        const { setAppState, mockPayload } = makeTestRig();
        expect(() => setAppState.handleRequest(-1, mockPayload)).toThrowError();
    });
    it("Passes the state machine the the state and duration", () => {
        const { mockStateMachine, setAppState, mockPayload } = makeTestRig();
        mockPayload.finalState = { foo: "bar" };
        mockPayload.duration = 5;
        setAppState.handleRequest(3, mockPayload);
        expect(mockStateMachine.setSerializedStates).toHaveBeenCalledWith({ foo: "bar" }, 5);
    });
    it("State machine is passed the state without a duration", () => {
        const { mockStateMachine, setAppState, mockPayload } = makeTestRig();
        mockPayload.finalState = { foo: "bar" };
        setAppState.handleRequest(3, mockPayload);
        expect(mockStateMachine.setSerializedStates).toHaveBeenCalledWith({ foo: "bar" }, 0);
    });
    it("Sets to a default state if an empty state is passed", () => {
        const { mockStateMachine, setAppState, mockPayload } = makeTestRig();
        mockPayload.finalState = undefined;
        mockPayload.duration = 5;
        setAppState.handleRequest(3, mockPayload);
        expect(mockStateMachine.setDefaultState).toHaveBeenCalledWith(5);
    });
    it("Resets the is reviewing flag", () => {
        const { baseApp, setAppState, mockPayload } = makeTestRig();
        baseApp.isReviewing = true;
        setAppState.handleRequest(3, mockPayload);
        expect(baseApp.isReviewing).toEqual(false);
    });
    it("Clears the undo stack", () => {
        const { setAppState, mockUndo, mockPayload } = makeTestRig();
        setAppState.handleRequest(3, mockPayload);
        expect(mockUndo.clearStack).toHaveBeenCalled();
    });
    it("Applies the has next state flag", () => {
        const { setAppState, mockPayload, baseApp } = makeTestRig();
        baseApp.hasNextState = false;
        mockPayload.hasNextSlide = true;
        setAppState.handleRequest(3, mockPayload);
        expect(baseApp.hasNextState).toEqual(true);
        mockPayload.hasNextSlide = false;
        setAppState.handleRequest(3, mockPayload);
        expect(baseApp.hasNextState).toEqual(false);
    });
    it("Applies the has previous state flag", () => {
        const { setAppState, mockPayload, baseApp } = makeTestRig();
        baseApp.hasPreviousState = false;
        mockPayload.hasPreviousSlide = true;
        setAppState.handleRequest(3, mockPayload);
        expect(baseApp.hasPreviousState).toEqual(true);
        mockPayload.hasPreviousSlide = false;
        setAppState.handleRequest(3, mockPayload);
        expect(baseApp.hasPreviousState).toEqual(false);
    });
    it("Applies the hide nav flag", () => {
        const { setAppState, mockPayload, baseApp } = makeTestRig();
        baseApp.hideSlideNavigation = false;
        mockPayload.hideNavigation = true;
        setAppState.handleRequest(3, mockPayload);
        expect(baseApp.hideSlideNavigation).toEqual(true);
        mockPayload.hideNavigation = false;
        setAppState.handleRequest(3, mockPayload);
        expect(baseApp.hideSlideNavigation).toEqual(false);
    });
});
//# sourceMappingURL=SetAppStateUC.test.js.map